blob: 4764f49ab80e32682e3ffe3777dcfcb262f3b822 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200352 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000353 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
355 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000356 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000357 {VV_NAME("swapname", VAR_STRING), VV_RO},
358 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000359 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200360 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000361 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200362 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000363 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
364 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000365 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000368 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200369 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200370 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200371 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200372 {VV_NAME("option_new", VAR_STRING), VV_RO},
373 {VV_NAME("option_old", VAR_STRING), VV_RO},
374 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100375 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100376 {VV_NAME("false", VAR_SPECIAL), VV_RO},
377 {VV_NAME("true", VAR_SPECIAL), VV_RO},
378 {VV_NAME("null", VAR_SPECIAL), VV_RO},
379 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100380 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200381 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000382};
383
384/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_type vv_di.di_tv.v_type
386#define vv_nr vv_di.di_tv.vval.v_number
387#define vv_float vv_di.di_tv.vval.v_float
388#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000389#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200390#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000391#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000392
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200393static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000394#define vimvarht vimvardict.dv_hashtab
395
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100396static void prepare_vimvar(int idx, typval_T *save_tv);
397static void restore_vimvar(int idx, typval_T *save_tv);
398static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
399static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
400static char_u *skip_var_one(char_u *arg);
401static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
402static void list_glob_vars(int *first);
403static void list_buf_vars(int *first);
404static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000407#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100408static void list_vim_vars(int *first);
409static void list_script_vars(int *first);
410static void list_func_vars(int *first);
411static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
412static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
413static int check_changedtick(char_u *arg);
414static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
415static void clear_lval(lval_T *lp);
416static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
417static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
418static void list_fix_watch(list_T *l, listitem_T *item);
419static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
420static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
421static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
422static void item_lock(typval_T *tv, int deep, int lock);
423static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000424
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100425static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
426static int eval1(char_u **arg, typval_T *rettv, int evaluate);
427static int eval2(char_u **arg, typval_T *rettv, int evaluate);
428static int eval3(char_u **arg, typval_T *rettv, int evaluate);
429static int eval4(char_u **arg, typval_T *rettv, int evaluate);
430static int eval5(char_u **arg, typval_T *rettv, int evaluate);
431static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
432static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000433
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100434static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
435static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
438static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200439static void list_free_contents(list_T *l);
440static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100441static long list_len(list_T *l);
442static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
443static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
444static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
445static long list_find_nr(list_T *l, long idx, int *errorp);
446static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100447static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
448static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
449static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200450static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
451static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
452static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100453static int free_unref_items(int copyID);
454static dictitem_T *dictitem_copy(dictitem_T *org);
455static void dictitem_remove(dict_T *dict, dictitem_T *item);
456static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
457static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200460static char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int dict_val);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static char_u *string_quote(char_u *str, int function);
463static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
464static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100465static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
466static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100467static void emsg_funcname(char *ermsg, char_u *name);
468static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200470static void dict_free_contents(dict_T *d);
471static void dict_free_dict(dict_T *d);
472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_abs(typval_T *argvars, typval_T *rettv);
475static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100477static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_and(typval_T *argvars, typval_T *rettv);
479static void f_append(typval_T *argvars, typval_T *rettv);
480static void f_argc(typval_T *argvars, typval_T *rettv);
481static void f_argidx(typval_T *argvars, typval_T *rettv);
482static void f_arglistid(typval_T *argvars, typval_T *rettv);
483static void f_argv(typval_T *argvars, typval_T *rettv);
484static void f_assert_equal(typval_T *argvars, typval_T *rettv);
485static void f_assert_exception(typval_T *argvars, typval_T *rettv);
486static void f_assert_fails(typval_T *argvars, typval_T *rettv);
487static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200488static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200489static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
490static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_asin(typval_T *argvars, typval_T *rettv);
494static void f_atan(typval_T *argvars, typval_T *rettv);
495static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_browse(typval_T *argvars, typval_T *rettv);
498static void f_browsedir(typval_T *argvars, typval_T *rettv);
499static void f_bufexists(typval_T *argvars, typval_T *rettv);
500static void f_buflisted(typval_T *argvars, typval_T *rettv);
501static void f_bufloaded(typval_T *argvars, typval_T *rettv);
502static void f_bufname(typval_T *argvars, typval_T *rettv);
503static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200504static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100505static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
506static void f_byte2line(typval_T *argvars, typval_T *rettv);
507static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
508static void f_byteidx(typval_T *argvars, typval_T *rettv);
509static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
510static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100516static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
517static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100518static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100519static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100520static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100521static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
523static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100524static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100525static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
527static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100528static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100529static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_changenr(typval_T *argvars, typval_T *rettv);
532static void f_char2nr(typval_T *argvars, typval_T *rettv);
533static void f_cindent(typval_T *argvars, typval_T *rettv);
534static void f_clearmatches(typval_T *argvars, typval_T *rettv);
535static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_complete(typval_T *argvars, typval_T *rettv);
538static void f_complete_add(typval_T *argvars, typval_T *rettv);
539static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_confirm(typval_T *argvars, typval_T *rettv);
542static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_cos(typval_T *argvars, typval_T *rettv);
545static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_count(typval_T *argvars, typval_T *rettv);
548static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
549static void f_cursor(typval_T *argsvars, typval_T *rettv);
550static void f_deepcopy(typval_T *argvars, typval_T *rettv);
551static void f_delete(typval_T *argvars, typval_T *rettv);
552static void f_did_filetype(typval_T *argvars, typval_T *rettv);
553static void f_diff_filler(typval_T *argvars, typval_T *rettv);
554static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
555static void f_empty(typval_T *argvars, typval_T *rettv);
556static void f_escape(typval_T *argvars, typval_T *rettv);
557static void f_eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200558static void f_evalcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_eventhandler(typval_T *argvars, typval_T *rettv);
560static void f_executable(typval_T *argvars, typval_T *rettv);
561static void f_exepath(typval_T *argvars, typval_T *rettv);
562static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200563#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200565#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100566static void f_expand(typval_T *argvars, typval_T *rettv);
567static void f_extend(typval_T *argvars, typval_T *rettv);
568static void f_feedkeys(typval_T *argvars, typval_T *rettv);
569static void f_filereadable(typval_T *argvars, typval_T *rettv);
570static void f_filewritable(typval_T *argvars, typval_T *rettv);
571static void f_filter(typval_T *argvars, typval_T *rettv);
572static void f_finddir(typval_T *argvars, typval_T *rettv);
573static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000574#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100575static void f_float2nr(typval_T *argvars, typval_T *rettv);
576static void f_floor(typval_T *argvars, typval_T *rettv);
577static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000578#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100579static void f_fnameescape(typval_T *argvars, typval_T *rettv);
580static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
581static void f_foldclosed(typval_T *argvars, typval_T *rettv);
582static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
583static void f_foldlevel(typval_T *argvars, typval_T *rettv);
584static void f_foldtext(typval_T *argvars, typval_T *rettv);
585static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
586static void f_foreground(typval_T *argvars, typval_T *rettv);
587static void f_function(typval_T *argvars, typval_T *rettv);
588static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
589static void f_get(typval_T *argvars, typval_T *rettv);
590static void f_getbufline(typval_T *argvars, typval_T *rettv);
591static void f_getbufvar(typval_T *argvars, typval_T *rettv);
592static void f_getchar(typval_T *argvars, typval_T *rettv);
593static void f_getcharmod(typval_T *argvars, typval_T *rettv);
594static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
595static void f_getcmdline(typval_T *argvars, typval_T *rettv);
596static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
597static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
598static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
599static void f_getcwd(typval_T *argvars, typval_T *rettv);
600static void f_getfontname(typval_T *argvars, typval_T *rettv);
601static void f_getfperm(typval_T *argvars, typval_T *rettv);
602static void f_getfsize(typval_T *argvars, typval_T *rettv);
603static void f_getftime(typval_T *argvars, typval_T *rettv);
604static void f_getftype(typval_T *argvars, typval_T *rettv);
605static void f_getline(typval_T *argvars, typval_T *rettv);
606static void f_getmatches(typval_T *argvars, typval_T *rettv);
607static void f_getpid(typval_T *argvars, typval_T *rettv);
608static void f_getcurpos(typval_T *argvars, typval_T *rettv);
609static void f_getpos(typval_T *argvars, typval_T *rettv);
610static void f_getqflist(typval_T *argvars, typval_T *rettv);
611static void f_getreg(typval_T *argvars, typval_T *rettv);
612static void f_getregtype(typval_T *argvars, typval_T *rettv);
613static void f_gettabvar(typval_T *argvars, typval_T *rettv);
614static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
615static void f_getwinposx(typval_T *argvars, typval_T *rettv);
616static void f_getwinposy(typval_T *argvars, typval_T *rettv);
617static void f_getwinvar(typval_T *argvars, typval_T *rettv);
618static void f_glob(typval_T *argvars, typval_T *rettv);
619static void f_globpath(typval_T *argvars, typval_T *rettv);
620static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
621static void f_has(typval_T *argvars, typval_T *rettv);
622static void f_has_key(typval_T *argvars, typval_T *rettv);
623static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
624static void f_hasmapto(typval_T *argvars, typval_T *rettv);
625static void f_histadd(typval_T *argvars, typval_T *rettv);
626static void f_histdel(typval_T *argvars, typval_T *rettv);
627static void f_histget(typval_T *argvars, typval_T *rettv);
628static void f_histnr(typval_T *argvars, typval_T *rettv);
629static void f_hlID(typval_T *argvars, typval_T *rettv);
630static void f_hlexists(typval_T *argvars, typval_T *rettv);
631static void f_hostname(typval_T *argvars, typval_T *rettv);
632static void f_iconv(typval_T *argvars, typval_T *rettv);
633static void f_indent(typval_T *argvars, typval_T *rettv);
634static void f_index(typval_T *argvars, typval_T *rettv);
635static void f_input(typval_T *argvars, typval_T *rettv);
636static void f_inputdialog(typval_T *argvars, typval_T *rettv);
637static void f_inputlist(typval_T *argvars, typval_T *rettv);
638static void f_inputrestore(typval_T *argvars, typval_T *rettv);
639static void f_inputsave(typval_T *argvars, typval_T *rettv);
640static void f_inputsecret(typval_T *argvars, typval_T *rettv);
641static void f_insert(typval_T *argvars, typval_T *rettv);
642static void f_invert(typval_T *argvars, typval_T *rettv);
643static void f_isdirectory(typval_T *argvars, typval_T *rettv);
644static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100645#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
646static void f_isnan(typval_T *argvars, typval_T *rettv);
647#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100648static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100650static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100651static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100652static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100653static void f_job_start(typval_T *argvars, typval_T *rettv);
654static void f_job_stop(typval_T *argvars, typval_T *rettv);
655static void f_job_status(typval_T *argvars, typval_T *rettv);
656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100658static void f_js_decode(typval_T *argvars, typval_T *rettv);
659static void f_js_encode(typval_T *argvars, typval_T *rettv);
660static void f_json_decode(typval_T *argvars, typval_T *rettv);
661static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_keys(typval_T *argvars, typval_T *rettv);
663static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
664static void f_len(typval_T *argvars, typval_T *rettv);
665static void f_libcall(typval_T *argvars, typval_T *rettv);
666static void f_libcallnr(typval_T *argvars, typval_T *rettv);
667static void f_line(typval_T *argvars, typval_T *rettv);
668static void f_line2byte(typval_T *argvars, typval_T *rettv);
669static void f_lispindent(typval_T *argvars, typval_T *rettv);
670static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_log(typval_T *argvars, typval_T *rettv);
673static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000674#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200675#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_map(typval_T *argvars, typval_T *rettv);
679static void f_maparg(typval_T *argvars, typval_T *rettv);
680static void f_mapcheck(typval_T *argvars, typval_T *rettv);
681static void f_match(typval_T *argvars, typval_T *rettv);
682static void f_matchadd(typval_T *argvars, typval_T *rettv);
683static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
684static void f_matcharg(typval_T *argvars, typval_T *rettv);
685static void f_matchdelete(typval_T *argvars, typval_T *rettv);
686static void f_matchend(typval_T *argvars, typval_T *rettv);
687static void f_matchlist(typval_T *argvars, typval_T *rettv);
688static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200689static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_max(typval_T *argvars, typval_T *rettv);
691static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000692#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100696#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100698#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
700static void f_nr2char(typval_T *argvars, typval_T *rettv);
701static void f_or(typval_T *argvars, typval_T *rettv);
702static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100703#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100705#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000708#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
710static void f_printf(typval_T *argvars, typval_T *rettv);
711static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200714#endif
715#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200717#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_range(typval_T *argvars, typval_T *rettv);
719static void f_readfile(typval_T *argvars, typval_T *rettv);
720static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100721#ifdef FEAT_FLOAT
722static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_reltimestr(typval_T *argvars, typval_T *rettv);
725static void f_remote_expr(typval_T *argvars, typval_T *rettv);
726static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
727static void f_remote_peek(typval_T *argvars, typval_T *rettv);
728static void f_remote_read(typval_T *argvars, typval_T *rettv);
729static void f_remote_send(typval_T *argvars, typval_T *rettv);
730static void f_remove(typval_T *argvars, typval_T *rettv);
731static void f_rename(typval_T *argvars, typval_T *rettv);
732static void f_repeat(typval_T *argvars, typval_T *rettv);
733static void f_resolve(typval_T *argvars, typval_T *rettv);
734static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000737#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100738static void f_screenattr(typval_T *argvars, typval_T *rettv);
739static void f_screenchar(typval_T *argvars, typval_T *rettv);
740static void f_screencol(typval_T *argvars, typval_T *rettv);
741static void f_screenrow(typval_T *argvars, typval_T *rettv);
742static void f_search(typval_T *argvars, typval_T *rettv);
743static void f_searchdecl(typval_T *argvars, typval_T *rettv);
744static void f_searchpair(typval_T *argvars, typval_T *rettv);
745static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
746static void f_searchpos(typval_T *argvars, typval_T *rettv);
747static void f_server2client(typval_T *argvars, typval_T *rettv);
748static void f_serverlist(typval_T *argvars, typval_T *rettv);
749static void f_setbufvar(typval_T *argvars, typval_T *rettv);
750static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
751static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100752static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100753static void f_setline(typval_T *argvars, typval_T *rettv);
754static void f_setloclist(typval_T *argvars, typval_T *rettv);
755static void f_setmatches(typval_T *argvars, typval_T *rettv);
756static void f_setpos(typval_T *argvars, typval_T *rettv);
757static void f_setqflist(typval_T *argvars, typval_T *rettv);
758static void f_setreg(typval_T *argvars, typval_T *rettv);
759static void f_settabvar(typval_T *argvars, typval_T *rettv);
760static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
761static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100762#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100764#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_shellescape(typval_T *argvars, typval_T *rettv);
766static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
767static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000768#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_sin(typval_T *argvars, typval_T *rettv);
770static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_sort(typval_T *argvars, typval_T *rettv);
773static void f_soundfold(typval_T *argvars, typval_T *rettv);
774static void f_spellbadword(typval_T *argvars, typval_T *rettv);
775static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
776static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_sqrt(typval_T *argvars, typval_T *rettv);
779static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000780#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_str2nr(typval_T *argvars, typval_T *rettv);
782static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000785#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200786static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_stridx(typval_T *argvars, typval_T *rettv);
788static void f_string(typval_T *argvars, typval_T *rettv);
789static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200790static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100791static void f_strpart(typval_T *argvars, typval_T *rettv);
792static void f_strridx(typval_T *argvars, typval_T *rettv);
793static void f_strtrans(typval_T *argvars, typval_T *rettv);
794static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
795static void f_strwidth(typval_T *argvars, typval_T *rettv);
796static void f_submatch(typval_T *argvars, typval_T *rettv);
797static void f_substitute(typval_T *argvars, typval_T *rettv);
798static void f_synID(typval_T *argvars, typval_T *rettv);
799static void f_synIDattr(typval_T *argvars, typval_T *rettv);
800static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
801static void f_synstack(typval_T *argvars, typval_T *rettv);
802static void f_synconcealed(typval_T *argvars, typval_T *rettv);
803static void f_system(typval_T *argvars, typval_T *rettv);
804static void f_systemlist(typval_T *argvars, typval_T *rettv);
805static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
806static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
807static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
808static void f_taglist(typval_T *argvars, typval_T *rettv);
809static void f_tagfiles(typval_T *argvars, typval_T *rettv);
810static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200811static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
812static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200813static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
814#ifdef FEAT_JOB_CHANNEL
815static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
816#endif
817static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
818#ifdef FEAT_JOB_CHANNEL
819static void f_test_null_job(typval_T *argvars, typval_T *rettv);
820#endif
821static void f_test_null_list(typval_T *argvars, typval_T *rettv);
822static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
823static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200824static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200825#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static void f_tan(typval_T *argvars, typval_T *rettv);
827static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200828#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100829#ifdef FEAT_TIMERS
830static void f_timer_start(typval_T *argvars, typval_T *rettv);
831static void f_timer_stop(typval_T *argvars, typval_T *rettv);
832#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_tolower(typval_T *argvars, typval_T *rettv);
834static void f_toupper(typval_T *argvars, typval_T *rettv);
835static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000836#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000838#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static void f_type(typval_T *argvars, typval_T *rettv);
840static void f_undofile(typval_T *argvars, typval_T *rettv);
841static void f_undotree(typval_T *argvars, typval_T *rettv);
842static void f_uniq(typval_T *argvars, typval_T *rettv);
843static void f_values(typval_T *argvars, typval_T *rettv);
844static void f_virtcol(typval_T *argvars, typval_T *rettv);
845static void f_visualmode(typval_T *argvars, typval_T *rettv);
846static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100847static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100848static void f_win_getid(typval_T *argvars, typval_T *rettv);
849static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
850static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
851static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static void f_winbufnr(typval_T *argvars, typval_T *rettv);
853static void f_wincol(typval_T *argvars, typval_T *rettv);
854static void f_winheight(typval_T *argvars, typval_T *rettv);
855static void f_winline(typval_T *argvars, typval_T *rettv);
856static void f_winnr(typval_T *argvars, typval_T *rettv);
857static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
858static void f_winrestview(typval_T *argvars, typval_T *rettv);
859static void f_winsaveview(typval_T *argvars, typval_T *rettv);
860static void f_winwidth(typval_T *argvars, typval_T *rettv);
861static void f_writefile(typval_T *argvars, typval_T *rettv);
862static void f_wordcount(typval_T *argvars, typval_T *rettv);
863static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000864
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100865static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
866static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
867static int get_env_len(char_u **arg);
868static int get_id_len(char_u **arg);
869static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
870static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000871#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
872#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
873 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
875static int eval_isnamec(int c);
876static int eval_isnamec1(int c);
877static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
878static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879static typval_T *alloc_string_tv(char_u *string);
880static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100881#ifdef FEAT_FLOAT
882static float_T get_tv_float(typval_T *varp);
883#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884static linenr_T get_tv_lnum(typval_T *argvars);
885static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
887static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
888static hashtab_T *find_var_ht(char_u *name, char_u **varname);
889static funccall_T *get_funccal(void);
890static void vars_clear_ext(hashtab_T *ht, int free_val);
891static void delete_var(hashtab_T *ht, hashitem_T *hi);
892static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
893static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
894static void set_var(char_u *name, typval_T *varp, int copy);
895static int var_check_ro(int flags, char_u *name, int use_gettext);
896static int var_check_fixed(int flags, char_u *name, int use_gettext);
897static int var_check_func_name(char_u *name, int new_var);
898static int valid_varname(char_u *varname);
899static int tv_check_lock(int lock, char_u *name, int use_gettext);
900static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
901static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100902static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100903static int eval_fname_script(char_u *p);
904static int eval_fname_sid(char_u *p);
905static void list_func_head(ufunc_T *fp, int indent);
906static ufunc_T *find_func(char_u *name);
907static int function_exists(char_u *name);
908static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100910static void func_do_profile(ufunc_T *fp);
911static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
912static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000913static int
914# ifdef __BORLANDC__
915 _RTLENTRYF
916# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100917 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000918static int
919# ifdef __BORLANDC__
920 _RTLENTRYF
921# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100922 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000923#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100924static int script_autoload(char_u *name, int reload);
925static char_u *autoload_name(char_u *name);
926static void cat_func_name(char_u *buf, ufunc_T *fp);
927static void func_free(ufunc_T *fp);
928static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
929static int can_free_funccal(funccall_T *fc, int copyID) ;
930static void free_funccal(funccall_T *fc, int free_val);
931static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
932static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
933static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
934static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
935static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
936static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
937static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
938static int write_list(FILE *fd, list_T *list, int binary);
939static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200941
942#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100943static int compare_func_name(const void *s1, const void *s2);
944static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200945#endif
946
Bram Moolenaar33570922005-01-25 22:26:29 +0000947/*
948 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000949 */
950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100951eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000952{
Bram Moolenaar33570922005-01-25 22:26:29 +0000953 int i;
954 struct vimvar *p;
955
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200956 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
957 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200958 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000959 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000960 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000961
962 for (i = 0; i < VV_LEN; ++i)
963 {
964 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200965 if (STRLEN(p->vv_name) > 16)
966 {
967 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
968 getout(1);
969 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 STRCPY(p->vv_di.di_key, p->vv_name);
971 if (p->vv_flags & VV_RO)
972 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
973 else if (p->vv_flags & VV_RO_SBX)
974 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
975 else
976 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000977
978 /* add to v: scope dict, unless the value is not always available */
979 if (p->vv_type != VAR_UNKNOWN)
980 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000981 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000982 /* add to compat scope dict */
983 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000984 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100985 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
986
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000987 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100988 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200989 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100990 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100991
992 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
993 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
994 set_vim_var_nr(VV_NONE, VVAL_NONE);
995 set_vim_var_nr(VV_NULL, VVAL_NULL);
996
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200997 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200998
999#ifdef EBCDIC
1000 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +01001001 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001002 */
1003 sortFunctions();
1004#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001005}
1006
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001007#if defined(EXITFREE) || defined(PROTO)
1008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001009eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010{
1011 int i;
1012 struct vimvar *p;
1013
1014 for (i = 0; i < VV_LEN; ++i)
1015 {
1016 p = &vimvars[i];
1017 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001018 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001019 vim_free(p->vv_str);
1020 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001021 }
1022 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1023 {
1024 list_unref(p->vv_list);
1025 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001026 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001027 }
1028 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001029 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030 hash_clear(&compat_hashtab);
1031
Bram Moolenaard9fba312005-06-26 22:34:35 +00001032 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001033# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001034 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001035# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001036
1037 /* global variables */
1038 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001039
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001040 /* autoloaded script names */
1041 ga_clear_strings(&ga_loaded);
1042
Bram Moolenaarcca74132013-09-25 21:00:28 +02001043 /* Script-local variables. First clear all the variables and in a second
1044 * loop free the scriptvar_T, because a variable in one script might hold
1045 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001046 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001047 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001048 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001049 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001050 ga_clear(&ga_scripts);
1051
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001052 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001053 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001054
1055 /* functions */
1056 free_all_functions();
1057 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001058}
1059#endif
1060
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 * Return the name of the executed function.
1063 */
1064 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001065func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068}
1069
1070/*
1071 * Return the address holding the next breakpoint line for a funccall cookie.
1072 */
1073 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001074func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077}
1078
1079/*
1080 * Return the address holding the debug tick for a funccall cookie.
1081 */
1082 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001083func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084{
Bram Moolenaar33570922005-01-25 22:26:29 +00001085 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086}
1087
1088/*
1089 * Return the nesting level for a funccall cookie.
1090 */
1091 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001092func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
Bram Moolenaar33570922005-01-25 22:26:29 +00001094 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095}
1096
1097/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001098funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001100/* pointer to list of previously used funccal, still around because some
1101 * item in it is still being used. */
1102funccall_T *previous_funccal = NULL;
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104/*
1105 * Return TRUE when a function was ended by a ":return" command.
1106 */
1107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001108current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 return current_funccal->returned;
1111}
1112
1113
1114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * Set an internal variable to a string value. Creates the variable if it does
1116 * not already exist.
1117 */
1118 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001119set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001121 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
1124 val = vim_strsave(value);
1125 if (val != NULL)
1126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001128 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001130 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133 }
1134}
1135
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001137#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139static char_u *redir_endp = NULL;
1140static char_u *redir_varname = NULL;
1141
1142/*
1143 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001144 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 * Returns OK if successfully completed the setup. FAIL otherwise.
1146 */
1147 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001148var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149{
1150 int save_emsg;
1151 int err;
1152 typval_T tv;
1153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001155 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 {
1157 EMSG(_(e_invarg));
1158 return FAIL;
1159 }
1160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 redir_varname = vim_strsave(name);
1163 if (redir_varname == NULL)
1164 return FAIL;
1165
1166 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1167 if (redir_lval == NULL)
1168 {
1169 var_redir_stop();
1170 return FAIL;
1171 }
1172
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173 /* The output is stored in growarray "redir_ga" until redirection ends. */
1174 ga_init2(&redir_ga, (int)sizeof(char), 500);
1175
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001177 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001178 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1180 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001181 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 if (redir_endp != NULL && *redir_endp != NUL)
1183 /* Trailing characters are present after the variable name */
1184 EMSG(_(e_trailing));
1185 else
1186 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001187 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188 var_redir_stop();
1189 return FAIL;
1190 }
1191
1192 /* check if we can write to the variable: set it to or append an empty
1193 * string */
1194 save_emsg = did_emsg;
1195 did_emsg = FALSE;
1196 tv.v_type = VAR_STRING;
1197 tv.vval.v_string = (char_u *)"";
1198 if (append)
1199 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1200 else
1201 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001202 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001204 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 if (err)
1206 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001207 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 var_redir_stop();
1209 return FAIL;
1210 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211
1212 return OK;
1213}
1214
1215/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216 * Append "value[value_len]" to the variable set by var_redir_start().
1217 * The actual appending is postponed until redirection ends, because the value
1218 * appended may in fact be the string we write to, changing it may cause freed
1219 * memory to be used:
1220 * :redir => foo
1221 * :let foo
1222 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001223 */
1224 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001225var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001226{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001227 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228
1229 if (redir_lval == NULL)
1230 return;
1231
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001235 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001237 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001238 {
1239 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001240 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001241 }
1242 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001243 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001244}
1245
1246/*
1247 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001248 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001249 */
1250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001251var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001252{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001253 typval_T tv;
1254
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001255 if (EVALCMD_BUSY)
1256 {
1257 redir_lval = NULL;
1258 return;
1259 }
1260
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001261 if (redir_lval != NULL)
1262 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001263 /* If there was no error: assign the text to the variable. */
1264 if (redir_endp != NULL)
1265 {
1266 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1267 tv.v_type = VAR_STRING;
1268 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001269 /* Call get_lval() again, if it's inside a Dict or List it may
1270 * have changed. */
1271 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001272 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001273 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1274 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1275 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001276 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001277
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001278 /* free the collected output */
1279 vim_free(redir_ga.ga_data);
1280 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001281
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001282 vim_free(redir_lval);
1283 redir_lval = NULL;
1284 }
1285 vim_free(redir_varname);
1286 redir_varname = NULL;
1287}
1288
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289# if defined(FEAT_MBYTE) || defined(PROTO)
1290 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001291eval_charconvert(
1292 char_u *enc_from,
1293 char_u *enc_to,
1294 char_u *fname_from,
1295 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int err = FALSE;
1298
1299 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1300 set_vim_var_string(VV_CC_TO, enc_to, -1);
1301 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1302 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1303 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1304 err = TRUE;
1305 set_vim_var_string(VV_CC_FROM, NULL, -1);
1306 set_vim_var_string(VV_CC_TO, NULL, -1);
1307 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1308 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1309
1310 if (err)
1311 return FAIL;
1312 return OK;
1313}
1314# endif
1315
1316# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1317 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int err = FALSE;
1321
1322 set_vim_var_string(VV_FNAME_IN, fname, -1);
1323 set_vim_var_string(VV_CMDARG, args, -1);
1324 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1325 err = TRUE;
1326 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1327 set_vim_var_string(VV_CMDARG, NULL, -1);
1328
1329 if (err)
1330 {
1331 mch_remove(fname);
1332 return FAIL;
1333 }
1334 return OK;
1335}
1336# endif
1337
1338# if defined(FEAT_DIFF) || defined(PROTO)
1339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001340eval_diff(
1341 char_u *origfile,
1342 char_u *newfile,
1343 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 int err = FALSE;
1346
1347 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1348 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1349 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1350 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1351 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1352 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1353 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1354}
1355
1356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001357eval_patch(
1358 char_u *origfile,
1359 char_u *difffile,
1360 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 int err;
1363
1364 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1365 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1366 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1367 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1368 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1369 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1370 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1371}
1372# endif
1373
1374/*
1375 * Top level evaluation function, returning a boolean.
1376 * Sets "error" to TRUE if there was an error.
1377 * Return TRUE or FALSE.
1378 */
1379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001380eval_to_bool(
1381 char_u *arg,
1382 int *error,
1383 char_u **nextcmd,
1384 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
Bram Moolenaar33570922005-01-25 22:26:29 +00001386 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001387 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 if (skip)
1390 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 else
1394 {
1395 *error = FALSE;
1396 if (!skip)
1397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001398 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401 }
1402 if (skip)
1403 --emsg_skip;
1404
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001405 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406}
1407
1408/*
1409 * Top level evaluation function, returning a string. If "skip" is TRUE,
1410 * only parsing to "nextcmd" is done, without reporting errors. Return
1411 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1412 */
1413 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001414eval_to_string_skip(
1415 char_u *arg,
1416 char_u **nextcmd,
1417 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
Bram Moolenaar33570922005-01-25 22:26:29 +00001419 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 char_u *retval;
1421
1422 if (skip)
1423 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 retval = NULL;
1426 else
1427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 retval = vim_strsave(get_tv_string(&tv));
1429 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431 if (skip)
1432 --emsg_skip;
1433
1434 return retval;
1435}
1436
1437/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001438 * Skip over an expression at "*pp".
1439 * Return FAIL for an error, OK otherwise.
1440 */
1441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001442skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001443{
Bram Moolenaar33570922005-01-25 22:26:29 +00001444 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001445
1446 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001448}
1449
1450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001452 * When "convert" is TRUE convert a List into a sequence of lines and convert
1453 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 * Return pointer to allocated memory, or NULL for failure.
1455 */
1456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457eval_to_string(
1458 char_u *arg,
1459 char_u **nextcmd,
1460 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
Bram Moolenaar33570922005-01-25 22:26:29 +00001462 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001464 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001465#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001466 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 retval = NULL;
1471 else
1472 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001473 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001474 {
1475 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001476 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001477 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001478 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001479 if (tv.vval.v_list->lv_len > 0)
1480 ga_append(&ga, NL);
1481 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001482 ga_append(&ga, NUL);
1483 retval = (char_u *)ga.ga_data;
1484 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001485#ifdef FEAT_FLOAT
1486 else if (convert && tv.v_type == VAR_FLOAT)
1487 {
1488 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1489 retval = vim_strsave(numbuf);
1490 }
1491#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001492 else
1493 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496
1497 return retval;
1498}
1499
1500/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001501 * Call eval_to_string() without using current local variables and using
1502 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 */
1504 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001505eval_to_string_safe(
1506 char_u *arg,
1507 char_u **nextcmd,
1508 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 char_u *retval;
1511 void *save_funccalp;
1512
1513 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001514 if (use_sandbox)
1515 ++sandbox;
1516 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001517 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001518 if (use_sandbox)
1519 --sandbox;
1520 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 restore_funccal(save_funccalp);
1522 return retval;
1523}
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525/*
1526 * Top level evaluation function, returning a number.
1527 * Evaluates "expr" silently.
1528 * Returns -1 for an error.
1529 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001530 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001531eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaar33570922005-01-25 22:26:29 +00001533 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001534 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001535 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536
1537 ++emsg_off;
1538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001539 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 retval = -1;
1541 else
1542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001543 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001544 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546 --emsg_off;
1547
1548 return retval;
1549}
1550
Bram Moolenaara40058a2005-07-11 22:42:07 +00001551/*
1552 * Prepare v: variable "idx" to be used.
1553 * Save the current typeval in "save_tv".
1554 * When not used yet add the variable to the v: hashtable.
1555 */
1556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001557prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001558{
1559 *save_tv = vimvars[idx].vv_tv;
1560 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1561 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1562}
1563
1564/*
1565 * Restore v: variable "idx" to typeval "save_tv".
1566 * When no longer defined, remove the variable from the v: hashtable.
1567 */
1568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001570{
1571 hashitem_T *hi;
1572
Bram Moolenaara40058a2005-07-11 22:42:07 +00001573 vimvars[idx].vv_tv = *save_tv;
1574 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1575 {
1576 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1577 if (HASHITEM_EMPTY(hi))
1578 EMSG2(_(e_intern2), "restore_vimvar()");
1579 else
1580 hash_remove(&vimvarht, hi);
1581 }
1582}
1583
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001584#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001585/*
1586 * Evaluate an expression to a list with suggestions.
1587 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001588 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001589 */
1590 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001591eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001592{
1593 typval_T save_val;
1594 typval_T rettv;
1595 list_T *list = NULL;
1596 char_u *p = skipwhite(expr);
1597
1598 /* Set "v:val" to the bad word. */
1599 prepare_vimvar(VV_VAL, &save_val);
1600 vimvars[VV_VAL].vv_type = VAR_STRING;
1601 vimvars[VV_VAL].vv_str = badword;
1602 if (p_verbose == 0)
1603 ++emsg_off;
1604
1605 if (eval1(&p, &rettv, TRUE) == OK)
1606 {
1607 if (rettv.v_type != VAR_LIST)
1608 clear_tv(&rettv);
1609 else
1610 list = rettv.vval.v_list;
1611 }
1612
1613 if (p_verbose == 0)
1614 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001615 restore_vimvar(VV_VAL, &save_val);
1616
1617 return list;
1618}
1619
1620/*
1621 * "list" is supposed to contain two items: a word and a number. Return the
1622 * word in "pp" and the number as the return value.
1623 * Return -1 if anything isn't right.
1624 * Used to get the good word and score from the eval_spell_expr() result.
1625 */
1626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001628{
1629 listitem_T *li;
1630
1631 li = list->lv_first;
1632 if (li == NULL)
1633 return -1;
1634 *pp = get_tv_string(&li->li_tv);
1635
1636 li = li->li_next;
1637 if (li == NULL)
1638 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001639 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001640}
1641#endif
1642
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001643/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001644 * Top level evaluation function.
1645 * Returns an allocated typval_T with the result.
1646 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647 */
1648 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001649eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001650{
1651 typval_T *tv;
1652
1653 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001654 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001655 {
1656 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001657 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001658 }
1659
1660 return tv;
1661}
1662
1663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001666 * Uses argv[argc] for the function arguments. Only Number and String
1667 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001671call_vim_function(
1672 char_u *func,
1673 int argc,
1674 char_u **argv,
1675 int safe, /* use the sandbox */
1676 int str_arg_only, /* all arguments are strings */
1677 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
Bram Moolenaar33570922005-01-25 22:26:29 +00001679 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001680 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 int len;
1682 int i;
1683 int doesrange;
1684 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001687 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
1691 for (i = 0; i < argc; i++)
1692 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001693 /* Pass a NULL or empty argument as an empty string */
1694 if (argv[i] == NULL || *argv[i] == NUL)
1695 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001696 argvars[i].v_type = VAR_STRING;
1697 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001698 continue;
1699 }
1700
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001701 if (str_arg_only)
1702 len = 0;
1703 else
1704 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001705 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (len != 0 && len == (int)STRLEN(argv[i]))
1707 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001708 argvars[i].v_type = VAR_NUMBER;
1709 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711 else
1712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001713 argvars[i].v_type = VAR_STRING;
1714 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 }
1716 }
1717
1718 if (safe)
1719 {
1720 save_funccalp = save_funccal();
1721 ++sandbox;
1722 }
1723
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1725 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001727 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (safe)
1729 {
1730 --sandbox;
1731 restore_funccal(save_funccalp);
1732 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733 vim_free(argvars);
1734
1735 if (ret == FAIL)
1736 clear_tv(rettv);
1737
1738 return ret;
1739}
1740
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001741/*
1742 * Call vimL function "func" and return the result as a number.
1743 * Returns -1 when calling the function fails.
1744 * Uses argv[argc] for the function arguments.
1745 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001746 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747call_func_retnr(
1748 char_u *func,
1749 int argc,
1750 char_u **argv,
1751 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001752{
1753 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001754 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001755
1756 /* All arguments are passed as strings, no conversion to number. */
1757 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1758 return -1;
1759
1760 retval = get_tv_number_chk(&rettv, NULL);
1761 clear_tv(&rettv);
1762 return retval;
1763}
1764
1765#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1766 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1767
Bram Moolenaar4f688582007-07-24 12:34:30 +00001768# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001769/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001770 * Call vimL function "func" and return the result as a string.
1771 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772 * Uses argv[argc] for the function arguments.
1773 */
1774 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775call_func_retstr(
1776 char_u *func,
1777 int argc,
1778 char_u **argv,
1779 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780{
1781 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001782 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001784 /* All arguments are passed as strings, no conversion to number. */
1785 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001786 return NULL;
1787
1788 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001789 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 return retval;
1791}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001792# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001793
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001794/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001795 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001796 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001797 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001798 */
1799 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800call_func_retlist(
1801 char_u *func,
1802 int argc,
1803 char_u **argv,
1804 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001805{
1806 typval_T rettv;
1807
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001808 /* All arguments are passed as strings, no conversion to number. */
1809 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001810 return NULL;
1811
1812 if (rettv.v_type != VAR_LIST)
1813 {
1814 clear_tv(&rettv);
1815 return NULL;
1816 }
1817
1818 return rettv.vval.v_list;
1819}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#endif
1821
1822/*
1823 * Save the current function call pointer, and set it to NULL.
1824 * Used when executing autocommands and for ":source".
1825 */
1826 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001829 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 current_funccal = NULL;
1832 return (void *)fc;
1833}
1834
1835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001836restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001838 funccall_T *fc = (funccall_T *)vfc;
1839
1840 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841}
1842
Bram Moolenaar05159a02005-02-26 23:04:13 +00001843#if defined(FEAT_PROFILE) || defined(PROTO)
1844/*
1845 * Prepare profiling for entering a child or something else that is not
1846 * counted for the script/function itself.
1847 * Should always be called in pair with prof_child_exit().
1848 */
1849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001850prof_child_enter(
1851 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001852{
1853 funccall_T *fc = current_funccal;
1854
1855 if (fc != NULL && fc->func->uf_profiling)
1856 profile_start(&fc->prof_child);
1857 script_prof_save(tm);
1858}
1859
1860/*
1861 * Take care of time spent in a child.
1862 * Should always be called after prof_child_enter().
1863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865prof_child_exit(
1866 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001867{
1868 funccall_T *fc = current_funccal;
1869
1870 if (fc != NULL && fc->func->uf_profiling)
1871 {
1872 profile_end(&fc->prof_child);
1873 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1874 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1875 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1876 }
1877 script_prof_restore(tm);
1878}
1879#endif
1880
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882#ifdef FEAT_FOLDING
1883/*
1884 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1885 * it in "*cp". Doesn't give error messages.
1886 */
1887 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
Bram Moolenaar33570922005-01-25 22:26:29 +00001890 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001891 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001893 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1894 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001897 if (use_sandbox)
1898 ++sandbox;
1899 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 retval = 0;
1903 else
1904 {
1905 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 if (tv.v_type == VAR_NUMBER)
1907 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001908 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 retval = 0;
1910 else
1911 {
1912 /* If the result is a string, check if there is a non-digit before
1913 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (!VIM_ISDIGIT(*s) && *s != '-')
1916 *cp = *s++;
1917 retval = atol((char *)s);
1918 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001922 if (use_sandbox)
1923 --sandbox;
1924 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001926 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927}
1928#endif
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 * ":let" list all variable values
1932 * ":let var1 var2" list variable values
1933 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 * ":let var += expr" assignment command.
1935 * ":let var -= expr" assignment command.
1936 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 */
1939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001940ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
1942 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 int var_count = 0;
1947 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001949 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001950 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
Bram Moolenaardb552d602006-03-23 22:59:57 +00001952 argend = skip_var_list(arg, &var_count, &semicolon);
1953 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001955 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1956 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001957 expr = skipwhite(argend);
1958 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1959 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001961 /*
1962 * ":let" without "=": list variables
1963 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 if (*arg == '[')
1965 EMSG(_(e_invarg));
1966 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001968 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001972 list_glob_vars(&first);
1973 list_buf_vars(&first);
1974 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001975#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001976 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001977#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001978 list_script_vars(&first);
1979 list_func_vars(&first);
1980 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 eap->nextcmd = check_nextcmd(arg);
1983 }
1984 else
1985 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 op[0] = '=';
1987 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001988 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001990 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1991 op[0] = *expr; /* +=, -= or .= */
1992 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001994 else
1995 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (eap->skip)
1998 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (eap->skip)
2001 {
2002 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002003 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 --emsg_skip;
2005 }
2006 else if (i != FAIL)
2007 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 }
2013}
2014
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015/*
2016 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2017 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002018 * When "nextchars" is not NULL it points to a string with characters that
2019 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2020 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 * Returns OK or FAIL;
2022 */
2023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002024ex_let_vars(
2025 char_u *arg_start,
2026 typval_T *tv,
2027 int copy, /* copy values from "tv", don't move */
2028 int semicolon, /* from skip_var_list() */
2029 int var_count, /* from skip_var_list() */
2030 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031{
2032 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002033 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002035 listitem_T *item;
2036 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037
2038 if (*arg != '[')
2039 {
2040 /*
2041 * ":let var = expr" or ":for var in list"
2042 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 return FAIL;
2045 return OK;
2046 }
2047
2048 /*
2049 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2050 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002051 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 {
2053 EMSG(_(e_listreq));
2054 return FAIL;
2055 }
2056
2057 i = list_len(l);
2058 if (semicolon == 0 && var_count < i)
2059 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002060 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 return FAIL;
2062 }
2063 if (var_count - semicolon > i)
2064 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002065 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 return FAIL;
2067 }
2068
2069 item = l->lv_first;
2070 while (*arg != ']')
2071 {
2072 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002073 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002074 item = item->li_next;
2075 if (arg == NULL)
2076 return FAIL;
2077
2078 arg = skipwhite(arg);
2079 if (*arg == ';')
2080 {
2081 /* Put the rest of the list (may be empty) in the var after ';'.
2082 * Create a new list for this. */
2083 l = list_alloc();
2084 if (l == NULL)
2085 return FAIL;
2086 while (item != NULL)
2087 {
2088 list_append_tv(l, &item->li_tv);
2089 item = item->li_next;
2090 }
2091
2092 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002093 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094 ltv.vval.v_list = l;
2095 l->lv_refcount = 1;
2096
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2098 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099 clear_tv(&ltv);
2100 if (arg == NULL)
2101 return FAIL;
2102 break;
2103 }
2104 else if (*arg != ',' && *arg != ']')
2105 {
2106 EMSG2(_(e_intern2), "ex_let_vars()");
2107 return FAIL;
2108 }
2109 }
2110
2111 return OK;
2112}
2113
2114/*
2115 * Skip over assignable variable "var" or list of variables "[var, var]".
2116 * Used for ":let varvar = expr" and ":for varvar in expr".
2117 * For "[var, var]" increment "*var_count" for each variable.
2118 * for "[var, var; var]" set "semicolon".
2119 * Return NULL for an error.
2120 */
2121 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002122skip_var_list(
2123 char_u *arg,
2124 int *var_count,
2125 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126{
2127 char_u *p, *s;
2128
2129 if (*arg == '[')
2130 {
2131 /* "[var, var]": find the matching ']'. */
2132 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002133 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134 {
2135 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2136 s = skip_var_one(p);
2137 if (s == p)
2138 {
2139 EMSG2(_(e_invarg2), p);
2140 return NULL;
2141 }
2142 ++*var_count;
2143
2144 p = skipwhite(s);
2145 if (*p == ']')
2146 break;
2147 else if (*p == ';')
2148 {
2149 if (*semicolon == 1)
2150 {
2151 EMSG(_("Double ; in list of variables"));
2152 return NULL;
2153 }
2154 *semicolon = 1;
2155 }
2156 else if (*p != ',')
2157 {
2158 EMSG2(_(e_invarg2), p);
2159 return NULL;
2160 }
2161 }
2162 return p + 1;
2163 }
2164 else
2165 return skip_var_one(arg);
2166}
2167
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002168/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002169 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002170 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002171 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002172 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002175 if (*arg == '@' && arg[1] != NUL)
2176 return arg + 2;
2177 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2178 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002179}
2180
Bram Moolenaara7043832005-01-21 11:56:39 +00002181/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002182 * List variables for hashtab "ht" with prefix "prefix".
2183 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_hashtable_vars(
2187 hashtab_T *ht,
2188 char_u *prefix,
2189 int empty,
2190 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002191{
Bram Moolenaar33570922005-01-25 22:26:29 +00002192 hashitem_T *hi;
2193 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002194 int todo;
2195
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002196 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002197 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2198 {
2199 if (!HASHITEM_EMPTY(hi))
2200 {
2201 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002202 di = HI2DI(hi);
2203 if (empty || di->di_tv.v_type != VAR_STRING
2204 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002206 }
2207 }
2208}
2209
2210/*
2211 * List global variables.
2212 */
2213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002215{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002217}
2218
2219/*
2220 * List buffer variables.
2221 */
2222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002224{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 char_u numbuf[NUMBUFLEN];
2226
Bram Moolenaar429fa852013-04-15 12:27:36 +02002227 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229
2230 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2232 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002233}
2234
2235/*
2236 * List window variables.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002240{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002241 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002243}
2244
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#ifdef FEAT_WINDOWS
2246/*
2247 * List tab page variables.
2248 */
2249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002252 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002253 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254}
2255#endif
2256
Bram Moolenaara7043832005-01-21 11:56:39 +00002257/*
2258 * List Vim variables.
2259 */
2260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002261list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264}
2265
2266/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002267 * List script-local variables, if there is a script.
2268 */
2269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002270list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002271{
2272 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2274 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002275}
2276
2277/*
2278 * List function variables, if there is a function.
2279 */
2280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002281list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002282{
2283 if (current_funccal != NULL)
2284 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002285 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002286}
2287
2288/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 * List variables in "arg".
2290 */
2291 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002292list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293{
2294 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 char_u *name_start;
2298 char_u *arg_subsc;
2299 char_u *tofree;
2300 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301
2302 while (!ends_excmd(*arg) && !got_int)
2303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002306 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2308 {
2309 emsg_severe = TRUE;
2310 EMSG(_(e_trailing));
2311 break;
2312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 /* get_name_len() takes care of expanding curly braces */
2317 name_start = name = arg;
2318 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2319 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 /* This is mainly to keep test 49 working: when expanding
2322 * curly braces fails overrule the exception error message. */
2323 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 emsg_severe = TRUE;
2326 EMSG2(_(e_invarg2), arg);
2327 break;
2328 }
2329 error = TRUE;
2330 }
2331 else
2332 {
2333 if (tofree != NULL)
2334 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002335 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 else
2338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339 /* handle d.key, l[idx], f(expr) */
2340 arg_subsc = arg;
2341 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002342 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002344 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002346 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002347 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002349 case 'g': list_glob_vars(first); break;
2350 case 'b': list_buf_vars(first); break;
2351 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002352#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002353 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002354#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002355 case 'v': list_vim_vars(first); break;
2356 case 's': list_script_vars(first); break;
2357 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002358 default:
2359 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002360 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002361 }
2362 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002363 {
2364 char_u numbuf[NUMBUFLEN];
2365 char_u *tf;
2366 int c;
2367 char_u *s;
2368
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002369 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002370 c = *arg;
2371 *arg = NUL;
2372 list_one_var_a((char_u *)"",
2373 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002374 tv.v_type,
2375 s == NULL ? (char_u *)"" : s,
2376 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002377 *arg = c;
2378 vim_free(tf);
2379 }
2380 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 }
2383 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002384
2385 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002387
2388 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390
2391 return arg;
2392}
2393
2394/*
2395 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2396 * Returns a pointer to the char just after the var name.
2397 * Returns NULL if there is an error.
2398 */
2399 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002400ex_let_one(
2401 char_u *arg, /* points to variable name */
2402 typval_T *tv, /* value to assign to variable */
2403 int copy, /* copy value from "tv" */
2404 char_u *endchars, /* valid chars after variable name or NULL */
2405 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406{
2407 int c1;
2408 char_u *name;
2409 char_u *p;
2410 char_u *arg_end = NULL;
2411 int len;
2412 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414
2415 /*
2416 * ":let $VAR = expr": Set environment variable.
2417 */
2418 if (*arg == '$')
2419 {
2420 /* Find the end of the name. */
2421 ++arg;
2422 name = arg;
2423 len = get_env_len(&arg);
2424 if (len == 0)
2425 EMSG2(_(e_invarg2), name - 1);
2426 else
2427 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 if (op != NULL && (*op == '+' || *op == '-'))
2429 EMSG2(_(e_letwrong), op);
2430 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002431 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002433 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
2435 c1 = name[len];
2436 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002437 p = get_tv_string_chk(tv);
2438 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 {
2440 int mustfree = FALSE;
2441 char_u *s = vim_getenv(name, &mustfree);
2442
2443 if (s != NULL)
2444 {
2445 p = tofree = concat_str(s, p);
2446 if (mustfree)
2447 vim_free(s);
2448 }
2449 }
2450 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 if (STRICMP(name, "HOME") == 0)
2454 init_homedir();
2455 else if (didset_vim && STRICMP(name, "VIM") == 0)
2456 didset_vim = FALSE;
2457 else if (didset_vimruntime
2458 && STRICMP(name, "VIMRUNTIME") == 0)
2459 didset_vimruntime = FALSE;
2460 arg_end = arg;
2461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 }
2465 }
2466 }
2467
2468 /*
2469 * ":let &option = expr": Set option value.
2470 * ":let &l:option = expr": Set local option value.
2471 * ":let &g:option = expr": Set global option value.
2472 */
2473 else if (*arg == '&')
2474 {
2475 /* Find the end of the name. */
2476 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 if (p == NULL || (endchars != NULL
2478 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 EMSG(_(e_letunexp));
2480 else
2481 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 long n;
2483 int opt_type;
2484 long numval;
2485 char_u *stringval = NULL;
2486 char_u *s;
2487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 c1 = *p;
2489 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002491 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 s = get_tv_string_chk(tv); /* != NULL if number or string */
2493 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 {
2495 opt_type = get_option_value(arg, &numval,
2496 &stringval, opt_flags);
2497 if ((opt_type == 1 && *op == '.')
2498 || (opt_type == 0 && *op != '.'))
2499 EMSG2(_(e_letwrong), op);
2500 else
2501 {
2502 if (opt_type == 1) /* number */
2503 {
2504 if (*op == '+')
2505 n = numval + n;
2506 else
2507 n = numval - n;
2508 }
2509 else if (opt_type == 0 && stringval != NULL) /* string */
2510 {
2511 s = concat_str(stringval, s);
2512 vim_free(stringval);
2513 stringval = s;
2514 }
2515 }
2516 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002517 if (s != NULL)
2518 {
2519 set_option_value(arg, n, s, opt_flags);
2520 arg_end = p;
2521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 }
2525 }
2526
2527 /*
2528 * ":let @r = expr": Set register contents.
2529 */
2530 else if (*arg == '@')
2531 {
2532 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 if (op != NULL && (*op == '+' || *op == '-'))
2534 EMSG2(_(e_letwrong), op);
2535 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002536 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 EMSG(_(e_letunexp));
2538 else
2539 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002540 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002541 char_u *s;
2542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 p = get_tv_string_chk(tv);
2544 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002546 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 if (s != NULL)
2548 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002549 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002550 vim_free(s);
2551 }
2552 }
2553 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002554 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002556 arg_end = arg + 1;
2557 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002558 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 }
2560 }
2561
2562 /*
2563 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002566 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002570 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2574 EMSG(_(e_letunexp));
2575 else
2576 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002577 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 arg_end = p;
2579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 }
2583
2584 else
2585 EMSG2(_(e_invarg2), arg);
2586
2587 return arg_end;
2588}
2589
2590/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2592 */
2593 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002594check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595{
2596 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2597 {
2598 EMSG2(_(e_readonlyvar), arg);
2599 return TRUE;
2600 }
2601 return FALSE;
2602}
2603
2604/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 * Get an lval: variable, Dict item or List item that can be assigned a value
2606 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2607 * "name.key", "name.key[expr]" etc.
2608 * Indexing only works if "name" is an existing List or Dictionary.
2609 * "name" points to the start of the name.
2610 * If "rettv" is not NULL it points to the value to be assigned.
2611 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2612 * wrong; must end in space or cmd separator.
2613 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002614 * flags:
2615 * GLV_QUIET: do not give error messages
2616 * GLV_NO_AUTOLOAD: do not use script autoloading
2617 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002619 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 */
2622 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002623get_lval(
2624 char_u *name,
2625 typval_T *rettv,
2626 lval_T *lp,
2627 int unlet,
2628 int skip,
2629 int flags, /* GLV_ values */
2630 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 char_u *expr_start, *expr_end;
2634 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 dictitem_T *v;
2636 typval_T var1;
2637 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002642 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002643 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647
2648 if (skip)
2649 {
2650 /* When skipping just find the end of the name. */
2651 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002652 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 }
2654
2655 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002656 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (expr_start != NULL)
2658 {
2659 /* Don't expand the name when we already know there is an error. */
2660 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2661 && *p != '[' && *p != '.')
2662 {
2663 EMSG(_(e_trailing));
2664 return NULL;
2665 }
2666
2667 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2668 if (lp->ll_exp_name == NULL)
2669 {
2670 /* Report an invalid expression in braces, unless the
2671 * expression evaluation has been cancelled due to an
2672 * aborting error, an interrupt, or an exception. */
2673 if (!aborting() && !quiet)
2674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002675 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 EMSG2(_(e_invarg2), name);
2677 return NULL;
2678 }
2679 }
2680 lp->ll_name = lp->ll_exp_name;
2681 }
2682 else
2683 lp->ll_name = name;
2684
2685 /* Without [idx] or .key we are done. */
2686 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2687 return p;
2688
2689 cc = *p;
2690 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002691 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (v == NULL && !quiet)
2693 EMSG2(_(e_undefvar), lp->ll_name);
2694 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 if (v == NULL)
2696 return NULL;
2697
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 /*
2699 * Loop until no more [idx] or .key is following.
2700 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002701 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2705 && !(lp->ll_tv->v_type == VAR_DICT
2706 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_("E689: Can only index a List or Dictionary"));
2710 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_("E708: [:] must come last"));
2716 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 len = -1;
2720 if (*p == '.')
2721 {
2722 key = p + 1;
2723 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2724 ;
2725 if (len == 0)
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 if (!quiet)
2728 EMSG(_(e_emptykey));
2729 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
2731 p = key + len;
2732 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 else
2734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (*p == ':')
2738 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 else
2740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 empty1 = FALSE;
2742 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 if (get_tv_string_chk(&var1) == NULL)
2745 {
2746 /* not a number or string */
2747 clear_tv(&var1);
2748 return NULL;
2749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751
2752 /* Optionally get the second index [ :expr]. */
2753 if (*p == ':')
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002758 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002759 if (!empty1)
2760 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002762 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (rettv != NULL && (rettv->v_type != VAR_LIST
2764 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
2767 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (!empty1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
2772 p = skipwhite(p + 1);
2773 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 else
2776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (!empty1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 if (get_tv_string_chk(&var2) == NULL)
2785 {
2786 /* not a number or string */
2787 if (!empty1)
2788 clear_tv(&var1);
2789 clear_tv(&var2);
2790 return NULL;
2791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002797
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (!quiet)
2801 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 if (!empty1)
2803 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808
2809 /* Skip to past ']'. */
2810 ++p;
2811 }
2812
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
2815 if (len == -1)
2816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002818 key = get_tv_string_chk(&var1); /* is number or string */
2819 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825 lp->ll_list = NULL;
2826 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002827 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002828
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002829 /* When assigning to a scope dictionary check that a function and
2830 * variable name is valid (only variable name unless it is l: or
2831 * g: dictionary). Disallow overwriting a builtin function. */
2832 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002834 int prevval;
2835 int wrong;
2836
2837 if (len != -1)
2838 {
2839 prevval = key[len];
2840 key[len] = NUL;
2841 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002842 else
2843 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002844 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2845 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002846 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002847 || !valid_varname(key);
2848 if (len != -1)
2849 key[len] = prevval;
2850 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002851 return NULL;
2852 }
2853
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002856 /* Can't add "v:" variable. */
2857 if (lp->ll_dict == &vimvardict)
2858 {
2859 EMSG2(_(e_illvar), name);
2860 return NULL;
2861 }
2862
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002863 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002867 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 if (len == -1)
2869 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 }
2872 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 if (len == -1)
2877 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 p = NULL;
2880 break;
2881 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002882 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002883 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002884 return NULL;
2885
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 if (len == -1)
2887 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 }
2890 else
2891 {
2892 /*
2893 * Get the number and item for the only or first index of the List.
2894 */
2895 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002897 else
2898 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002899 lp->ll_n1 = (long)get_tv_number(&var1);
2900 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 clear_tv(&var1);
2902 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 lp->ll_list = lp->ll_tv->vval.v_list;
2905 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2906 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002908 if (lp->ll_n1 < 0)
2909 {
2910 lp->ll_n1 = 0;
2911 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2912 }
2913 }
2914 if (lp->ll_li == NULL)
2915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002918 if (!quiet)
2919 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 }
2922
2923 /*
2924 * May need to find the item or absolute index for the second
2925 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 * When no index given: "lp->ll_empty2" is TRUE.
2927 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002928 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002930 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002931 lp->ll_n2 = (long)get_tv_number(&var2);
2932 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002933 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002937 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002938 {
2939 if (!quiet)
2940 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002944 }
2945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2947 if (lp->ll_n1 < 0)
2948 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2949 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002950 {
2951 if (!quiet)
2952 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002955 }
2956
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002958 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 }
2960
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 return p;
2962}
2963
2964/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002965 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 */
2967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002968clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969{
2970 vim_free(lp->ll_exp_name);
2971 vim_free(lp->ll_newkey);
2972}
2973
2974/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 */
2979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002980set_var_lval(
2981 lval_T *lp,
2982 char_u *endp,
2983 typval_T *rettv,
2984 int copy,
2985 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986{
2987 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 listitem_T *ri;
2989 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990
2991 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 cc = *endp;
2996 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 if (op != NULL && *op != '=')
2998 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003001 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003002 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003003 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003004 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003006 if ((di == NULL
3007 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
3008 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
3009 FALSE)))
3010 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 set_var(lp->ll_name, &tv, FALSE);
3012 clear_tv(&tv);
3013 }
3014 }
3015 else
3016 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003018 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003020 else if (tv_check_lock(lp->ll_newkey == NULL
3021 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003022 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003023 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 else if (lp->ll_range)
3025 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003026 listitem_T *ll_li = lp->ll_li;
3027 int ll_n1 = lp->ll_n1;
3028
3029 /*
3030 * Check whether any of the list items is locked
3031 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003032 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003033 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003034 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003035 return;
3036 ri = ri->li_next;
3037 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3038 break;
3039 ll_li = ll_li->li_next;
3040 ++ll_n1;
3041 }
3042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 /*
3044 * Assign the List values to the list items.
3045 */
3046 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 if (op != NULL && *op != '=')
3049 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3050 else
3051 {
3052 clear_tv(&lp->ll_li->li_tv);
3053 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 ri = ri->li_next;
3056 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3057 break;
3058 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003059 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003061 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003062 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 ri = NULL;
3064 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003065 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003066 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 lp->ll_li = lp->ll_li->li_next;
3068 ++lp->ll_n1;
3069 }
3070 if (ri != NULL)
3071 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 else if (lp->ll_empty2
3073 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 : lp->ll_n1 != lp->ll_n2)
3075 EMSG(_("E711: List value has not enough items"));
3076 }
3077 else
3078 {
3079 /*
3080 * Assign to a List or Dictionary item.
3081 */
3082 if (lp->ll_newkey != NULL)
3083 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 if (op != NULL && *op != '=')
3085 {
3086 EMSG2(_(e_letwrong), op);
3087 return;
3088 }
3089
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003090 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003091 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 if (di == NULL)
3093 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003094 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3095 {
3096 vim_free(di);
3097 return;
3098 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003099 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003100 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003101 else if (op != NULL && *op != '=')
3102 {
3103 tv_op(lp->ll_tv, rettv, op);
3104 return;
3105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003107 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003108
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003109 /*
3110 * Assign the value to the variable or list item.
3111 */
3112 if (copy)
3113 copy_tv(rettv, lp->ll_tv);
3114 else
3115 {
3116 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003117 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003118 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 }
3120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003121}
3122
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3125 * Returns OK or FAIL.
3126 */
3127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003128tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003130 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 char_u numbuf[NUMBUFLEN];
3132 char_u *s;
3133
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003134 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3135 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3136 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 {
3138 switch (tv1->v_type)
3139 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003140 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 case VAR_DICT:
3142 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003143 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003144 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003145 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003146 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 break;
3148
3149 case VAR_LIST:
3150 if (*op != '+' || tv2->v_type != VAR_LIST)
3151 break;
3152 /* List += List */
3153 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3154 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3155 return OK;
3156
3157 case VAR_NUMBER:
3158 case VAR_STRING:
3159 if (tv2->v_type == VAR_LIST)
3160 break;
3161 if (*op == '+' || *op == '-')
3162 {
3163 /* nr += nr or nr -= nr*/
3164 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165#ifdef FEAT_FLOAT
3166 if (tv2->v_type == VAR_FLOAT)
3167 {
3168 float_T f = n;
3169
3170 if (*op == '+')
3171 f += tv2->vval.v_float;
3172 else
3173 f -= tv2->vval.v_float;
3174 clear_tv(tv1);
3175 tv1->v_type = VAR_FLOAT;
3176 tv1->vval.v_float = f;
3177 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003178 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179#endif
3180 {
3181 if (*op == '+')
3182 n += get_tv_number(tv2);
3183 else
3184 n -= get_tv_number(tv2);
3185 clear_tv(tv1);
3186 tv1->v_type = VAR_NUMBER;
3187 tv1->vval.v_number = n;
3188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003189 }
3190 else
3191 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192 if (tv2->v_type == VAR_FLOAT)
3193 break;
3194
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003195 /* str .= str */
3196 s = get_tv_string(tv1);
3197 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3198 clear_tv(tv1);
3199 tv1->v_type = VAR_STRING;
3200 tv1->vval.v_string = s;
3201 }
3202 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003205#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206 {
3207 float_T f;
3208
3209 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3210 && tv2->v_type != VAR_NUMBER
3211 && tv2->v_type != VAR_STRING))
3212 break;
3213 if (tv2->v_type == VAR_FLOAT)
3214 f = tv2->vval.v_float;
3215 else
3216 f = get_tv_number(tv2);
3217 if (*op == '+')
3218 tv1->vval.v_float += f;
3219 else
3220 tv1->vval.v_float -= f;
3221 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003222#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003223 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003224 }
3225 }
3226
3227 EMSG2(_(e_letwrong), op);
3228 return FAIL;
3229}
3230
3231/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 * Add a watcher to a list.
3233 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236{
3237 lw->lw_next = l->lv_watch;
3238 l->lv_watch = lw;
3239}
3240
3241/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003242 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 * No warning when it isn't found...
3244 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003245 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003246list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247{
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249
3250 lwp = &l->lv_watch;
3251 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3252 {
3253 if (lw == lwrem)
3254 {
3255 *lwp = lw->lw_next;
3256 break;
3257 }
3258 lwp = &lw->lw_next;
3259 }
3260}
3261
3262/*
3263 * Just before removing an item from a list: advance watchers to the next
3264 * item.
3265 */
3266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003267list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268{
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270
3271 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3272 if (lw->lw_item == item)
3273 lw->lw_item = item->li_next;
3274}
3275
3276/*
3277 * Evaluate the expression used in a ":for var in expr" command.
3278 * "arg" points to "var".
3279 * Set "*errp" to TRUE for an error, FALSE otherwise;
3280 * Return a pointer that holds the info. Null when there is an error.
3281 */
3282 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003283eval_for_line(
3284 char_u *arg,
3285 int *errp,
3286 char_u **nextcmdp,
3287 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288{
Bram Moolenaar33570922005-01-25 22:26:29 +00003289 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 typval_T tv;
3292 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293
3294 *errp = TRUE; /* default: there is an error */
3295
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (fi == NULL)
3298 return NULL;
3299
3300 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3301 if (expr == NULL)
3302 return fi;
3303
3304 expr = skipwhite(expr);
3305 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3306 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003307 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 return fi;
3309 }
3310
3311 if (skip)
3312 ++emsg_skip;
3313 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3314 {
3315 *errp = FALSE;
3316 if (!skip)
3317 {
3318 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003319 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 clear_tv(&tv);
3323 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003324 else if (l == NULL)
3325 {
3326 /* a null list is like an empty list: do nothing */
3327 clear_tv(&tv);
3328 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 else
3330 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003331 /* No need to increment the refcount, it's already set for the
3332 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 fi->fi_list = l;
3334 list_add_watch(l, &fi->fi_lw);
3335 fi->fi_lw.lw_item = l->lv_first;
3336 }
3337 }
3338 }
3339 if (skip)
3340 --emsg_skip;
3341
3342 return fi;
3343}
3344
3345/*
3346 * Use the first item in a ":for" list. Advance to the next.
3347 * Assign the values to the variable (list). "arg" points to the first one.
3348 * Return TRUE when a valid item was found, FALSE when at end of list or
3349 * something wrong.
3350 */
3351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003352next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003354 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003356 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003357
3358 item = fi->fi_lw.lw_item;
3359 if (item == NULL)
3360 result = FALSE;
3361 else
3362 {
3363 fi->fi_lw.lw_item = item->li_next;
3364 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3365 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3366 }
3367 return result;
3368}
3369
3370/*
3371 * Free the structure used to store info used by ":for".
3372 */
3373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375{
Bram Moolenaar33570922005-01-25 22:26:29 +00003376 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003377
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003378 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003379 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003380 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003381 list_unref(fi->fi_list);
3382 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 vim_free(fi);
3384}
3385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3387
3388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003389set_context_for_expression(
3390 expand_T *xp,
3391 char_u *arg,
3392 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 int got_eq = FALSE;
3395 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003396 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003398 if (cmdidx == CMD_let)
3399 {
3400 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003401 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003402 {
3403 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003404 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003405 {
3406 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003407 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003408 if (vim_iswhite(*p))
3409 break;
3410 }
3411 return;
3412 }
3413 }
3414 else
3415 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3416 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 while ((xp->xp_pattern = vim_strpbrk(arg,
3418 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3419 {
3420 c = *xp->xp_pattern;
3421 if (c == '&')
3422 {
3423 c = xp->xp_pattern[1];
3424 if (c == '&')
3425 {
3426 ++xp->xp_pattern;
3427 xp->xp_context = cmdidx != CMD_let || got_eq
3428 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3429 }
3430 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003433 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3434 xp->xp_pattern += 2;
3435
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 else if (c == '$')
3439 {
3440 /* environment variable */
3441 xp->xp_context = EXPAND_ENV_VARS;
3442 }
3443 else if (c == '=')
3444 {
3445 got_eq = TRUE;
3446 xp->xp_context = EXPAND_EXPRESSION;
3447 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003448 else if (c == '#'
3449 && xp->xp_context == EXPAND_EXPRESSION)
3450 {
3451 /* Autoload function/variable contains '#'. */
3452 break;
3453 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003454 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 && xp->xp_context == EXPAND_FUNCTIONS
3456 && vim_strchr(xp->xp_pattern, '(') == NULL)
3457 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003458 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 break;
3460 }
3461 else if (cmdidx != CMD_let || got_eq)
3462 {
3463 if (c == '"') /* string */
3464 {
3465 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3466 if (c == '\\' && xp->xp_pattern[1] != NUL)
3467 ++xp->xp_pattern;
3468 xp->xp_context = EXPAND_NOTHING;
3469 }
3470 else if (c == '\'') /* literal string */
3471 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003472 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3474 /* skip */ ;
3475 xp->xp_context = EXPAND_NOTHING;
3476 }
3477 else if (c == '|')
3478 {
3479 if (xp->xp_pattern[1] == '|')
3480 {
3481 ++xp->xp_pattern;
3482 xp->xp_context = EXPAND_EXPRESSION;
3483 }
3484 else
3485 xp->xp_context = EXPAND_COMMANDS;
3486 }
3487 else
3488 xp->xp_context = EXPAND_EXPRESSION;
3489 }
3490 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003491 /* Doesn't look like something valid, expand as an expression
3492 * anyway. */
3493 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 arg = xp->xp_pattern;
3495 if (*arg != NUL)
3496 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3497 /* skip */ ;
3498 }
3499 xp->xp_pattern = arg;
3500}
3501
3502#endif /* FEAT_CMDL_COMPL */
3503
3504/*
3505 * ":1,25call func(arg1, arg2)" function call.
3506 */
3507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003508ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
3510 char_u *arg = eap->arg;
3511 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003513 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 linenr_T lnum;
3517 int doesrange;
3518 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003520 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003522 if (eap->skip)
3523 {
3524 /* trans_function_name() doesn't work well when skipping, use eval0()
3525 * instead to skip to any following command, e.g. for:
3526 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003527 ++emsg_skip;
3528 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3529 clear_tv(&rettv);
3530 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003531 return;
3532 }
3533
Bram Moolenaar65639032016-03-16 21:40:30 +01003534 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003535 if (fudi.fd_newkey != NULL)
3536 {
3537 /* Still need to give an error message for missing key. */
3538 EMSG2(_(e_dictkey), fudi.fd_newkey);
3539 vim_free(fudi.fd_newkey);
3540 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003541 if (tofree == NULL)
3542 return;
3543
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003544 /* Increase refcount on dictionary, it could get deleted when evaluating
3545 * the arguments. */
3546 if (fudi.fd_dict != NULL)
3547 ++fudi.fd_dict->dv_refcount;
3548
Bram Moolenaar65639032016-03-16 21:40:30 +01003549 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3550 * contents. For VAR_PARTIAL get its partial, unless we already have one
3551 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003552 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003553 name = deref_func_name(tofree, &len,
3554 partial != NULL ? NULL : &partial, FALSE);
3555
Bram Moolenaar532c7802005-01-27 14:44:31 +00003556 /* Skip white space to allow ":call func ()". Not good, but required for
3557 * backward compatibility. */
3558 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003559 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 if (*startarg != '(')
3562 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003563 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 goto end;
3565 }
3566
3567 /*
3568 * When skipping, evaluate the function once, to find the end of the
3569 * arguments.
3570 * When the function takes a range, this is discovered after the first
3571 * call, and the loop is broken.
3572 */
3573 if (eap->skip)
3574 {
3575 ++emsg_skip;
3576 lnum = eap->line2; /* do it once, also with an invalid range */
3577 }
3578 else
3579 lnum = eap->line1;
3580 for ( ; lnum <= eap->line2; ++lnum)
3581 {
3582 if (!eap->skip && eap->addr_count > 0)
3583 {
3584 curwin->w_cursor.lnum = lnum;
3585 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003586#ifdef FEAT_VIRTUALEDIT
3587 curwin->w_cursor.coladd = 0;
3588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003591 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003592 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003593 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 failed = TRUE;
3596 break;
3597 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003598
3599 /* Handle a function returning a Funcref, Dictionary or List. */
3600 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3601 {
3602 failed = TRUE;
3603 break;
3604 }
3605
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (doesrange || eap->skip)
3608 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003611 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003612 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003613 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (aborting())
3615 break;
3616 }
3617 if (eap->skip)
3618 --emsg_skip;
3619
3620 if (!failed)
3621 {
3622 /* Check for trailing illegal characters and a following command. */
3623 if (!ends_excmd(*arg))
3624 {
3625 emsg_severe = TRUE;
3626 EMSG(_(e_trailing));
3627 }
3628 else
3629 eap->nextcmd = check_nextcmd(arg);
3630 }
3631
3632end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003633 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003634 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635}
3636
3637/*
3638 * ":unlet[!] var1 ... " command.
3639 */
3640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 ex_unletlock(eap, eap->arg, 0);
3644}
3645
3646/*
3647 * ":lockvar" and ":unlockvar" commands
3648 */
3649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003650ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653 int deep = 2;
3654
3655 if (eap->forceit)
3656 deep = -1;
3657 else if (vim_isdigit(*arg))
3658 {
3659 deep = getdigits(&arg);
3660 arg = skipwhite(arg);
3661 }
3662
3663 ex_unletlock(eap, arg, deep);
3664}
3665
3666/*
3667 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3668 */
3669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003670ex_unletlock(
3671 exarg_T *eap,
3672 char_u *argstart,
3673 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674{
3675 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 do
3681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003683 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003684 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 if (lv.ll_name == NULL)
3686 error = TRUE; /* error but continue parsing */
3687 if (name_end == NULL || (!vim_iswhite(*name_end)
3688 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 if (name_end != NULL)
3691 {
3692 emsg_severe = TRUE;
3693 EMSG(_(e_trailing));
3694 }
3695 if (!(eap->skip || error))
3696 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 break;
3698 }
3699
3700 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003701 {
3702 if (eap->cmdidx == CMD_unlet)
3703 {
3704 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3705 error = TRUE;
3706 }
3707 else
3708 {
3709 if (do_lock_var(&lv, name_end, deep,
3710 eap->cmdidx == CMD_lockvar) == FAIL)
3711 error = TRUE;
3712 }
3713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 if (!eap->skip)
3716 clear_lval(&lv);
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 arg = skipwhite(name_end);
3719 } while (!ends_excmd(*arg));
3720
3721 eap->nextcmd = check_nextcmd(arg);
3722}
3723
Bram Moolenaar8c711452005-01-14 21:53:12 +00003724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003725do_unlet_var(
3726 lval_T *lp,
3727 char_u *name_end,
3728 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003730 int ret = OK;
3731 int cc;
3732
3733 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 cc = *name_end;
3736 *name_end = NUL;
3737
3738 /* Normal name or expanded name. */
3739 if (check_changedtick(lp->ll_name))
3740 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003741 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003742 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003743 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003744 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003745 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003746 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003747 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003748 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750 else if (lp->ll_range)
3751 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003752 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003753 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003754 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003755
3756 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3757 {
3758 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003759 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003760 return FAIL;
3761 ll_li = li;
3762 ++ll_n1;
3763 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764
3765 /* Delete a range of List items. */
3766 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3767 {
3768 li = lp->ll_li->li_next;
3769 listitem_remove(lp->ll_list, lp->ll_li);
3770 lp->ll_li = li;
3771 ++lp->ll_n1;
3772 }
3773 }
3774 else
3775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003776 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003777 /* unlet a List item. */
3778 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003779 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003780 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003781 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003782 }
3783
3784 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003785}
3786
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787/*
3788 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003789 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 */
3791 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003792do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
Bram Moolenaar33570922005-01-25 22:26:29 +00003794 hashtab_T *ht;
3795 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003796 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003797 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003798 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
Bram Moolenaar33570922005-01-25 22:26:29 +00003800 ht = find_var_ht(name, &varname);
3801 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003803 if (ht == &globvarht)
3804 d = &globvardict;
3805 else if (current_funccal != NULL
3806 && ht == &current_funccal->l_vars.dv_hashtab)
3807 d = &current_funccal->l_vars;
3808 else if (ht == &compat_hashtab)
3809 d = &vimvardict;
3810 else
3811 {
3812 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3813 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3814 }
3815 if (d == NULL)
3816 {
3817 EMSG2(_(e_intern2), "do_unlet()");
3818 return FAIL;
3819 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003820 hi = hash_find(ht, varname);
3821 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003822 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003823 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003824 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003825 || var_check_ro(di->di_flags, name, FALSE)
3826 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003827 return FAIL;
3828
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003829 delete_var(ht, hi);
3830 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003833 if (forceit)
3834 return OK;
3835 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return FAIL;
3837}
3838
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003839/*
3840 * Lock or unlock variable indicated by "lp".
3841 * "deep" is the levels to go (-1 for unlimited);
3842 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3843 */
3844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845do_lock_var(
3846 lval_T *lp,
3847 char_u *name_end,
3848 int deep,
3849 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003850{
3851 int ret = OK;
3852 int cc;
3853 dictitem_T *di;
3854
3855 if (deep == 0) /* nothing to do */
3856 return OK;
3857
3858 if (lp->ll_tv == NULL)
3859 {
3860 cc = *name_end;
3861 *name_end = NUL;
3862
3863 /* Normal name or expanded name. */
3864 if (check_changedtick(lp->ll_name))
3865 ret = FAIL;
3866 else
3867 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003868 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003869 if (di == NULL)
3870 ret = FAIL;
3871 else
3872 {
3873 if (lock)
3874 di->di_flags |= DI_FLAGS_LOCK;
3875 else
3876 di->di_flags &= ~DI_FLAGS_LOCK;
3877 item_lock(&di->di_tv, deep, lock);
3878 }
3879 }
3880 *name_end = cc;
3881 }
3882 else if (lp->ll_range)
3883 {
3884 listitem_T *li = lp->ll_li;
3885
3886 /* (un)lock a range of List items. */
3887 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3888 {
3889 item_lock(&li->li_tv, deep, lock);
3890 li = li->li_next;
3891 ++lp->ll_n1;
3892 }
3893 }
3894 else if (lp->ll_list != NULL)
3895 /* (un)lock a List item. */
3896 item_lock(&lp->ll_li->li_tv, deep, lock);
3897 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003898 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003899 item_lock(&lp->ll_di->di_tv, deep, lock);
3900
3901 return ret;
3902}
3903
3904/*
3905 * Lock or unlock an item. "deep" is nr of levels to go.
3906 */
3907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003908item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003909{
3910 static int recurse = 0;
3911 list_T *l;
3912 listitem_T *li;
3913 dict_T *d;
3914 hashitem_T *hi;
3915 int todo;
3916
3917 if (recurse >= DICT_MAXNEST)
3918 {
3919 EMSG(_("E743: variable nested too deep for (un)lock"));
3920 return;
3921 }
3922 if (deep == 0)
3923 return;
3924 ++recurse;
3925
3926 /* lock/unlock the item itself */
3927 if (lock)
3928 tv->v_lock |= VAR_LOCKED;
3929 else
3930 tv->v_lock &= ~VAR_LOCKED;
3931
3932 switch (tv->v_type)
3933 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003934 case VAR_UNKNOWN:
3935 case VAR_NUMBER:
3936 case VAR_STRING:
3937 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003938 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003939 case VAR_FLOAT:
3940 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003941 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003942 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003943 break;
3944
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003945 case VAR_LIST:
3946 if ((l = tv->vval.v_list) != NULL)
3947 {
3948 if (lock)
3949 l->lv_lock |= VAR_LOCKED;
3950 else
3951 l->lv_lock &= ~VAR_LOCKED;
3952 if (deep < 0 || deep > 1)
3953 /* recursive: lock/unlock the items the List contains */
3954 for (li = l->lv_first; li != NULL; li = li->li_next)
3955 item_lock(&li->li_tv, deep - 1, lock);
3956 }
3957 break;
3958 case VAR_DICT:
3959 if ((d = tv->vval.v_dict) != NULL)
3960 {
3961 if (lock)
3962 d->dv_lock |= VAR_LOCKED;
3963 else
3964 d->dv_lock &= ~VAR_LOCKED;
3965 if (deep < 0 || deep > 1)
3966 {
3967 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003968 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003969 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3970 {
3971 if (!HASHITEM_EMPTY(hi))
3972 {
3973 --todo;
3974 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3975 }
3976 }
3977 }
3978 }
3979 }
3980 --recurse;
3981}
3982
Bram Moolenaara40058a2005-07-11 22:42:07 +00003983/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003984 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3985 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003986 */
3987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003988tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003989{
3990 return (tv->v_lock & VAR_LOCKED)
3991 || (tv->v_type == VAR_LIST
3992 && tv->vval.v_list != NULL
3993 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3994 || (tv->v_type == VAR_DICT
3995 && tv->vval.v_dict != NULL
3996 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3997}
3998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
4000/*
4001 * Delete all "menutrans_" variables.
4002 */
4003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004007 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004010 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004011 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 {
4013 if (!HASHITEM_EMPTY(hi))
4014 {
4015 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004016 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4017 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 }
4019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021}
4022#endif
4023
4024#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4025
4026/*
4027 * Local string buffer for the next two functions to store a variable name
4028 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4029 * get_user_var_name().
4030 */
4031
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004032static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
4034static char_u *varnamebuf = NULL;
4035static int varnamebuflen = 0;
4036
4037/*
4038 * Function to concatenate a prefix and a variable name.
4039 */
4040 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004041cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042{
4043 int len;
4044
4045 len = (int)STRLEN(name) + 3;
4046 if (len > varnamebuflen)
4047 {
4048 vim_free(varnamebuf);
4049 len += 10; /* some additional space */
4050 varnamebuf = alloc(len);
4051 if (varnamebuf == NULL)
4052 {
4053 varnamebuflen = 0;
4054 return NULL;
4055 }
4056 varnamebuflen = len;
4057 }
4058 *varnamebuf = prefix;
4059 varnamebuf[1] = ':';
4060 STRCPY(varnamebuf + 2, name);
4061 return varnamebuf;
4062}
4063
4064/*
4065 * Function given to ExpandGeneric() to obtain the list of user defined
4066 * (global/buffer/window/built-in) variable names.
4067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004069get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004071 static long_u gdone;
4072 static long_u bdone;
4073 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004074#ifdef FEAT_WINDOWS
4075 static long_u tdone;
4076#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004077 static int vidx;
4078 static hashitem_T *hi;
4079 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004082 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004083 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084#ifdef FEAT_WINDOWS
4085 tdone = 0;
4086#endif
4087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004088
4089 /* Global variables */
4090 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004092 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004094 else
4095 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 while (HASHITEM_EMPTY(hi))
4097 ++hi;
4098 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4099 return cat_prefix_varname('g', hi->hi_key);
4100 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004102
4103 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004104 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004105 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004107 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004109 else
4110 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004111 while (HASHITEM_EMPTY(hi))
4112 ++hi;
4113 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004115 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004117 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 return (char_u *)"b:changedtick";
4119 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004120
4121 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004122 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004123 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004125 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004126 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004127 else
4128 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004129 while (HASHITEM_EMPTY(hi))
4130 ++hi;
4131 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004133
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004134#ifdef FEAT_WINDOWS
4135 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004136 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004137 if (tdone < ht->ht_used)
4138 {
4139 if (tdone++ == 0)
4140 hi = ht->ht_array;
4141 else
4142 ++hi;
4143 while (HASHITEM_EMPTY(hi))
4144 ++hi;
4145 return cat_prefix_varname('t', hi->hi_key);
4146 }
4147#endif
4148
Bram Moolenaar33570922005-01-25 22:26:29 +00004149 /* v: variables */
4150 if (vidx < VV_LEN)
4151 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 vim_free(varnamebuf);
4154 varnamebuf = NULL;
4155 varnamebuflen = 0;
4156 return NULL;
4157}
4158
4159#endif /* FEAT_CMDL_COMPL */
4160
4161/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004162 * Return TRUE if "pat" matches "text".
4163 * Does not use 'cpo' and always uses 'magic'.
4164 */
4165 static int
4166pattern_match(char_u *pat, char_u *text, int ic)
4167{
4168 int matches = FALSE;
4169 char_u *save_cpo;
4170 regmatch_T regmatch;
4171
4172 /* avoid 'l' flag in 'cpoptions' */
4173 save_cpo = p_cpo;
4174 p_cpo = (char_u *)"";
4175 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4176 if (regmatch.regprog != NULL)
4177 {
4178 regmatch.rm_ic = ic;
4179 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4180 vim_regfree(regmatch.regprog);
4181 }
4182 p_cpo = save_cpo;
4183 return matches;
4184}
4185
4186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 * types for expressions.
4188 */
4189typedef enum
4190{
4191 TYPE_UNKNOWN = 0
4192 , TYPE_EQUAL /* == */
4193 , TYPE_NEQUAL /* != */
4194 , TYPE_GREATER /* > */
4195 , TYPE_GEQUAL /* >= */
4196 , TYPE_SMALLER /* < */
4197 , TYPE_SEQUAL /* <= */
4198 , TYPE_MATCH /* =~ */
4199 , TYPE_NOMATCH /* !~ */
4200} exptype_T;
4201
4202/*
4203 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4206 */
4207
4208/*
4209 * Handle zero level expression.
4210 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004212 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 * Return OK or FAIL.
4214 */
4215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004216eval0(
4217 char_u *arg,
4218 typval_T *rettv,
4219 char_u **nextcmd,
4220 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
4222 int ret;
4223 char_u *p;
4224
4225 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (ret == FAIL || !ends_excmd(*p))
4228 {
4229 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 /*
4232 * Report the invalid expression unless the expression evaluation has
4233 * been cancelled due to an aborting error, an interrupt, or an
4234 * exception.
4235 */
4236 if (!aborting())
4237 EMSG2(_(e_invexpr2), arg);
4238 ret = FAIL;
4239 }
4240 if (nextcmd != NULL)
4241 *nextcmd = check_nextcmd(p);
4242
4243 return ret;
4244}
4245
4246/*
4247 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004248 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 *
4250 * "arg" must point to the first non-white of the expression.
4251 * "arg" is advanced to the next non-white after the recognized expression.
4252 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004253 * Note: "rettv.v_lock" is not set.
4254 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 * Return OK or FAIL.
4256 */
4257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004258eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
4260 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004261 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 /*
4264 * Get the first variable.
4265 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 return FAIL;
4268
4269 if ((*arg)[0] == '?')
4270 {
4271 result = FALSE;
4272 if (evaluate)
4273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 int error = FALSE;
4275
4276 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (error)
4280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
4283 /*
4284 * Get the second variable.
4285 */
4286 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289
4290 /*
4291 * Check for the ":".
4292 */
4293 if ((*arg)[0] != ':')
4294 {
4295 EMSG(_("E109: Missing ':' after '?'"));
4296 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return FAIL;
4299 }
4300
4301 /*
4302 * Get the third variable.
4303 */
4304 *arg = skipwhite(*arg + 1);
4305 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4306 {
4307 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FAIL;
4310 }
4311 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314
4315 return OK;
4316}
4317
4318/*
4319 * Handle first level expression:
4320 * expr2 || expr2 || expr2 logical OR
4321 *
4322 * "arg" must point to the first non-white of the expression.
4323 * "arg" is advanced to the next non-white after the recognized expression.
4324 *
4325 * Return OK or FAIL.
4326 */
4327 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004328eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
Bram Moolenaar33570922005-01-25 22:26:29 +00004330 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 long result;
4332 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 /*
4336 * Get the first variable.
4337 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 return FAIL;
4340
4341 /*
4342 * Repeat until there is no following "||".
4343 */
4344 first = TRUE;
4345 result = FALSE;
4346 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4347 {
4348 if (evaluate && first)
4349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004352 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 if (error)
4354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 first = FALSE;
4356 }
4357
4358 /*
4359 * Get the second variable.
4360 */
4361 *arg = skipwhite(*arg + 2);
4362 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4363 return FAIL;
4364
4365 /*
4366 * Compute the result.
4367 */
4368 if (evaluate && !result)
4369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004370 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004373 if (error)
4374 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 if (evaluate)
4377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 rettv->v_type = VAR_NUMBER;
4379 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381 }
4382
4383 return OK;
4384}
4385
4386/*
4387 * Handle second level expression:
4388 * expr3 && expr3 && expr3 logical AND
4389 *
4390 * "arg" must point to the first non-white of the expression.
4391 * "arg" is advanced to the next non-white after the recognized expression.
4392 *
4393 * Return OK or FAIL.
4394 */
4395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004396eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 long result;
4400 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 /*
4404 * Get the first variable.
4405 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 return FAIL;
4408
4409 /*
4410 * Repeat until there is no following "&&".
4411 */
4412 first = TRUE;
4413 result = TRUE;
4414 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4415 {
4416 if (evaluate && first)
4417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004418 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 if (error)
4422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 first = FALSE;
4424 }
4425
4426 /*
4427 * Get the second variable.
4428 */
4429 *arg = skipwhite(*arg + 2);
4430 if (eval4(arg, &var2, evaluate && result) == FAIL)
4431 return FAIL;
4432
4433 /*
4434 * Compute the result.
4435 */
4436 if (evaluate && result)
4437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004438 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 if (error)
4442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 if (evaluate)
4445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 rettv->v_type = VAR_NUMBER;
4447 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 }
4450
4451 return OK;
4452}
4453
4454/*
4455 * Handle third level expression:
4456 * var1 == var2
4457 * var1 =~ var2
4458 * var1 != var2
4459 * var1 !~ var2
4460 * var1 > var2
4461 * var1 >= var2
4462 * var1 < var2
4463 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 * var1 is var2
4465 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 *
4467 * "arg" must point to the first non-white of the expression.
4468 * "arg" is advanced to the next non-white after the recognized expression.
4469 *
4470 * Return OK or FAIL.
4471 */
4472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004473eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
Bram Moolenaar33570922005-01-25 22:26:29 +00004475 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 char_u *p;
4477 int i;
4478 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004481 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 char_u *s1, *s2;
4483 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 /*
4487 * Get the first variable.
4488 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004489 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 return FAIL;
4491
4492 p = *arg;
4493 switch (p[0])
4494 {
4495 case '=': if (p[1] == '=')
4496 type = TYPE_EQUAL;
4497 else if (p[1] == '~')
4498 type = TYPE_MATCH;
4499 break;
4500 case '!': if (p[1] == '=')
4501 type = TYPE_NEQUAL;
4502 else if (p[1] == '~')
4503 type = TYPE_NOMATCH;
4504 break;
4505 case '>': if (p[1] != '=')
4506 {
4507 type = TYPE_GREATER;
4508 len = 1;
4509 }
4510 else
4511 type = TYPE_GEQUAL;
4512 break;
4513 case '<': if (p[1] != '=')
4514 {
4515 type = TYPE_SMALLER;
4516 len = 1;
4517 }
4518 else
4519 type = TYPE_SEQUAL;
4520 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004521 case 'i': if (p[1] == 's')
4522 {
4523 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4524 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004525 i = p[len];
4526 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004527 {
4528 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4529 type_is = TRUE;
4530 }
4531 }
4532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534
4535 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004536 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 */
4538 if (type != TYPE_UNKNOWN)
4539 {
4540 /* extra question mark appended: ignore case */
4541 if (p[len] == '?')
4542 {
4543 ic = TRUE;
4544 ++len;
4545 }
4546 /* extra '#' appended: match case */
4547 else if (p[len] == '#')
4548 {
4549 ic = FALSE;
4550 ++len;
4551 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004552 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else
4554 ic = p_ic;
4555
4556 /*
4557 * Get the second variable.
4558 */
4559 *arg = skipwhite(p + len);
4560 if (eval5(arg, &var2, evaluate) == FAIL)
4561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 return FAIL;
4564 }
4565
4566 if (evaluate)
4567 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 if (type_is && rettv->v_type != var2.v_type)
4569 {
4570 /* For "is" a different type always means FALSE, for "notis"
4571 * it means TRUE. */
4572 n1 = (type == TYPE_NEQUAL);
4573 }
4574 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4575 {
4576 if (type_is)
4577 {
4578 n1 = (rettv->v_type == var2.v_type
4579 && rettv->vval.v_list == var2.vval.v_list);
4580 if (type == TYPE_NEQUAL)
4581 n1 = !n1;
4582 }
4583 else if (rettv->v_type != var2.v_type
4584 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4585 {
4586 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004587 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004588 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004589 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004590 clear_tv(rettv);
4591 clear_tv(&var2);
4592 return FAIL;
4593 }
4594 else
4595 {
4596 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004597 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4598 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004599 if (type == TYPE_NEQUAL)
4600 n1 = !n1;
4601 }
4602 }
4603
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004604 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4605 {
4606 if (type_is)
4607 {
4608 n1 = (rettv->v_type == var2.v_type
4609 && rettv->vval.v_dict == var2.vval.v_dict);
4610 if (type == TYPE_NEQUAL)
4611 n1 = !n1;
4612 }
4613 else if (rettv->v_type != var2.v_type
4614 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4615 {
4616 if (rettv->v_type != var2.v_type)
4617 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4618 else
4619 EMSG(_("E736: Invalid operation for Dictionary"));
4620 clear_tv(rettv);
4621 clear_tv(&var2);
4622 return FAIL;
4623 }
4624 else
4625 {
4626 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004627 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4628 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004629 if (type == TYPE_NEQUAL)
4630 n1 = !n1;
4631 }
4632 }
4633
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004634 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4635 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004636 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004637 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004638 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004639 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004640 clear_tv(rettv);
4641 clear_tv(&var2);
4642 return FAIL;
4643 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004644 if ((rettv->v_type == VAR_PARTIAL
4645 && rettv->vval.v_partial == NULL)
4646 || (var2.v_type == VAR_PARTIAL
4647 && var2.vval.v_partial == NULL))
4648 /* when a partial is NULL assume not equal */
4649 n1 = FALSE;
4650 else if (type_is)
4651 {
4652 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4653 /* strings are considered the same if their value is
4654 * the same */
4655 n1 = tv_equal(rettv, &var2, ic, FALSE);
4656 else if (rettv->v_type == VAR_PARTIAL
4657 && var2.v_type == VAR_PARTIAL)
4658 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4659 else
4660 n1 = FALSE;
4661 }
4662 else
4663 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004664 if (type == TYPE_NEQUAL)
4665 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004666 }
4667
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004668#ifdef FEAT_FLOAT
4669 /*
4670 * If one of the two variables is a float, compare as a float.
4671 * When using "=~" or "!~", always compare as string.
4672 */
4673 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4674 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4675 {
4676 float_T f1, f2;
4677
4678 if (rettv->v_type == VAR_FLOAT)
4679 f1 = rettv->vval.v_float;
4680 else
4681 f1 = get_tv_number(rettv);
4682 if (var2.v_type == VAR_FLOAT)
4683 f2 = var2.vval.v_float;
4684 else
4685 f2 = get_tv_number(&var2);
4686 n1 = FALSE;
4687 switch (type)
4688 {
4689 case TYPE_EQUAL: n1 = (f1 == f2); break;
4690 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4691 case TYPE_GREATER: n1 = (f1 > f2); break;
4692 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4693 case TYPE_SMALLER: n1 = (f1 < f2); break;
4694 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4695 case TYPE_UNKNOWN:
4696 case TYPE_MATCH:
4697 case TYPE_NOMATCH: break; /* avoid gcc warning */
4698 }
4699 }
4700#endif
4701
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 /*
4703 * If one of the two variables is a number, compare as a number.
4704 * When using "=~" or "!~", always compare as string.
4705 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004706 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 n1 = get_tv_number(rettv);
4710 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 switch (type)
4712 {
4713 case TYPE_EQUAL: n1 = (n1 == n2); break;
4714 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4715 case TYPE_GREATER: n1 = (n1 > n2); break;
4716 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4717 case TYPE_SMALLER: n1 = (n1 < n2); break;
4718 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4719 case TYPE_UNKNOWN:
4720 case TYPE_MATCH:
4721 case TYPE_NOMATCH: break; /* avoid gcc warning */
4722 }
4723 }
4724 else
4725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004726 s1 = get_tv_string_buf(rettv, buf1);
4727 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4729 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4730 else
4731 i = 0;
4732 n1 = FALSE;
4733 switch (type)
4734 {
4735 case TYPE_EQUAL: n1 = (i == 0); break;
4736 case TYPE_NEQUAL: n1 = (i != 0); break;
4737 case TYPE_GREATER: n1 = (i > 0); break;
4738 case TYPE_GEQUAL: n1 = (i >= 0); break;
4739 case TYPE_SMALLER: n1 = (i < 0); break;
4740 case TYPE_SEQUAL: n1 = (i <= 0); break;
4741
4742 case TYPE_MATCH:
4743 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004744 n1 = pattern_match(s2, s1, ic);
4745 if (type == TYPE_NOMATCH)
4746 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 break;
4748
4749 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4750 }
4751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
4753 clear_tv(&var2);
4754 rettv->v_type = VAR_NUMBER;
4755 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 }
4758
4759 return OK;
4760}
4761
4762/*
4763 * Handle fourth level expression:
4764 * + number addition
4765 * - number subtraction
4766 * . string concatenation
4767 *
4768 * "arg" must point to the first non-white of the expression.
4769 * "arg" is advanced to the next non-white after the recognized expression.
4770 *
4771 * Return OK or FAIL.
4772 */
4773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004774eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
Bram Moolenaar33570922005-01-25 22:26:29 +00004776 typval_T var2;
4777 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004779 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#ifdef FEAT_FLOAT
4781 float_T f1 = 0, f2 = 0;
4782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 char_u *s1, *s2;
4784 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4785 char_u *p;
4786
4787 /*
4788 * Get the first variable.
4789 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004790 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 return FAIL;
4792
4793 /*
4794 * Repeat computing, until no '+', '-' or '.' is following.
4795 */
4796 for (;;)
4797 {
4798 op = **arg;
4799 if (op != '+' && op != '-' && op != '.')
4800 break;
4801
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802 if ((op != '+' || rettv->v_type != VAR_LIST)
4803#ifdef FEAT_FLOAT
4804 && (op == '.' || rettv->v_type != VAR_FLOAT)
4805#endif
4806 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 {
4808 /* For "list + ...", an illegal use of the first operand as
4809 * a number cannot be determined before evaluating the 2nd
4810 * operand: if this is also a list, all is ok.
4811 * For "something . ...", "something - ..." or "non-list + ...",
4812 * we know that the first operand needs to be a string or number
4813 * without evaluating the 2nd operand. So check before to avoid
4814 * side effects after an error. */
4815 if (evaluate && get_tv_string_chk(rettv) == NULL)
4816 {
4817 clear_tv(rettv);
4818 return FAIL;
4819 }
4820 }
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 /*
4823 * Get the second variable.
4824 */
4825 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 return FAIL;
4830 }
4831
4832 if (evaluate)
4833 {
4834 /*
4835 * Compute the result.
4836 */
4837 if (op == '.')
4838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004839 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4840 s2 = get_tv_string_buf_chk(&var2, buf2);
4841 if (s2 == NULL) /* type error ? */
4842 {
4843 clear_tv(rettv);
4844 clear_tv(&var2);
4845 return FAIL;
4846 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004847 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 clear_tv(rettv);
4849 rettv->v_type = VAR_STRING;
4850 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004852 else if (op == '+' && rettv->v_type == VAR_LIST
4853 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004854 {
4855 /* concatenate Lists */
4856 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4857 &var3) == FAIL)
4858 {
4859 clear_tv(rettv);
4860 clear_tv(&var2);
4861 return FAIL;
4862 }
4863 clear_tv(rettv);
4864 *rettv = var3;
4865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 else
4867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004868 int error = FALSE;
4869
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004870#ifdef FEAT_FLOAT
4871 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873 f1 = rettv->vval.v_float;
4874 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 else
4877#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 n1 = get_tv_number_chk(rettv, &error);
4880 if (error)
4881 {
4882 /* This can only happen for "list + non-list". For
4883 * "non-list + ..." or "something - ...", we returned
4884 * before evaluating the 2nd operand. */
4885 clear_tv(rettv);
4886 return FAIL;
4887 }
4888#ifdef FEAT_FLOAT
4889 if (var2.v_type == VAR_FLOAT)
4890 f1 = n1;
4891#endif
4892 }
4893#ifdef FEAT_FLOAT
4894 if (var2.v_type == VAR_FLOAT)
4895 {
4896 f2 = var2.vval.v_float;
4897 n2 = 0;
4898 }
4899 else
4900#endif
4901 {
4902 n2 = get_tv_number_chk(&var2, &error);
4903 if (error)
4904 {
4905 clear_tv(rettv);
4906 clear_tv(&var2);
4907 return FAIL;
4908 }
4909#ifdef FEAT_FLOAT
4910 if (rettv->v_type == VAR_FLOAT)
4911 f2 = n2;
4912#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915
4916#ifdef FEAT_FLOAT
4917 /* If there is a float on either side the result is a float. */
4918 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4919 {
4920 if (op == '+')
4921 f1 = f1 + f2;
4922 else
4923 f1 = f1 - f2;
4924 rettv->v_type = VAR_FLOAT;
4925 rettv->vval.v_float = f1;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928#endif
4929 {
4930 if (op == '+')
4931 n1 = n1 + n2;
4932 else
4933 n1 = n1 - n2;
4934 rettv->v_type = VAR_NUMBER;
4935 rettv->vval.v_number = n1;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 }
4941 return OK;
4942}
4943
4944/*
4945 * Handle fifth level expression:
4946 * * number multiplication
4947 * / number division
4948 * % number modulo
4949 *
4950 * "arg" must point to the first non-white of the expression.
4951 * "arg" is advanced to the next non-white after the recognized expression.
4952 *
4953 * Return OK or FAIL.
4954 */
4955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004956eval6(
4957 char_u **arg,
4958 typval_T *rettv,
4959 int evaluate,
4960 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
Bram Moolenaar33570922005-01-25 22:26:29 +00004962 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004964 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965#ifdef FEAT_FLOAT
4966 int use_float = FALSE;
4967 float_T f1 = 0, f2;
4968#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004969 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971 /*
4972 * Get the first variable.
4973 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004974 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 return FAIL;
4976
4977 /*
4978 * Repeat computing, until no '*', '/' or '%' is following.
4979 */
4980 for (;;)
4981 {
4982 op = **arg;
4983 if (op != '*' && op != '/' && op != '%')
4984 break;
4985
4986 if (evaluate)
4987 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988#ifdef FEAT_FLOAT
4989 if (rettv->v_type == VAR_FLOAT)
4990 {
4991 f1 = rettv->vval.v_float;
4992 use_float = TRUE;
4993 n1 = 0;
4994 }
4995 else
4996#endif
4997 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004999 if (error)
5000 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002 else
5003 n1 = 0;
5004
5005 /*
5006 * Get the second variable.
5007 */
5008 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005009 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 return FAIL;
5011
5012 if (evaluate)
5013 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014#ifdef FEAT_FLOAT
5015 if (var2.v_type == VAR_FLOAT)
5016 {
5017 if (!use_float)
5018 {
5019 f1 = n1;
5020 use_float = TRUE;
5021 }
5022 f2 = var2.vval.v_float;
5023 n2 = 0;
5024 }
5025 else
5026#endif
5027 {
5028 n2 = get_tv_number_chk(&var2, &error);
5029 clear_tv(&var2);
5030 if (error)
5031 return FAIL;
5032#ifdef FEAT_FLOAT
5033 if (use_float)
5034 f2 = n2;
5035#endif
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 /*
5039 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005040 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005042#ifdef FEAT_FLOAT
5043 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 if (op == '*')
5046 f1 = f1 * f2;
5047 else if (op == '/')
5048 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005049# ifdef VMS
5050 /* VMS crashes on divide by zero, work around it */
5051 if (f2 == 0.0)
5052 {
5053 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005054 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005055 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005056 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005057 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005058 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005059 }
5060 else
5061 f1 = f1 / f2;
5062# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005063 /* We rely on the floating point library to handle divide
5064 * by zero to result in "inf" and not a crash. */
5065 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005066# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005069 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005070 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 return FAIL;
5072 }
5073 rettv->v_type = VAR_FLOAT;
5074 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 if (op == '*')
5080 n1 = n1 * n2;
5081 else if (op == '/')
5082 {
5083 if (n2 == 0) /* give an error message? */
5084 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005085#ifdef FEAT_NUM64
5086 if (n1 == 0)
5087 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
5088 else if (n1 < 0)
5089 n1 = -0x7fffffffffffffff;
5090 else
5091 n1 = 0x7fffffffffffffff;
5092#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 if (n1 == 0)
5094 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5095 else if (n1 < 0)
5096 n1 = -0x7fffffffL;
5097 else
5098 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005099#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005100 }
5101 else
5102 n1 = n1 / n2;
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 {
5106 if (n2 == 0) /* give an error message? */
5107 n1 = 0;
5108 else
5109 n1 = n1 % n2;
5110 }
5111 rettv->v_type = VAR_NUMBER;
5112 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 }
5116
5117 return OK;
5118}
5119
5120/*
5121 * Handle sixth level expression:
5122 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005123 * "string" string constant
5124 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 * &option-name option value
5126 * @r register contents
5127 * identifier variable value
5128 * function() function call
5129 * $VAR environment variable
5130 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005131 * [expr, expr] List
5132 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 *
5134 * Also handle:
5135 * ! in front logical NOT
5136 * - in front unary minus
5137 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 * trailing [] subscript in String or List
5139 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 *
5141 * "arg" must point to the first non-white of the expression.
5142 * "arg" is advanced to the next non-white after the recognized expression.
5143 *
5144 * Return OK or FAIL.
5145 */
5146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005147eval7(
5148 char_u **arg,
5149 typval_T *rettv,
5150 int evaluate,
5151 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005153 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 int len;
5155 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 char_u *start_leader, *end_leader;
5157 int ret = OK;
5158 char_u *alias;
5159
5160 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005164 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166 /*
5167 * Skip '!' and '-' characters. They are handled later.
5168 */
5169 start_leader = *arg;
5170 while (**arg == '!' || **arg == '-' || **arg == '+')
5171 *arg = skipwhite(*arg + 1);
5172 end_leader = *arg;
5173
5174 switch (**arg)
5175 {
5176 /*
5177 * Number constant.
5178 */
5179 case '0':
5180 case '1':
5181 case '2':
5182 case '3':
5183 case '4':
5184 case '5':
5185 case '6':
5186 case '7':
5187 case '8':
5188 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005189 {
5190#ifdef FEAT_FLOAT
5191 char_u *p = skipdigits(*arg + 1);
5192 int get_float = FALSE;
5193
5194 /* We accept a float when the format matches
5195 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005196 * strict to avoid backwards compatibility problems.
5197 * Don't look for a float after the "." operator, so that
5198 * ":let vers = 1.2.3" doesn't fail. */
5199 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 get_float = TRUE;
5202 p = skipdigits(p + 2);
5203 if (*p == 'e' || *p == 'E')
5204 {
5205 ++p;
5206 if (*p == '-' || *p == '+')
5207 ++p;
5208 if (!vim_isdigit(*p))
5209 get_float = FALSE;
5210 else
5211 p = skipdigits(p + 1);
5212 }
5213 if (ASCII_ISALPHA(*p) || *p == '.')
5214 get_float = FALSE;
5215 }
5216 if (get_float)
5217 {
5218 float_T f;
5219
5220 *arg += string2float(*arg, &f);
5221 if (evaluate)
5222 {
5223 rettv->v_type = VAR_FLOAT;
5224 rettv->vval.v_float = f;
5225 }
5226 }
5227 else
5228#endif
5229 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005230 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005231 *arg += len;
5232 if (evaluate)
5233 {
5234 rettv->v_type = VAR_NUMBER;
5235 rettv->vval.v_number = n;
5236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241 /*
5242 * String constant: "string".
5243 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005244 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 break;
5246
5247 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005250 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 break;
5252
5253 /*
5254 * List: [expr, expr]
5255 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 break;
5258
5259 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 * Dictionary: {key: val, key: val}
5261 */
5262 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5263 break;
5264
5265 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005266 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005268 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 break;
5270
5271 /*
5272 * Environment variable: $VAR.
5273 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 break;
5276
5277 /*
5278 * Register contents: @r.
5279 */
5280 case '@': ++*arg;
5281 if (evaluate)
5282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005283 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005284 rettv->vval.v_string = get_reg_contents(**arg,
5285 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287 if (**arg != NUL)
5288 ++*arg;
5289 break;
5290
5291 /*
5292 * nested expression: (expression).
5293 */
5294 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005295 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (**arg == ')')
5297 ++*arg;
5298 else if (ret == OK)
5299 {
5300 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 ret = FAIL;
5303 }
5304 break;
5305
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 break;
5308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309
5310 if (ret == NOTDONE)
5311 {
5312 /*
5313 * Must be a variable or function name.
5314 * Can also be a curly-braces kind of name: {expr}.
5315 */
5316 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005317 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 if (alias != NULL)
5319 s = alias;
5320
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 ret = FAIL;
5323 else
5324 {
5325 if (**arg == '(') /* recursive! */
5326 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005327 partial_T *partial;
5328
Bram Moolenaar8c711452005-01-14 21:53:12 +00005329 /* If "s" is the name of a variable of type VAR_FUNC
5330 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005331 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332
5333 /* Invoke the function. */
5334 ret = get_func_tv(s, len, rettv, arg,
5335 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005336 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005337
5338 /* If evaluate is FALSE rettv->v_type was not set in
5339 * get_func_tv, but it's needed in handle_subscript() to parse
5340 * what follows. So set it here. */
5341 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5342 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005343 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005344 rettv->v_type = VAR_FUNC;
5345 }
5346
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 /* Stop the expression evaluation when immediately
5348 * aborting on error, or when an interrupt occurred or
5349 * an exception was thrown but not caught. */
5350 if (aborting())
5351 {
5352 if (ret == OK)
5353 clear_tv(rettv);
5354 ret = FAIL;
5355 }
5356 }
5357 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005358 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005359 else
5360 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005362 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 }
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 *arg = skipwhite(*arg);
5366
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005367 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5368 * expr(expr). */
5369 if (ret == OK)
5370 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372 /*
5373 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5374 */
5375 if (ret == OK && evaluate && end_leader > start_leader)
5376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005377 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005378 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005379#ifdef FEAT_FLOAT
5380 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005381
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005382 if (rettv->v_type == VAR_FLOAT)
5383 f = rettv->vval.v_float;
5384 else
5385#endif
5386 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005387 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005389 clear_tv(rettv);
5390 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005392 else
5393 {
5394 while (end_leader > start_leader)
5395 {
5396 --end_leader;
5397 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005398 {
5399#ifdef FEAT_FLOAT
5400 if (rettv->v_type == VAR_FLOAT)
5401 f = !f;
5402 else
5403#endif
5404 val = !val;
5405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005406 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005407 {
5408#ifdef FEAT_FLOAT
5409 if (rettv->v_type == VAR_FLOAT)
5410 f = -f;
5411 else
5412#endif
5413 val = -val;
5414 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005415 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005416#ifdef FEAT_FLOAT
5417 if (rettv->v_type == VAR_FLOAT)
5418 {
5419 clear_tv(rettv);
5420 rettv->vval.v_float = f;
5421 }
5422 else
5423#endif
5424 {
5425 clear_tv(rettv);
5426 rettv->v_type = VAR_NUMBER;
5427 rettv->vval.v_number = val;
5428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431
5432 return ret;
5433}
5434
5435/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005436 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5437 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5439 */
5440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005441eval_index(
5442 char_u **arg,
5443 typval_T *rettv,
5444 int evaluate,
5445 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446{
5447 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005448 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 long len = -1;
5451 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454
Bram Moolenaara03f2332016-02-06 18:09:59 +01005455 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005457 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005458 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005459 if (verbose)
5460 EMSG(_("E695: Cannot index a Funcref"));
5461 return FAIL;
5462 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005463#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005464 if (verbose)
5465 EMSG(_(e_float_as_string));
5466 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005467#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005468 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005469 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005470 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005471 if (verbose)
5472 EMSG(_("E909: Cannot index a special variable"));
5473 return FAIL;
5474 case VAR_UNKNOWN:
5475 if (evaluate)
5476 return FAIL;
5477 /* FALLTHROUGH */
5478
5479 case VAR_STRING:
5480 case VAR_NUMBER:
5481 case VAR_LIST:
5482 case VAR_DICT:
5483 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005484 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005486 init_tv(&var1);
5487 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 /*
5491 * dict.name
5492 */
5493 key = *arg + 1;
5494 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5495 ;
5496 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 }
5500 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 /*
5503 * something[idx]
5504 *
5505 * Get the (first) variable from inside the [].
5506 */
5507 *arg = skipwhite(*arg + 1);
5508 if (**arg == ':')
5509 empty1 = TRUE;
5510 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5511 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005512 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5513 {
5514 /* not a number or string */
5515 clear_tv(&var1);
5516 return FAIL;
5517 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518
5519 /*
5520 * Get the second variable from inside the [:].
5521 */
5522 if (**arg == ':')
5523 {
5524 range = TRUE;
5525 *arg = skipwhite(*arg + 1);
5526 if (**arg == ']')
5527 empty2 = TRUE;
5528 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005530 if (!empty1)
5531 clear_tv(&var1);
5532 return FAIL;
5533 }
5534 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5535 {
5536 /* not a number or string */
5537 if (!empty1)
5538 clear_tv(&var1);
5539 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 return FAIL;
5541 }
5542 }
5543
5544 /* Check for the ']'. */
5545 if (**arg != ']')
5546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005547 if (verbose)
5548 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005549 clear_tv(&var1);
5550 if (range)
5551 clear_tv(&var2);
5552 return FAIL;
5553 }
5554 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 }
5556
5557 if (evaluate)
5558 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559 n1 = 0;
5560 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 n1 = get_tv_number(&var1);
5563 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 }
5565 if (range)
5566 {
5567 if (empty2)
5568 n2 = -1;
5569 else
5570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 n2 = get_tv_number(&var2);
5572 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 }
5574 }
5575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005578 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005579 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005580 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005581 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582 case VAR_SPECIAL:
5583 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005584 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005585 break; /* not evaluating, skipping over subscript */
5586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 case VAR_NUMBER:
5588 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 len = (long)STRLEN(s);
5591 if (range)
5592 {
5593 /* The resulting variable is a substring. If the indexes
5594 * are out of range the result is empty. */
5595 if (n1 < 0)
5596 {
5597 n1 = len + n1;
5598 if (n1 < 0)
5599 n1 = 0;
5600 }
5601 if (n2 < 0)
5602 n2 = len + n2;
5603 else if (n2 >= len)
5604 n2 = len;
5605 if (n1 >= len || n2 < 0 || n1 > n2)
5606 s = NULL;
5607 else
5608 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5609 }
5610 else
5611 {
5612 /* The resulting variable is a string of a single
5613 * character. If the index is too big or negative the
5614 * result is empty. */
5615 if (n1 >= len || n1 < 0)
5616 s = NULL;
5617 else
5618 s = vim_strnsave(s + n1, 1);
5619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620 clear_tv(rettv);
5621 rettv->v_type = VAR_STRING;
5622 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 break;
5624
5625 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 if (n1 < 0)
5628 n1 = len + n1;
5629 if (!empty1 && (n1 < 0 || n1 >= len))
5630 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005631 /* For a range we allow invalid values and return an empty
5632 * list. A list index out of range is an error. */
5633 if (!range)
5634 {
5635 if (verbose)
5636 EMSGN(_(e_listidx), n1);
5637 return FAIL;
5638 }
5639 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 }
5641 if (range)
5642 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 list_T *l;
5644 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645
5646 if (n2 < 0)
5647 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005648 else if (n2 >= len)
5649 n2 = len - 1;
5650 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005651 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 l = list_alloc();
5653 if (l == NULL)
5654 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 n1 <= n2; ++n1)
5657 {
5658 if (list_append_tv(l, &item->li_tv) == FAIL)
5659 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005660 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005661 return FAIL;
5662 }
5663 item = item->li_next;
5664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 clear_tv(rettv);
5666 rettv->v_type = VAR_LIST;
5667 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005668 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005669 }
5670 else
5671 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005672 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 clear_tv(rettv);
5674 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 }
5676 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677
5678 case VAR_DICT:
5679 if (range)
5680 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005681 if (verbose)
5682 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 if (len == -1)
5684 clear_tv(&var1);
5685 return FAIL;
5686 }
5687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005688 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689
5690 if (len == -1)
5691 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005692 key = get_tv_string_chk(&var1);
5693 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 clear_tv(&var1);
5696 return FAIL;
5697 }
5698 }
5699
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005700 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005702 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005703 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 if (len == -1)
5705 clear_tv(&var1);
5706 if (item == NULL)
5707 return FAIL;
5708
5709 copy_tv(&item->di_tv, &var1);
5710 clear_tv(rettv);
5711 *rettv = var1;
5712 }
5713 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 }
5715 }
5716
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 return OK;
5718}
5719
5720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 * Get an option value.
5722 * "arg" points to the '&' or '+' before the option name.
5723 * "arg" is advanced to character after the option name.
5724 * Return OK or FAIL.
5725 */
5726 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005727get_option_tv(
5728 char_u **arg,
5729 typval_T *rettv, /* when NULL, only check if option exists */
5730 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 char_u *option_end;
5733 long numval;
5734 char_u *stringval;
5735 int opt_type;
5736 int c;
5737 int working = (**arg == '+'); /* has("+option") */
5738 int ret = OK;
5739 int opt_flags;
5740
5741 /*
5742 * Isolate the option name and find its value.
5743 */
5744 option_end = find_option_end(arg, &opt_flags);
5745 if (option_end == NULL)
5746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005747 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 EMSG2(_("E112: Option name missing: %s"), *arg);
5749 return FAIL;
5750 }
5751
5752 if (!evaluate)
5753 {
5754 *arg = option_end;
5755 return OK;
5756 }
5757
5758 c = *option_end;
5759 *option_end = NUL;
5760 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005761 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 if (opt_type == -3) /* invalid name */
5764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 EMSG2(_("E113: Unknown option: %s"), *arg);
5767 ret = FAIL;
5768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005769 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 if (opt_type == -2) /* hidden string option */
5772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005773 rettv->v_type = VAR_STRING;
5774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776 else if (opt_type == -1) /* hidden number option */
5777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005778 rettv->v_type = VAR_NUMBER;
5779 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
5781 else if (opt_type == 1) /* number option */
5782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005783 rettv->v_type = VAR_NUMBER;
5784 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
5786 else /* string option */
5787 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005788 rettv->v_type = VAR_STRING;
5789 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791 }
5792 else if (working && (opt_type == -2 || opt_type == -1))
5793 ret = FAIL;
5794
5795 *option_end = c; /* put back for error messages */
5796 *arg = option_end;
5797
5798 return ret;
5799}
5800
5801/*
5802 * Allocate a variable for a string constant.
5803 * Return OK or FAIL.
5804 */
5805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005806get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
5808 char_u *p;
5809 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 int extra = 0;
5811
5812 /*
5813 * Find the end of the string, skipping backslashed characters.
5814 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {
5817 if (*p == '\\' && p[1] != NUL)
5818 {
5819 ++p;
5820 /* A "\<x>" form occupies at least 4 characters, and produces up
5821 * to 6 characters: reserve space for 2 extra */
5822 if (*p == '<')
5823 extra += 2;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826
5827 if (*p != '"')
5828 {
5829 EMSG2(_("E114: Missing quote: %s"), *arg);
5830 return FAIL;
5831 }
5832
5833 /* If only parsing, set *arg and return here */
5834 if (!evaluate)
5835 {
5836 *arg = p + 1;
5837 return OK;
5838 }
5839
5840 /*
5841 * Copy the string into allocated memory, handling backslashed
5842 * characters.
5843 */
5844 name = alloc((unsigned)(p - *arg + extra));
5845 if (name == NULL)
5846 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 rettv->v_type = VAR_STRING;
5848 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
5852 if (*p == '\\')
5853 {
5854 switch (*++p)
5855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 case 'b': *name++ = BS; ++p; break;
5857 case 'e': *name++ = ESC; ++p; break;
5858 case 'f': *name++ = FF; ++p; break;
5859 case 'n': *name++ = NL; ++p; break;
5860 case 'r': *name++ = CAR; ++p; break;
5861 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863 case 'X': /* hex: "\x1", "\x12" */
5864 case 'x':
5865 case 'u': /* Unicode: "\u0023" */
5866 case 'U':
5867 if (vim_isxdigit(p[1]))
5868 {
5869 int n, nr;
5870 int c = toupper(*p);
5871
5872 if (c == 'X')
5873 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005874 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005876 else
5877 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 nr = 0;
5879 while (--n >= 0 && vim_isxdigit(p[1]))
5880 {
5881 ++p;
5882 nr = (nr << 4) + hex2nr(*p);
5883 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885#ifdef FEAT_MBYTE
5886 /* For "\u" store the number according to
5887 * 'encoding'. */
5888 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 else
5891#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 break;
5895
5896 /* octal: "\1", "\12", "\123" */
5897 case '0':
5898 case '1':
5899 case '2':
5900 case '3':
5901 case '4':
5902 case '5':
5903 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 case '7': *name = *p++ - '0';
5905 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 *name = (*name << 3) + *p++ - '0';
5908 if (*p >= '0' && *p <= '7')
5909 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 break;
5913
5914 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 if (extra != 0)
5917 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 break;
5920 }
5921 /* FALLTHROUGH */
5922
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 break;
5925 }
5926 }
5927 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 *arg = p + 1;
5933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 return OK;
5935}
5936
5937/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 * Return OK or FAIL.
5940 */
5941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005945 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 int reduce = 0;
5947
5948 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 */
5951 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005956 break;
5957 ++reduce;
5958 ++p;
5959 }
5960 }
5961
Bram Moolenaar8c711452005-01-14 21:53:12 +00005962 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005964 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005965 return FAIL;
5966 }
5967
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 if (!evaluate)
5970 {
5971 *arg = p + 1;
5972 return OK;
5973 }
5974
5975 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005976 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005977 */
5978 str = alloc((unsigned)((p - *arg) - reduce));
5979 if (str == NULL)
5980 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981 rettv->v_type = VAR_STRING;
5982 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005983
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005986 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005988 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 break;
5990 ++p;
5991 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005992 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005994 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995 *arg = p + 1;
5996
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 return OK;
5998}
5999
Bram Moolenaarddecc252016-04-06 22:59:37 +02006000 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006001partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02006002{
6003 int i;
6004
6005 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006006 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006007 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006008 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006009 func_unref(pt->pt_name);
6010 vim_free(pt->pt_name);
6011 vim_free(pt);
6012}
6013
6014/*
6015 * Unreference a closure: decrement the reference count and free it when it
6016 * becomes zero.
6017 */
6018 void
6019partial_unref(partial_T *pt)
6020{
6021 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006022 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006023}
6024
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006025/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 * Allocate a variable for a List and fill it from "*arg".
6027 * Return OK or FAIL.
6028 */
6029 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006030get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031{
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 list_T *l = NULL;
6033 typval_T tv;
6034 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035
6036 if (evaluate)
6037 {
6038 l = list_alloc();
6039 if (l == NULL)
6040 return FAIL;
6041 }
6042
6043 *arg = skipwhite(*arg + 1);
6044 while (**arg != ']' && **arg != NUL)
6045 {
6046 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6047 goto failret;
6048 if (evaluate)
6049 {
6050 item = listitem_alloc();
6051 if (item != NULL)
6052 {
6053 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006054 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 list_append(l, item);
6056 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006057 else
6058 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 }
6060
6061 if (**arg == ']')
6062 break;
6063 if (**arg != ',')
6064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006065 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 goto failret;
6067 }
6068 *arg = skipwhite(*arg + 1);
6069 }
6070
6071 if (**arg != ']')
6072 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006073 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074failret:
6075 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006076 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 return FAIL;
6078 }
6079
6080 *arg = skipwhite(*arg + 1);
6081 if (evaluate)
6082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006083 rettv->v_type = VAR_LIST;
6084 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 ++l->lv_refcount;
6086 }
6087
6088 return OK;
6089}
6090
6091/*
6092 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006093 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006095 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006098 list_T *l;
6099
6100 l = (list_T *)alloc_clear(sizeof(list_T));
6101 if (l != NULL)
6102 {
6103 /* Prepend the list to the list of lists for garbage collection. */
6104 if (first_list != NULL)
6105 first_list->lv_used_prev = l;
6106 l->lv_used_prev = NULL;
6107 l->lv_used_next = first_list;
6108 first_list = l;
6109 }
6110 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111}
6112
6113/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006114 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006115 * Returns OK or FAIL.
6116 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006117 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006119{
6120 list_T *l = list_alloc();
6121
6122 if (l == NULL)
6123 return FAIL;
6124
6125 rettv->vval.v_list = l;
6126 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006127 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006128 ++l->lv_refcount;
6129 return OK;
6130}
6131
6132/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133 * Unreference a list: decrement the reference count and free it when it
6134 * becomes zero.
6135 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006137list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006138{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006139 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006140 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141}
6142
6143/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006144 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 * Ignores the reference count.
6146 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006147 static void
6148list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149{
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006152 for (item = l->lv_first; item != NULL; item = l->lv_first)
6153 {
6154 /* Remove the item before deleting it. */
6155 l->lv_first = item->li_next;
6156 clear_tv(&item->li_tv);
6157 vim_free(item);
6158 }
6159}
6160
6161 static void
6162list_free_list(list_T *l)
6163{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006164 /* Remove the list from the list of lists for garbage collection. */
6165 if (l->lv_used_prev == NULL)
6166 first_list = l->lv_used_next;
6167 else
6168 l->lv_used_prev->lv_used_next = l->lv_used_next;
6169 if (l->lv_used_next != NULL)
6170 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6171
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 vim_free(l);
6173}
6174
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006175 void
6176list_free(list_T *l)
6177{
6178 if (!in_free_unref_items)
6179 {
6180 list_free_contents(l);
6181 list_free_list(l);
6182 }
6183}
6184
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185/*
6186 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006187 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006189 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006190listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191{
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193}
6194
6195/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006196 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006201 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 vim_free(item);
6203}
6204
6205/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006206 * Remove a list item from a List and free it. Also clears the value.
6207 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006209listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006210{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006211 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006212 listitem_free(item);
6213}
6214
6215/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216 * Get the number of items in a list.
6217 */
6218 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006219list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221 if (l == NULL)
6222 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006223 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224}
6225
6226/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227 * Return TRUE when two lists have exactly the same values.
6228 */
6229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006230list_equal(
6231 list_T *l1,
6232 list_T *l2,
6233 int ic, /* ignore case for strings */
6234 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006235{
Bram Moolenaar33570922005-01-25 22:26:29 +00006236 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006237
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006238 if (l1 == NULL || l2 == NULL)
6239 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006240 if (l1 == l2)
6241 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006242 if (list_len(l1) != list_len(l2))
6243 return FALSE;
6244
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 for (item1 = l1->lv_first, item2 = l2->lv_first;
6246 item1 != NULL && item2 != NULL;
6247 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006248 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 return FALSE;
6250 return item1 == NULL && item2 == NULL;
6251}
6252
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006253/*
6254 * Return the dictitem that an entry in a hashtable points to.
6255 */
6256 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006257dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006258{
6259 return HI2DI(hi);
6260}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006261
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006263 * Return TRUE when two dictionaries have exactly the same key/values.
6264 */
6265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266dict_equal(
6267 dict_T *d1,
6268 dict_T *d2,
6269 int ic, /* ignore case for strings */
6270 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006271{
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 hashitem_T *hi;
6273 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006274 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006275
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006276 if (d1 == NULL && d2 == NULL)
6277 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006278 if (d1 == NULL || d2 == NULL)
6279 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006280 if (d1 == d2)
6281 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006282 if (dict_len(d1) != dict_len(d2))
6283 return FALSE;
6284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006285 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288 if (!HASHITEM_EMPTY(hi))
6289 {
6290 item2 = dict_find(d2, hi->hi_key, -1);
6291 if (item2 == NULL)
6292 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006293 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006294 return FALSE;
6295 --todo;
6296 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006297 }
6298 return TRUE;
6299}
6300
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006301static int tv_equal_recurse_limit;
6302
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006303 static int
6304func_equal(
6305 typval_T *tv1,
6306 typval_T *tv2,
6307 int ic) /* ignore case */
6308{
6309 char_u *s1, *s2;
6310 dict_T *d1, *d2;
6311 int a1, a2;
6312 int i;
6313
6314 /* empty and NULL function name considered the same */
6315 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6316 : tv1->vval.v_partial->pt_name;
6317 if (s1 != NULL && *s1 == NUL)
6318 s1 = NULL;
6319 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6320 : tv2->vval.v_partial->pt_name;
6321 if (s2 != NULL && *s2 == NUL)
6322 s2 = NULL;
6323 if (s1 == NULL || s2 == NULL)
6324 {
6325 if (s1 != s2)
6326 return FALSE;
6327 }
6328 else if (STRCMP(s1, s2) != 0)
6329 return FALSE;
6330
6331 /* empty dict and NULL dict is different */
6332 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6333 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6334 if (d1 == NULL || d2 == NULL)
6335 {
6336 if (d1 != d2)
6337 return FALSE;
6338 }
6339 else if (!dict_equal(d1, d2, ic, TRUE))
6340 return FALSE;
6341
6342 /* empty list and no list considered the same */
6343 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6344 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6345 if (a1 != a2)
6346 return FALSE;
6347 for (i = 0; i < a1; ++i)
6348 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6349 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6350 return FALSE;
6351
6352 return TRUE;
6353}
6354
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006355/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006356 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006357 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006358 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006359 */
6360 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006361tv_equal(
6362 typval_T *tv1,
6363 typval_T *tv2,
6364 int ic, /* ignore case */
6365 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006366{
6367 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006368 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006369 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006370 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006371
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006372 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006373 * recursiveness to a limit. We guess they are equal then.
6374 * A fixed limit has the problem of still taking an awful long time.
6375 * Reduce the limit every time running into it. That should work fine for
6376 * deeply linked structures that are not recursively linked and catch
6377 * recursiveness quickly. */
6378 if (!recursive)
6379 tv_equal_recurse_limit = 1000;
6380 if (recursive_cnt >= tv_equal_recurse_limit)
6381 {
6382 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006383 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006384 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006385
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006386 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6387 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006388 if ((tv1->v_type == VAR_FUNC
6389 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6390 && (tv2->v_type == VAR_FUNC
6391 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6392 {
6393 ++recursive_cnt;
6394 r = func_equal(tv1, tv2, ic);
6395 --recursive_cnt;
6396 return r;
6397 }
6398
6399 if (tv1->v_type != tv2->v_type)
6400 return FALSE;
6401
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006402 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006404 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006405 ++recursive_cnt;
6406 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6407 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006408 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006409
6410 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006411 ++recursive_cnt;
6412 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6413 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006414 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006415
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006416 case VAR_NUMBER:
6417 return tv1->vval.v_number == tv2->vval.v_number;
6418
6419 case VAR_STRING:
6420 s1 = get_tv_string_buf(tv1, buf1);
6421 s2 = get_tv_string_buf(tv2, buf2);
6422 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006423
6424 case VAR_SPECIAL:
6425 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006426
6427 case VAR_FLOAT:
6428#ifdef FEAT_FLOAT
6429 return tv1->vval.v_float == tv2->vval.v_float;
6430#endif
6431 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006432#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006433 return tv1->vval.v_job == tv2->vval.v_job;
6434#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006435 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006436#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006437 return tv1->vval.v_channel == tv2->vval.v_channel;
6438#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006439 case VAR_FUNC:
6440 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006441 case VAR_UNKNOWN:
6442 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006444
Bram Moolenaara03f2332016-02-06 18:09:59 +01006445 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6446 * does not equal anything, not even itself. */
6447 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448}
6449
6450/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 * Locate item with index "n" in list "l" and return it.
6452 * A negative index is counted from the end; -1 is the last item.
6453 * Returns NULL when "n" is out of range.
6454 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006455 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457{
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 long idx;
6460
6461 if (l == NULL)
6462 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463
6464 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006466 n = l->lv_len + n;
6467
6468 /* Check for index out of range. */
6469 if (n < 0 || n >= l->lv_len)
6470 return NULL;
6471
6472 /* When there is a cached index may start search from there. */
6473 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006475 if (n < l->lv_idx / 2)
6476 {
6477 /* closest to the start of the list */
6478 item = l->lv_first;
6479 idx = 0;
6480 }
6481 else if (n > (l->lv_idx + l->lv_len) / 2)
6482 {
6483 /* closest to the end of the list */
6484 item = l->lv_last;
6485 idx = l->lv_len - 1;
6486 }
6487 else
6488 {
6489 /* closest to the cached index */
6490 item = l->lv_idx_item;
6491 idx = l->lv_idx;
6492 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 }
6494 else
6495 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006496 if (n < l->lv_len / 2)
6497 {
6498 /* closest to the start of the list */
6499 item = l->lv_first;
6500 idx = 0;
6501 }
6502 else
6503 {
6504 /* closest to the end of the list */
6505 item = l->lv_last;
6506 idx = l->lv_len - 1;
6507 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006509
6510 while (n > idx)
6511 {
6512 /* search forward */
6513 item = item->li_next;
6514 ++idx;
6515 }
6516 while (n < idx)
6517 {
6518 /* search backward */
6519 item = item->li_prev;
6520 --idx;
6521 }
6522
6523 /* cache the used index */
6524 l->lv_idx = idx;
6525 l->lv_idx_item = item;
6526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527 return item;
6528}
6529
6530/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006531 * Get list item "l[idx]" as a number.
6532 */
6533 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534list_find_nr(
6535 list_T *l,
6536 long idx,
6537 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006538{
6539 listitem_T *li;
6540
6541 li = list_find(l, idx);
6542 if (li == NULL)
6543 {
6544 if (errorp != NULL)
6545 *errorp = TRUE;
6546 return -1L;
6547 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006548 return (long)get_tv_number_chk(&li->li_tv, errorp);
Bram Moolenaara5525202006-03-02 22:52:09 +00006549}
6550
6551/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006552 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6553 */
6554 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006556{
6557 listitem_T *li;
6558
6559 li = list_find(l, idx - 1);
6560 if (li == NULL)
6561 {
6562 EMSGN(_(e_listidx), idx);
6563 return NULL;
6564 }
6565 return get_tv_string(&li->li_tv);
6566}
6567
6568/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006569 * Locate "item" list "l" and return its index.
6570 * Returns -1 when "item" is not in the list.
6571 */
6572 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006574{
6575 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006577
6578 if (l == NULL)
6579 return -1;
6580 idx = 0;
6581 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6582 ++idx;
6583 if (li == NULL)
6584 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006585 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006586}
6587
6588/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 * Append item "item" to the end of list "l".
6590 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006592list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593{
6594 if (l->lv_last == NULL)
6595 {
6596 /* empty list */
6597 l->lv_first = item;
6598 l->lv_last = item;
6599 item->li_prev = NULL;
6600 }
6601 else
6602 {
6603 l->lv_last->li_next = item;
6604 item->li_prev = l->lv_last;
6605 l->lv_last = item;
6606 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006607 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 item->li_next = NULL;
6609}
6610
6611/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006616list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006618 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
Bram Moolenaar05159a02005-02-26 23:04:13 +00006620 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006622 copy_tv(tv, &li->li_tv);
6623 list_append(l, li);
6624 return OK;
6625}
6626
6627/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006628 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006629 * Return FAIL when out of memory.
6630 */
6631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006633{
6634 listitem_T *li = listitem_alloc();
6635
6636 if (li == NULL)
6637 return FAIL;
6638 li->li_tv.v_type = VAR_DICT;
6639 li->li_tv.v_lock = 0;
6640 li->li_tv.vval.v_dict = dict;
6641 list_append(list, li);
6642 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 return OK;
6644}
6645
6646/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006647 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006648 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006649 * Returns FAIL when out of memory.
6650 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006651 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006652list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006653{
6654 listitem_T *li = listitem_alloc();
6655
6656 if (li == NULL)
6657 return FAIL;
6658 list_append(l, li);
6659 li->li_tv.v_type = VAR_STRING;
6660 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006661 if (str == NULL)
6662 li->li_tv.vval.v_string = NULL;
6663 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006664 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006665 return FAIL;
6666 return OK;
6667}
6668
6669/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006670 * Append "n" to list "l".
6671 * Returns FAIL when out of memory.
6672 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006675{
6676 listitem_T *li;
6677
6678 li = listitem_alloc();
6679 if (li == NULL)
6680 return FAIL;
6681 li->li_tv.v_type = VAR_NUMBER;
6682 li->li_tv.v_lock = 0;
6683 li->li_tv.vval.v_number = n;
6684 list_append(l, li);
6685 return OK;
6686}
6687
6688/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006689 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006690 * If "item" is NULL append at the end.
6691 * Return FAIL when out of memory.
6692 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006694list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006695{
Bram Moolenaar33570922005-01-25 22:26:29 +00006696 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006697
6698 if (ni == NULL)
6699 return FAIL;
6700 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006701 list_insert(l, ni, item);
6702 return OK;
6703}
6704
6705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006707{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006708 if (item == NULL)
6709 /* Append new item at end of list. */
6710 list_append(l, ni);
6711 else
6712 {
6713 /* Insert new item before existing item. */
6714 ni->li_prev = item->li_prev;
6715 ni->li_next = item;
6716 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006717 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006718 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006719 ++l->lv_idx;
6720 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006721 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006722 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006723 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006724 l->lv_idx_item = NULL;
6725 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006726 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006727 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006728 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006729}
6730
6731/*
6732 * Extend "l1" with "l2".
6733 * If "bef" is NULL append at the end, otherwise insert before this item.
6734 * Returns FAIL when out of memory.
6735 */
6736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006738{
Bram Moolenaar33570922005-01-25 22:26:29 +00006739 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006740 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006742 /* We also quit the loop when we have inserted the original item count of
6743 * the list, avoid a hang when we extend a list with itself. */
6744 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006745 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6746 return FAIL;
6747 return OK;
6748}
6749
6750/*
6751 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6752 * Return FAIL when out of memory.
6753 */
6754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006755list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006756{
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006758
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006759 if (l1 == NULL || l2 == NULL)
6760 return FAIL;
6761
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006762 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006763 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006764 if (l == NULL)
6765 return FAIL;
6766 tv->v_type = VAR_LIST;
6767 tv->vval.v_list = l;
6768
6769 /* append all items from the second list */
6770 return list_extend(l, l2, NULL);
6771}
6772
6773/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006775 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006776 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777 * Returns NULL when out of memory.
6778 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006780list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781{
Bram Moolenaar33570922005-01-25 22:26:29 +00006782 list_T *copy;
6783 listitem_T *item;
6784 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006785
6786 if (orig == NULL)
6787 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788
6789 copy = list_alloc();
6790 if (copy != NULL)
6791 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006792 if (copyID != 0)
6793 {
6794 /* Do this before adding the items, because one of the items may
6795 * refer back to this list. */
6796 orig->lv_copyID = copyID;
6797 orig->lv_copylist = copy;
6798 }
6799 for (item = orig->lv_first; item != NULL && !got_int;
6800 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801 {
6802 ni = listitem_alloc();
6803 if (ni == NULL)
6804 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006805 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006806 {
6807 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6808 {
6809 vim_free(ni);
6810 break;
6811 }
6812 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006815 list_append(copy, ni);
6816 }
6817 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006818 if (item != NULL)
6819 {
6820 list_unref(copy);
6821 copy = NULL;
6822 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006823 }
6824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 return copy;
6826}
6827
6828/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006829 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006830 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006831 * This used to be called list_remove, but that conflicts with a Sun header
6832 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006835vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006836{
Bram Moolenaar33570922005-01-25 22:26:29 +00006837 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006838
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006839 /* notify watchers */
6840 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006841 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006842 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006843 list_fix_watch(l, ip);
6844 if (ip == item2)
6845 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006847
6848 if (item2->li_next == NULL)
6849 l->lv_last = item->li_prev;
6850 else
6851 item2->li_next->li_prev = item->li_prev;
6852 if (item->li_prev == NULL)
6853 l->lv_first = item2->li_next;
6854 else
6855 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006856 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857}
6858
6859/*
6860 * Return an allocated string with the string representation of a list.
6861 * May return NULL.
6862 */
6863 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006864list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006865{
6866 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006867
6868 if (tv->vval.v_list == NULL)
6869 return NULL;
6870 ga_init2(&ga, (int)sizeof(char), 80);
6871 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006872 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6873 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006874 {
6875 vim_free(ga.ga_data);
6876 return NULL;
6877 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006878 ga_append(&ga, ']');
6879 ga_append(&ga, NUL);
6880 return (char_u *)ga.ga_data;
6881}
6882
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006883typedef struct join_S {
6884 char_u *s;
6885 char_u *tofree;
6886} join_T;
6887
6888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006889list_join_inner(
6890 garray_T *gap, /* to store the result in */
6891 list_T *l,
6892 char_u *sep,
6893 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006894 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006895 int copyID,
6896 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006897{
6898 int i;
6899 join_T *p;
6900 int len;
6901 int sumlen = 0;
6902 int first = TRUE;
6903 char_u *tofree;
6904 char_u numbuf[NUMBUFLEN];
6905 listitem_T *item;
6906 char_u *s;
6907
6908 /* Stringify each item in the list. */
6909 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6910 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006911 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6912 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006913 if (s == NULL)
6914 return FAIL;
6915
6916 len = (int)STRLEN(s);
6917 sumlen += len;
6918
Bram Moolenaarcde88542015-08-11 19:14:00 +02006919 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006920 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6921 if (tofree != NULL || s != numbuf)
6922 {
6923 p->s = s;
6924 p->tofree = tofree;
6925 }
6926 else
6927 {
6928 p->s = vim_strnsave(s, len);
6929 p->tofree = p->s;
6930 }
6931
6932 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006933 if (did_echo_string_emsg) /* recursion error, bail out */
6934 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006935 }
6936
6937 /* Allocate result buffer with its total size, avoid re-allocation and
6938 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6939 if (join_gap->ga_len >= 2)
6940 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6941 if (ga_grow(gap, sumlen + 2) == FAIL)
6942 return FAIL;
6943
6944 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6945 {
6946 if (first)
6947 first = FALSE;
6948 else
6949 ga_concat(gap, sep);
6950 p = ((join_T *)join_gap->ga_data) + i;
6951
6952 if (p->s != NULL)
6953 ga_concat(gap, p->s);
6954 line_breakcheck();
6955 }
6956
6957 return OK;
6958}
6959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006960/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006961 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006962 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006963 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006964 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006966list_join(
6967 garray_T *gap,
6968 list_T *l,
6969 char_u *sep,
6970 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006971 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006973{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006974 garray_T join_ga;
6975 int retval;
6976 join_T *p;
6977 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006978
Bram Moolenaard39a7512015-04-16 22:51:22 +02006979 if (l->lv_len < 1)
6980 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006981 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006982 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6983 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006984
6985 /* Dispose each item in join_ga. */
6986 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006987 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006988 p = (join_T *)join_ga.ga_data;
6989 for (i = 0; i < join_ga.ga_len; ++i)
6990 {
6991 vim_free(p->tofree);
6992 ++p;
6993 }
6994 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006995 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006996
6997 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006998}
6999
7000/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007001 * Return the next (unique) copy ID.
7002 * Used for serializing nested structures.
7003 */
7004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007006{
7007 current_copyID += COPYID_INC;
7008 return current_copyID;
7009}
7010
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007011/* Used by get_func_tv() */
7012static garray_T funcargs = GA_EMPTY;
7013
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007014/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 * Garbage collection for lists and dictionaries.
7016 *
7017 * We use reference counts to be able to free most items right away when they
7018 * are no longer used. But for composite items it's possible that it becomes
7019 * unused while the reference count is > 0: When there is a recursive
7020 * reference. Example:
7021 * :let l = [1, 2, 3]
7022 * :let d = {9: l}
7023 * :let l[1] = d
7024 *
7025 * Since this is quite unusual we handle this with garbage collection: every
7026 * once in a while find out which lists and dicts are not referenced from any
7027 * variable.
7028 *
7029 * Here is a good reference text about garbage collection (refers to Python
7030 * but it applies to all reference-counting mechanisms):
7031 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007032 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007033
7034/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007036 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007038 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007040garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007041{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007042 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007043 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 buf_T *buf;
7045 win_T *wp;
7046 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007047 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007048 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007050#ifdef FEAT_WINDOWS
7051 tabpage_T *tp;
7052#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007054 if (!testing)
7055 {
7056 /* Only do this once. */
7057 want_garbage_collect = FALSE;
7058 may_garbage_collect = FALSE;
7059 garbage_collect_at_exit = FALSE;
7060 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007061
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007062 /* We advance by two because we add one for items referenced through
7063 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007064 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007065
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066 /*
7067 * 1. Go through all accessible variables and mark all lists and dicts
7068 * with copyID.
7069 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007070
7071 /* Don't free variables in the previous_funccal list unless they are only
7072 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007073 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7075 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7077 NULL);
7078 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7079 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007080 }
7081
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 /* script-local variables */
7083 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085
7086 /* buffer-local variables */
7087 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7089 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090
7091 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007092 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007093 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7094 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007095#ifdef FEAT_AUTOCMD
7096 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7098 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007099#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007101#ifdef FEAT_WINDOWS
7102 /* tabpage-local variables */
7103 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7105 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007106#endif
7107
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007108 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007109 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110
7111 /* function-local variables */
7112 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7113 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7115 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116 }
7117
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007118 /* function call arguments, if v:testing is set. */
7119 for (i = 0; i < funcargs.ga_len; ++i)
7120 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7121 copyID, NULL, NULL);
7122
Bram Moolenaard812df62008-11-09 12:46:09 +00007123 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007124 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007125
Bram Moolenaar1dced572012-04-05 16:54:08 +02007126#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007128#endif
7129
Bram Moolenaardb913952012-06-29 12:54:53 +02007130#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007132#endif
7133
7134#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007136#endif
7137
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007138#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007139 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007140 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007141#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007142#ifdef FEAT_NETBEANS_INTG
7143 abort = abort || set_ref_in_nb_channel(copyID);
7144#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007145
Bram Moolenaare3188e22016-05-31 21:13:04 +02007146#ifdef FEAT_TIMERS
7147 abort = abort || set_ref_in_timer(copyID);
7148#endif
7149
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007151 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007152 /*
7153 * 2. Free lists and dictionaries that are not referenced.
7154 */
7155 did_free = free_unref_items(copyID);
7156
7157 /*
7158 * 3. Check if any funccal can be freed now.
7159 */
7160 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007161 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007162 if (can_free_funccal(*pfc, copyID))
7163 {
7164 fc = *pfc;
7165 *pfc = fc->caller;
7166 free_funccal(fc, TRUE);
7167 did_free = TRUE;
7168 did_free_funccal = TRUE;
7169 }
7170 else
7171 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007172 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 if (did_free_funccal)
7174 /* When a funccal was freed some more items might be garbage
7175 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007176 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007177 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 else if (p_verbose > 0)
7179 {
7180 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7181 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007182
7183 return did_free;
7184}
7185
7186/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007187 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007188 */
7189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007190free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007191{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007192 dict_T *dd, *dd_next;
7193 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007194 int did_free = FALSE;
7195
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007196 /* Let all "free" functions know that we are here. This means no
7197 * dictionaries, lists, channels or jobs are to be freed, because we will
7198 * do that here. */
7199 in_free_unref_items = TRUE;
7200
7201 /*
7202 * PASS 1: free the contents of the items. We don't free the items
7203 * themselves yet, so that it is possible to decrement refcount counters
7204 */
7205
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007206 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007207 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007208 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007209 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007210 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007211 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007212 /* Free the Dictionary and ordinary items it contains, but don't
7213 * recurse into Lists and Dictionaries, they will be in the list
7214 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007215 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007216 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007218
7219 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007220 * Go through the list of lists and free items without the copyID.
7221 * But don't free a list that has a watcher (used in a for loop), these
7222 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007224 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7225 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7226 && ll->lv_watch == NULL)
7227 {
7228 /* Free the List and ordinary items it contains, but don't recurse
7229 * into Lists and Dictionaries, they will be in the list of dicts
7230 * or list of lists. */
7231 list_free_contents(ll);
7232 did_free = TRUE;
7233 }
7234
7235#ifdef FEAT_JOB_CHANNEL
7236 /* Go through the list of jobs and free items without the copyID. This
7237 * must happen before doing channels, because jobs refer to channels, but
7238 * the reference from the channel to the job isn't tracked. */
7239 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7240
7241 /* Go through the list of channels and free items without the copyID. */
7242 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7243#endif
7244
7245 /*
7246 * PASS 2: free the items themselves.
7247 */
7248 for (dd = first_dict; dd != NULL; dd = dd_next)
7249 {
7250 dd_next = dd->dv_used_next;
7251 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7252 dict_free_dict(dd);
7253 }
7254
7255 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007256 {
7257 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007258 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7259 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007260 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007261 /* Free the List and ordinary items it contains, but don't recurse
7262 * into Lists and Dictionaries, they will be in the list of dicts
7263 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007264 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007265 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007266 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007267
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007268#ifdef FEAT_JOB_CHANNEL
7269 /* Go through the list of jobs and free items without the copyID. This
7270 * must happen before doing channels, because jobs refer to channels, but
7271 * the reference from the channel to the job isn't tracked. */
7272 free_unused_jobs(copyID, COPYID_MASK);
7273
7274 /* Go through the list of channels and free items without the copyID. */
7275 free_unused_channels(copyID, COPYID_MASK);
7276#endif
7277
7278 in_free_unref_items = FALSE;
7279
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007280 return did_free;
7281}
7282
7283/*
7284 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007285 * "list_stack" is used to add lists to be marked. Can be NULL.
7286 *
7287 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007288 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007291{
7292 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007293 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007294 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007295 hashtab_T *cur_ht;
7296 ht_stack_T *ht_stack = NULL;
7297 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007298
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007299 cur_ht = ht;
7300 for (;;)
7301 {
7302 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007303 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007304 /* Mark each item in the hashtab. If the item contains a hashtab
7305 * it is added to ht_stack, if it contains a list it is added to
7306 * list_stack. */
7307 todo = (int)cur_ht->ht_used;
7308 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7309 if (!HASHITEM_EMPTY(hi))
7310 {
7311 --todo;
7312 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7313 &ht_stack, list_stack);
7314 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007315 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007316
7317 if (ht_stack == NULL)
7318 break;
7319
7320 /* take an item from the stack */
7321 cur_ht = ht_stack->ht;
7322 tempitem = ht_stack;
7323 ht_stack = ht_stack->prev;
7324 free(tempitem);
7325 }
7326
7327 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007328}
7329
7330/*
7331 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007332 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7333 *
7334 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007335 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007337set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007338{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007339 listitem_T *li;
7340 int abort = FALSE;
7341 list_T *cur_l;
7342 list_stack_T *list_stack = NULL;
7343 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007344
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007345 cur_l = l;
7346 for (;;)
7347 {
7348 if (!abort)
7349 /* Mark each item in the list. If the item contains a hashtab
7350 * it is added to ht_stack, if it contains a list it is added to
7351 * list_stack. */
7352 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7353 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7354 ht_stack, &list_stack);
7355 if (list_stack == NULL)
7356 break;
7357
7358 /* take an item from the stack */
7359 cur_l = list_stack->list;
7360 tempitem = list_stack;
7361 list_stack = list_stack->prev;
7362 free(tempitem);
7363 }
7364
7365 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007366}
7367
7368/*
7369 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007370 * "list_stack" is used to add lists to be marked. Can be NULL.
7371 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7372 *
7373 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007374 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007375 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007376set_ref_in_item(
7377 typval_T *tv,
7378 int copyID,
7379 ht_stack_T **ht_stack,
7380 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007381{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007382 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007383
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007384 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007385 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007386 dict_T *dd = tv->vval.v_dict;
7387
Bram Moolenaara03f2332016-02-06 18:09:59 +01007388 if (dd != NULL && dd->dv_copyID != copyID)
7389 {
7390 /* Didn't see this dict yet. */
7391 dd->dv_copyID = copyID;
7392 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007393 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007394 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7395 }
7396 else
7397 {
7398 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7399 if (newitem == NULL)
7400 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007401 else
7402 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007403 newitem->ht = &dd->dv_hashtab;
7404 newitem->prev = *ht_stack;
7405 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007406 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007407 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007408 }
7409 }
7410 else if (tv->v_type == VAR_LIST)
7411 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007412 list_T *ll = tv->vval.v_list;
7413
Bram Moolenaara03f2332016-02-06 18:09:59 +01007414 if (ll != NULL && ll->lv_copyID != copyID)
7415 {
7416 /* Didn't see this list yet. */
7417 ll->lv_copyID = copyID;
7418 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007419 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007420 abort = set_ref_in_list(ll, copyID, ht_stack);
7421 }
7422 else
7423 {
7424 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007425 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007426 if (newitem == NULL)
7427 abort = TRUE;
7428 else
7429 {
7430 newitem->list = ll;
7431 newitem->prev = *list_stack;
7432 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007433 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007434 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007435 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007436 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007437 else if (tv->v_type == VAR_PARTIAL)
7438 {
7439 partial_T *pt = tv->vval.v_partial;
7440 int i;
7441
7442 /* A partial does not have a copyID, because it cannot contain itself.
7443 */
7444 if (pt != NULL)
7445 {
7446 if (pt->pt_dict != NULL)
7447 {
7448 typval_T dtv;
7449
7450 dtv.v_type = VAR_DICT;
7451 dtv.vval.v_dict = pt->pt_dict;
7452 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7453 }
7454
7455 for (i = 0; i < pt->pt_argc; ++i)
7456 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7457 ht_stack, list_stack);
7458 }
7459 }
7460#ifdef FEAT_JOB_CHANNEL
7461 else if (tv->v_type == VAR_JOB)
7462 {
7463 job_T *job = tv->vval.v_job;
7464 typval_T dtv;
7465
7466 if (job != NULL && job->jv_copyID != copyID)
7467 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007468 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007469 if (job->jv_channel != NULL)
7470 {
7471 dtv.v_type = VAR_CHANNEL;
7472 dtv.vval.v_channel = job->jv_channel;
7473 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7474 }
7475 if (job->jv_exit_partial != NULL)
7476 {
7477 dtv.v_type = VAR_PARTIAL;
7478 dtv.vval.v_partial = job->jv_exit_partial;
7479 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7480 }
7481 }
7482 }
7483 else if (tv->v_type == VAR_CHANNEL)
7484 {
7485 channel_T *ch =tv->vval.v_channel;
7486 int part;
7487 typval_T dtv;
7488 jsonq_T *jq;
7489 cbq_T *cq;
7490
7491 if (ch != NULL && ch->ch_copyID != copyID)
7492 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007493 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007494 for (part = PART_SOCK; part <= PART_IN; ++part)
7495 {
7496 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7497 jq = jq->jq_next)
7498 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7499 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7500 cq = cq->cq_next)
7501 if (cq->cq_partial != NULL)
7502 {
7503 dtv.v_type = VAR_PARTIAL;
7504 dtv.vval.v_partial = cq->cq_partial;
7505 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7506 }
7507 if (ch->ch_part[part].ch_partial != NULL)
7508 {
7509 dtv.v_type = VAR_PARTIAL;
7510 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7511 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7512 }
7513 }
7514 if (ch->ch_partial != NULL)
7515 {
7516 dtv.v_type = VAR_PARTIAL;
7517 dtv.vval.v_partial = ch->ch_partial;
7518 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7519 }
7520 if (ch->ch_close_partial != NULL)
7521 {
7522 dtv.v_type = VAR_PARTIAL;
7523 dtv.vval.v_partial = ch->ch_close_partial;
7524 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7525 }
7526 }
7527 }
7528#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007529 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007530}
7531
7532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 * Allocate an empty header for a dictionary.
7534 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007535 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537{
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007541 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007542 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007543 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007544 if (first_dict != NULL)
7545 first_dict->dv_used_prev = d;
7546 d->dv_used_next = first_dict;
7547 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007548 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007549
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007551 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007552 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007553 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007554 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007555 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557}
7558
7559/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007560 * Allocate an empty dict for a return value.
7561 * Returns OK or FAIL.
7562 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007563 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007565{
7566 dict_T *d = dict_alloc();
7567
7568 if (d == NULL)
7569 return FAIL;
7570
7571 rettv->vval.v_dict = d;
7572 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007573 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007574 ++d->dv_refcount;
7575 return OK;
7576}
7577
7578
7579/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 * Unreference a Dictionary: decrement the reference count and free it when it
7581 * becomes zero.
7582 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007586 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007587 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588}
7589
7590/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007591 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 * Ignores the reference count.
7593 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007594 static void
7595dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007599 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007601 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007602 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007603 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007606 if (!HASHITEM_EMPTY(hi))
7607 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007608 /* Remove the item before deleting it, just in case there is
7609 * something recursive causing trouble. */
7610 di = HI2DI(hi);
7611 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007612 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007613 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 --todo;
7615 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007618}
7619
7620 static void
7621dict_free_dict(dict_T *d)
7622{
7623 /* Remove the dict from the list of dicts for garbage collection. */
7624 if (d->dv_used_prev == NULL)
7625 first_dict = d->dv_used_next;
7626 else
7627 d->dv_used_prev->dv_used_next = d->dv_used_next;
7628 if (d->dv_used_next != NULL)
7629 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 vim_free(d);
7631}
7632
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007633 void
7634dict_free(dict_T *d)
7635{
7636 if (!in_free_unref_items)
7637 {
7638 dict_free_contents(d);
7639 dict_free_dict(d);
7640 }
7641}
7642
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643/*
7644 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007645 * The "key" is copied to the new item.
7646 * Note that the value of the item "di_tv" still needs to be initialized!
7647 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007649 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007650dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651{
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007653
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007654 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007656 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007658 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007659 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007660 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661}
7662
7663/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007664 * Make a copy of a Dictionary item.
7665 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007667dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668{
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007670
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007671 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7672 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 if (di != NULL)
7674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007675 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007676 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 copy_tv(&org->di_tv, &di->di_tv);
7678 }
7679 return di;
7680}
7681
7682/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007683 * Remove item "item" from Dictionary "dict" and free it.
7684 */
7685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007686dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687{
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007689
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 if (HASHITEM_EMPTY(hi))
7692 EMSG2(_(e_intern2), "dictitem_remove()");
7693 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 dictitem_free(item);
7696}
7697
7698/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 * Free a dict item. Also clears the value.
7700 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007702dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007705 if (item->di_flags & DI_FLAGS_ALLOC)
7706 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707}
7708
7709/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7711 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007712 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 * Returns NULL when out of memory.
7714 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007716dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007717{
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 dict_T *copy;
7719 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007720 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722
7723 if (orig == NULL)
7724 return NULL;
7725
7726 copy = dict_alloc();
7727 if (copy != NULL)
7728 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007729 if (copyID != 0)
7730 {
7731 orig->dv_copyID = copyID;
7732 orig->dv_copydict = copy;
7733 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007734 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007735 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007736 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007737 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739 --todo;
7740
7741 di = dictitem_alloc(hi->hi_key);
7742 if (di == NULL)
7743 break;
7744 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007745 {
7746 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7747 copyID) == FAIL)
7748 {
7749 vim_free(di);
7750 break;
7751 }
7752 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007753 else
7754 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7755 if (dict_add(copy, di) == FAIL)
7756 {
7757 dictitem_free(di);
7758 break;
7759 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007761 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007762
Bram Moolenaare9a41262005-01-15 22:18:47 +00007763 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007764 if (todo > 0)
7765 {
7766 dict_unref(copy);
7767 copy = NULL;
7768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007769 }
7770
7771 return copy;
7772}
7773
7774/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007776 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007779dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007780{
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782}
7783
Bram Moolenaar8c711452005-01-14 21:53:12 +00007784/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 * Add a number or string entry to dictionary "d".
7786 * When "str" is NULL use number "nr", otherwise use "str".
7787 * Returns FAIL when out of memory and when key already exists.
7788 */
7789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007790dict_add_nr_str(
7791 dict_T *d,
7792 char *key,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007793 varnumber_T nr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007795{
7796 dictitem_T *item;
7797
7798 item = dictitem_alloc((char_u *)key);
7799 if (item == NULL)
7800 return FAIL;
7801 item->di_tv.v_lock = 0;
7802 if (str == NULL)
7803 {
7804 item->di_tv.v_type = VAR_NUMBER;
7805 item->di_tv.vval.v_number = nr;
7806 }
7807 else
7808 {
7809 item->di_tv.v_type = VAR_STRING;
7810 item->di_tv.vval.v_string = vim_strsave(str);
7811 }
7812 if (dict_add(d, item) == FAIL)
7813 {
7814 dictitem_free(item);
7815 return FAIL;
7816 }
7817 return OK;
7818}
7819
7820/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007821 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007822 * Returns FAIL when out of memory and when key already exists.
7823 */
7824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007825dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007826{
7827 dictitem_T *item;
7828
7829 item = dictitem_alloc((char_u *)key);
7830 if (item == NULL)
7831 return FAIL;
7832 item->di_tv.v_lock = 0;
7833 item->di_tv.v_type = VAR_LIST;
7834 item->di_tv.vval.v_list = list;
7835 if (dict_add(d, item) == FAIL)
7836 {
7837 dictitem_free(item);
7838 return FAIL;
7839 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007840 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007841 return OK;
7842}
7843
7844/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 * Get the number of items in a Dictionary.
7846 */
7847 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007848dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 if (d == NULL)
7851 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007852 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853}
7854
7855/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 * Find item "key[len]" in Dictionary "d".
7857 * If "len" is negative use strlen(key).
7858 * Returns NULL when not found.
7859 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007860 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007861dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007862{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007863#define AKEYLEN 200
7864 char_u buf[AKEYLEN];
7865 char_u *akey;
7866 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007868
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007869 if (d == NULL)
7870 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007871 if (len < 0)
7872 akey = key;
7873 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007874 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007875 tofree = akey = vim_strnsave(key, len);
7876 if (akey == NULL)
7877 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007878 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 else
7880 {
7881 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007882 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007883 akey = buf;
7884 }
7885
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007887 vim_free(tofree);
7888 if (HASHITEM_EMPTY(hi))
7889 return NULL;
7890 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007891}
7892
7893/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007894 * Get a string item from a dictionary.
7895 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007896 * Returns NULL if the entry doesn't exist or out of memory.
7897 */
7898 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007899get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007900{
7901 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007902 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007903
7904 di = dict_find(d, key, -1);
7905 if (di == NULL)
7906 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007907 s = get_tv_string(&di->di_tv);
7908 if (save && s != NULL)
7909 s = vim_strsave(s);
7910 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007911}
7912
7913/*
7914 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007915 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007916 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007917 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007918get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007919{
7920 dictitem_T *di;
7921
7922 di = dict_find(d, key, -1);
7923 if (di == NULL)
7924 return 0;
7925 return get_tv_number(&di->di_tv);
7926}
7927
7928/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 * Return an allocated string with the string representation of a Dictionary.
7930 * May return NULL.
7931 */
7932 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007933dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934{
7935 garray_T ga;
7936 int first = TRUE;
7937 char_u *tofree;
7938 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007940 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007942 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007944 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 return NULL;
7946 ga_init2(&ga, (int)sizeof(char), 80);
7947 ga_append(&ga, '{');
7948
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007949 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007950 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007951 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007952 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007954 --todo;
7955
7956 if (first)
7957 first = FALSE;
7958 else
7959 ga_concat(&ga, (char_u *)", ");
7960
7961 tofree = string_quote(hi->hi_key, FALSE);
7962 if (tofree != NULL)
7963 {
7964 ga_concat(&ga, tofree);
7965 vim_free(tofree);
7966 }
7967 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007968 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7969 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007970 if (s != NULL)
7971 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007973 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007974 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007975 line_breakcheck();
7976
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007979 if (todo > 0)
7980 {
7981 vim_free(ga.ga_data);
7982 return NULL;
7983 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007984
7985 ga_append(&ga, '}');
7986 ga_append(&ga, NUL);
7987 return (char_u *)ga.ga_data;
7988}
7989
7990/*
7991 * Allocate a variable for a Dictionary and fill it from "*arg".
7992 * Return OK or FAIL. Returns NOTDONE for {expr}.
7993 */
7994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007995get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007996{
Bram Moolenaar33570922005-01-25 22:26:29 +00007997 dict_T *d = NULL;
7998 typval_T tvkey;
7999 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00008000 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008002 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008003 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00008004
8005 /*
8006 * First check if it's not a curly-braces thing: {expr}.
8007 * Must do this without evaluating, otherwise a function may be called
8008 * twice. Unfortunately this means we need to call eval1() twice for the
8009 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008011 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008012 if (*start != '}')
8013 {
8014 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
8015 return FAIL;
8016 if (*start == '}')
8017 return NOTDONE;
8018 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008019
8020 if (evaluate)
8021 {
8022 d = dict_alloc();
8023 if (d == NULL)
8024 return FAIL;
8025 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008026 tvkey.v_type = VAR_UNKNOWN;
8027 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008028
8029 *arg = skipwhite(*arg + 1);
8030 while (**arg != '}' && **arg != NUL)
8031 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008032 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 goto failret;
8034 if (**arg != ':')
8035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008036 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008037 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008038 goto failret;
8039 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008040 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008041 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008042 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008043 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008044 {
8045 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008046 clear_tv(&tvkey);
8047 goto failret;
8048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008049 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008050
8051 *arg = skipwhite(*arg + 1);
8052 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8053 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008054 if (evaluate)
8055 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008056 goto failret;
8057 }
8058 if (evaluate)
8059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008060 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 if (item != NULL)
8062 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008063 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008064 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 clear_tv(&tv);
8066 goto failret;
8067 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008068 item = dictitem_alloc(key);
8069 clear_tv(&tvkey);
8070 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008071 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008072 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008073 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008074 if (dict_add(d, item) == FAIL)
8075 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008076 }
8077 }
8078
8079 if (**arg == '}')
8080 break;
8081 if (**arg != ',')
8082 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008083 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008084 goto failret;
8085 }
8086 *arg = skipwhite(*arg + 1);
8087 }
8088
8089 if (**arg != '}')
8090 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008091 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008092failret:
8093 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008094 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008095 return FAIL;
8096 }
8097
8098 *arg = skipwhite(*arg + 1);
8099 if (evaluate)
8100 {
8101 rettv->v_type = VAR_DICT;
8102 rettv->vval.v_dict = d;
8103 ++d->dv_refcount;
8104 }
8105
8106 return OK;
8107}
8108
Bram Moolenaar17a13432016-01-24 14:22:10 +01008109 static char *
8110get_var_special_name(int nr)
8111{
8112 switch (nr)
8113 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008114 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008115 case VVAL_TRUE: return "v:true";
8116 case VVAL_NONE: return "v:none";
8117 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008118 }
8119 EMSG2(_(e_intern2), "get_var_special_name()");
8120 return "42";
8121}
8122
Bram Moolenaar8c711452005-01-14 21:53:12 +00008123/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 * Return a string with the string representation of a variable.
8125 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008127 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008128 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8129 * "string()", otherwise does not put quotes around strings, as ":echo"
8130 * displays values.
8131 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8132 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008133 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134 */
8135 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008136echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008137 typval_T *tv,
8138 char_u **tofree,
8139 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008140 int copyID,
8141 int echo_style,
8142 int restore_copyID,
8143 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008144{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145 static int recurse = 0;
8146 char_u *r = NULL;
8147
Bram Moolenaar33570922005-01-25 22:26:29 +00008148 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008149 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008150 if (!did_echo_string_emsg)
8151 {
8152 /* Only give this message once for a recursive call to avoid
8153 * flooding the user with errors. And stop iterating over lists
8154 * and dicts. */
8155 did_echo_string_emsg = TRUE;
8156 EMSG(_("E724: variable nested too deep for displaying"));
8157 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008159 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008160 }
8161 ++recurse;
8162
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008163 switch (tv->v_type)
8164 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008165 case VAR_STRING:
8166 if (echo_style && !dict_val)
8167 {
8168 *tofree = NULL;
8169 r = get_tv_string_buf(tv, numbuf);
8170 }
8171 else
8172 {
8173 *tofree = string_quote(tv->vval.v_string, FALSE);
8174 r = *tofree;
8175 }
8176 break;
8177
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008178 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008179 if (echo_style)
8180 {
8181 *tofree = NULL;
8182 r = tv->vval.v_string;
8183 }
8184 else
8185 {
8186 *tofree = string_quote(tv->vval.v_string, TRUE);
8187 r = *tofree;
8188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008190
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008191 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008192 {
8193 partial_T *pt = tv->vval.v_partial;
8194 char_u *fname = string_quote(pt == NULL ? NULL
8195 : pt->pt_name, FALSE);
8196 garray_T ga;
8197 int i;
8198 char_u *tf;
8199
8200 ga_init2(&ga, 1, 100);
8201 ga_concat(&ga, (char_u *)"function(");
8202 if (fname != NULL)
8203 {
8204 ga_concat(&ga, fname);
8205 vim_free(fname);
8206 }
8207 if (pt != NULL && pt->pt_argc > 0)
8208 {
8209 ga_concat(&ga, (char_u *)", [");
8210 for (i = 0; i < pt->pt_argc; ++i)
8211 {
8212 if (i > 0)
8213 ga_concat(&ga, (char_u *)", ");
8214 ga_concat(&ga,
8215 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8216 vim_free(tf);
8217 }
8218 ga_concat(&ga, (char_u *)"]");
8219 }
8220 if (pt != NULL && pt->pt_dict != NULL)
8221 {
8222 typval_T dtv;
8223
8224 ga_concat(&ga, (char_u *)", ");
8225 dtv.v_type = VAR_DICT;
8226 dtv.vval.v_dict = pt->pt_dict;
8227 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8228 vim_free(tf);
8229 }
8230 ga_concat(&ga, (char_u *)")");
8231
8232 *tofree = ga.ga_data;
8233 r = *tofree;
8234 break;
8235 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008236
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008237 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008238 if (tv->vval.v_list == NULL)
8239 {
8240 *tofree = NULL;
8241 r = NULL;
8242 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008243 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8244 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008245 {
8246 *tofree = NULL;
8247 r = (char_u *)"[...]";
8248 }
8249 else
8250 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008251 int old_copyID = tv->vval.v_list->lv_copyID;
8252
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008253 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008254 *tofree = list2string(tv, copyID, restore_copyID);
8255 if (restore_copyID)
8256 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008257 r = *tofree;
8258 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008259 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008260
Bram Moolenaar8c711452005-01-14 21:53:12 +00008261 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008262 if (tv->vval.v_dict == NULL)
8263 {
8264 *tofree = NULL;
8265 r = NULL;
8266 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008267 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8268 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008269 {
8270 *tofree = NULL;
8271 r = (char_u *)"{...}";
8272 }
8273 else
8274 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008275 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008276 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008277 *tofree = dict2string(tv, copyID, restore_copyID);
8278 if (restore_copyID)
8279 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008280 r = *tofree;
8281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008282 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008283
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008285 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008286 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008287 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008288 *tofree = NULL;
8289 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008291
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008292 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008293#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008294 *tofree = NULL;
8295 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8296 r = numbuf;
8297 break;
8298#endif
8299
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008300 case VAR_SPECIAL:
8301 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008302 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008303 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008304 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305
Bram Moolenaar8502c702014-06-17 12:51:16 +02008306 if (--recurse == 0)
8307 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008308 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008309}
8310
8311/*
8312 * Return a string with the string representation of a variable.
8313 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8314 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008315 * Does not put quotes around strings, as ":echo" displays values.
8316 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8317 * May return NULL.
8318 */
8319 static char_u *
8320echo_string(
8321 typval_T *tv,
8322 char_u **tofree,
8323 char_u *numbuf,
8324 int copyID)
8325{
8326 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8327}
8328
8329/*
8330 * Return a string with the string representation of a variable.
8331 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8332 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008333 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008334 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008335 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008337tv2string(
8338 typval_T *tv,
8339 char_u **tofree,
8340 char_u *numbuf,
8341 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008342{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008343 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344}
8345
8346/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 * Return string "str" in ' quotes, doubling ' characters.
8348 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008349 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008350 */
8351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008352string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008353{
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008355 char_u *p, *r, *s;
8356
Bram Moolenaar33570922005-01-25 22:26:29 +00008357 len = (function ? 13 : 3);
8358 if (str != NULL)
8359 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008360 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 for (p = str; *p != NUL; mb_ptr_adv(p))
8362 if (*p == '\'')
8363 ++len;
8364 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008365 s = r = alloc(len);
8366 if (r != NULL)
8367 {
8368 if (function)
8369 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008370 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008371 r += 10;
8372 }
8373 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008374 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 if (str != NULL)
8376 for (p = str; *p != NUL; )
8377 {
8378 if (*p == '\'')
8379 *r++ = '\'';
8380 MB_COPY_CHAR(p, r);
8381 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008382 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008383 if (function)
8384 *r++ = ')';
8385 *r++ = NUL;
8386 }
8387 return s;
8388}
8389
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008390#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008391/*
8392 * Convert the string "text" to a floating point number.
8393 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8394 * this always uses a decimal point.
8395 * Returns the length of the text that was consumed.
8396 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008398string2float(
8399 char_u *text,
8400 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008401{
8402 char *s = (char *)text;
8403 float_T f;
8404
8405 f = strtod(s, &s);
8406 *value = f;
8407 return (int)((char_u *)s - text);
8408}
8409#endif
8410
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 * Get the value of an environment variable.
8413 * "arg" is pointing to the '$'. It is advanced to after the name.
8414 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008415 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 */
8417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008418get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 char_u *string = NULL;
8421 int len;
8422 int cc;
8423 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008424 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425
8426 ++*arg;
8427 name = *arg;
8428 len = get_env_len(arg);
8429 if (evaluate)
8430 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008431 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008432 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008433
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008434 cc = name[len];
8435 name[len] = NUL;
8436 /* first try vim_getenv(), fast for normal environment vars */
8437 string = vim_getenv(name, &mustfree);
8438 if (string != NULL && *string != NUL)
8439 {
8440 if (!mustfree)
8441 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008443 else
8444 {
8445 if (mustfree)
8446 vim_free(string);
8447
8448 /* next try expanding things like $VIM and ${HOME} */
8449 string = expand_env_save(name - 1);
8450 if (string != NULL && *string == '$')
8451 {
8452 vim_free(string);
8453 string = NULL;
8454 }
8455 }
8456 name[len] = cc;
8457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 rettv->v_type = VAR_STRING;
8459 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 }
8461
8462 return OK;
8463}
8464
8465/*
8466 * Array with names and number of arguments of all internal functions
8467 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8468 */
8469static struct fst
8470{
8471 char *f_name; /* function name */
8472 char f_min_argc; /* minimal number of arguments */
8473 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008474 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008475 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476} functions[] =
8477{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008478#ifdef FEAT_FLOAT
8479 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008480 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008481#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008482 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008483 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"append", 2, 2, f_append},
8485 {"argc", 0, 0, f_argc},
8486 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008487 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008488 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008489#ifdef FEAT_FLOAT
8490 {"asin", 1, 1, f_asin}, /* WJMc */
8491#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008492 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008493 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008494 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008495 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008496 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008497 {"assert_notequal", 2, 3, f_assert_notequal},
8498 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008499 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008500#ifdef FEAT_FLOAT
8501 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008502 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008505 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"bufexists", 1, 1, f_bufexists},
8507 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8508 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8509 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8510 {"buflisted", 1, 1, f_buflisted},
8511 {"bufloaded", 1, 1, f_bufloaded},
8512 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008513 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008514 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {"bufwinnr", 1, 1, f_bufwinnr},
8516 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008517 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008518 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008520#ifdef FEAT_FLOAT
8521 {"ceil", 1, 1, f_ceil},
8522#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008523#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008524 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008525 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8526 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008527 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008528 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008529 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008530 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008531 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008532 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008533 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008534 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008535 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8536 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008537 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008538 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008539#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008540 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008541 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008543 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008545#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008546 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008547 {"complete_add", 1, 1, f_complete_add},
8548 {"complete_check", 0, 0, f_complete_check},
8549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008552#ifdef FEAT_FLOAT
8553 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008554 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008555#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008556 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008558 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008559 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008560 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008562 {"diff_filler", 1, 1, f_diff_filler},
8563 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008564 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008566 {"eval", 1, 1, f_eval},
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02008567 {"evalcmd", 1, 1, f_evalcmd},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {"eventhandler", 0, 0, f_eventhandler},
8569 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008570 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008572#ifdef FEAT_FLOAT
8573 {"exp", 1, 1, f_exp},
8574#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008575 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008576 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008577 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8579 {"filereadable", 1, 1, f_filereadable},
8580 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008581 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008582 {"finddir", 1, 3, f_finddir},
8583 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008584#ifdef FEAT_FLOAT
8585 {"float2nr", 1, 1, f_float2nr},
8586 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008587 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008588#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008589 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 {"fnamemodify", 2, 2, f_fnamemodify},
8591 {"foldclosed", 1, 1, f_foldclosed},
8592 {"foldclosedend", 1, 1, f_foldclosedend},
8593 {"foldlevel", 1, 1, f_foldlevel},
8594 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008595 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008597 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008598 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008599 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008600 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008601 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {"getchar", 0, 1, f_getchar},
8603 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008604 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 {"getcmdline", 0, 0, f_getcmdline},
8606 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008607 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008608 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008609 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008610 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008611 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008612 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {"getfsize", 1, 1, f_getfsize},
8614 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008615 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008616 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008617 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008618 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008619 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008620 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008621 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008622 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008624 {"gettabvar", 2, 3, f_gettabvar},
8625 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {"getwinposx", 0, 0, f_getwinposx},
8627 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008628 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008629 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008630 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008631 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008634 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008635 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8637 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8638 {"histadd", 2, 2, f_histadd},
8639 {"histdel", 1, 2, f_histdel},
8640 {"histget", 1, 2, f_histget},
8641 {"histnr", 1, 1, f_histnr},
8642 {"hlID", 1, 1, f_hlID},
8643 {"hlexists", 1, 1, f_hlexists},
8644 {"hostname", 0, 0, f_hostname},
8645 {"iconv", 3, 3, f_iconv},
8646 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008647 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008648 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008650 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 {"inputrestore", 0, 0, f_inputrestore},
8652 {"inputsave", 0, 0, f_inputsave},
8653 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008655 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008657 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008658#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8659 {"isnan", 1, 1, f_isnan},
8660#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008661 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008662#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008663 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008664 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008665 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008666 {"job_start", 1, 2, f_job_start},
8667 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008668 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008669#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008670 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008671 {"js_decode", 1, 1, f_js_decode},
8672 {"js_encode", 1, 1, f_js_encode},
8673 {"json_decode", 1, 1, f_json_decode},
8674 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008675 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008677 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {"libcall", 3, 3, f_libcall},
8679 {"libcallnr", 3, 3, f_libcallnr},
8680 {"line", 1, 1, f_line},
8681 {"line2byte", 1, 1, f_line2byte},
8682 {"lispindent", 1, 1, f_lispindent},
8683 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008684#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008685 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686 {"log10", 1, 1, f_log10},
8687#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008688#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008689 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008690#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008691 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008692 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008693 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008694 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008695 {"matchadd", 2, 5, f_matchadd},
8696 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008697 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008698 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008699 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008700 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008701 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008702 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008703 {"max", 1, 1, f_max},
8704 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008705#ifdef vim_mkdir
8706 {"mkdir", 1, 3, f_mkdir},
8707#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008708 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008709#ifdef FEAT_MZSCHEME
8710 {"mzeval", 1, 1, f_mzeval},
8711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008713 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008714 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008715 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008716#ifdef FEAT_PERL
8717 {"perleval", 1, 1, f_perleval},
8718#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008719#ifdef FEAT_FLOAT
8720 {"pow", 2, 2, f_pow},
8721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008723 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008724 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008725#ifdef FEAT_PYTHON3
8726 {"py3eval", 1, 1, f_py3eval},
8727#endif
8728#ifdef FEAT_PYTHON
8729 {"pyeval", 1, 1, f_pyeval},
8730#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008731 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008732 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008733 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008734#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008735 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008736#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008737 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {"remote_expr", 2, 3, f_remote_expr},
8739 {"remote_foreground", 1, 1, f_remote_foreground},
8740 {"remote_peek", 1, 2, f_remote_peek},
8741 {"remote_read", 1, 1, f_remote_read},
8742 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008743 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008745 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008748#ifdef FEAT_FLOAT
8749 {"round", 1, 1, f_round},
8750#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008751 {"screenattr", 2, 2, f_screenattr},
8752 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008753 {"screencol", 0, 0, f_screencol},
8754 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008755 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008756 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008757 {"searchpair", 3, 7, f_searchpair},
8758 {"searchpairpos", 3, 7, f_searchpairpos},
8759 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {"server2client", 2, 2, f_server2client},
8761 {"serverlist", 0, 0, f_serverlist},
8762 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008763 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008765 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008767 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008768 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008769 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008770 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008772 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008773 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008775#ifdef FEAT_CRYPT
8776 {"sha256", 1, 1, f_sha256},
8777#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008778 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008779 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781#ifdef FEAT_FLOAT
8782 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008783 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008784#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008785 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008786 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008787 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008788 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008789 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008790#ifdef FEAT_FLOAT
8791 {"sqrt", 1, 1, f_sqrt},
8792 {"str2float", 1, 1, f_str2float},
8793#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008794 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008795 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008796 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008797 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798#ifdef HAVE_STRFTIME
8799 {"strftime", 1, 2, f_strftime},
8800#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008801 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008803 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {"strlen", 1, 1, f_strlen},
8805 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008806 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008808 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008809 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {"substitute", 4, 4, f_substitute},
8811 {"synID", 3, 3, f_synID},
8812 {"synIDattr", 2, 3, f_synIDattr},
8813 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008814 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008815 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008816 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008817 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008818 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008819 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008820 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008821 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008822 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008823#ifdef FEAT_FLOAT
8824 {"tan", 1, 1, f_tan},
8825 {"tanh", 1, 1, f_tanh},
8826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008828 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8829 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008830 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8831#ifdef FEAT_JOB_CHANNEL
8832 {"test_null_channel", 0, 0, f_test_null_channel},
8833#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008834 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008835#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008836 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008837#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008838 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008839 {"test_null_partial", 0, 0, f_test_null_partial},
8840 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008841 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008842#ifdef FEAT_TIMERS
8843 {"timer_start", 2, 3, f_timer_start},
8844 {"timer_stop", 1, 1, f_timer_stop},
8845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {"tolower", 1, 1, f_tolower},
8847 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008848 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008849#ifdef FEAT_FLOAT
8850 {"trunc", 1, 1, f_trunc},
8851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008853 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008854 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008855 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008856 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {"virtcol", 1, 1, f_virtcol},
8858 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008859 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008860 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008861 {"win_getid", 0, 2, f_win_getid},
8862 {"win_gotoid", 1, 1, f_win_gotoid},
8863 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8864 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {"winbufnr", 1, 1, f_winbufnr},
8866 {"wincol", 0, 0, f_wincol},
8867 {"winheight", 1, 1, f_winheight},
8868 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008869 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008871 {"winrestview", 1, 1, f_winrestview},
8872 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008874 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008875 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008876 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877};
8878
8879#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8880
8881/*
8882 * Function given to ExpandGeneric() to obtain the list of internal
8883 * or user defined function names.
8884 */
8885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008886get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 static int intidx = -1;
8889 char_u *name;
8890
8891 if (idx == 0)
8892 intidx = -1;
8893 if (intidx < 0)
8894 {
8895 name = get_user_func_name(xp, idx);
8896 if (name != NULL)
8897 return name;
8898 }
8899 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8900 {
8901 STRCPY(IObuff, functions[intidx].f_name);
8902 STRCAT(IObuff, "(");
8903 if (functions[intidx].f_max_argc == 0)
8904 STRCAT(IObuff, ")");
8905 return IObuff;
8906 }
8907
8908 return NULL;
8909}
8910
8911/*
8912 * Function given to ExpandGeneric() to obtain the list of internal or
8913 * user defined variable or function names.
8914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008916get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 static int intidx = -1;
8919 char_u *name;
8920
8921 if (idx == 0)
8922 intidx = -1;
8923 if (intidx < 0)
8924 {
8925 name = get_function_name(xp, idx);
8926 if (name != NULL)
8927 return name;
8928 }
8929 return get_user_var_name(xp, ++intidx);
8930}
8931
8932#endif /* FEAT_CMDL_COMPL */
8933
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008934#if defined(EBCDIC) || defined(PROTO)
8935/*
8936 * Compare struct fst by function name.
8937 */
8938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008939compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008940{
8941 struct fst *p1 = (struct fst *)s1;
8942 struct fst *p2 = (struct fst *)s2;
8943
8944 return STRCMP(p1->f_name, p2->f_name);
8945}
8946
8947/*
8948 * Sort the function table by function name.
8949 * The sorting of the table above is ASCII dependant.
8950 * On machines using EBCDIC we have to sort it.
8951 */
8952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008953sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008954{
8955 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8956
8957 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8958}
8959#endif
8960
8961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962/*
8963 * Find internal function in table above.
8964 * Return index, or -1 if not found
8965 */
8966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008967find_internal_func(
8968 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970 int first = 0;
8971 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8972 int cmp;
8973 int x;
8974
8975 /*
8976 * Find the function name in the table. Binary search.
8977 */
8978 while (first <= last)
8979 {
8980 x = first + ((unsigned)(last - first) >> 1);
8981 cmp = STRCMP(name, functions[x].f_name);
8982 if (cmp < 0)
8983 last = x - 1;
8984 else if (cmp > 0)
8985 first = x + 1;
8986 else
8987 return x;
8988 }
8989 return -1;
8990}
8991
8992/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8994 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008995 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8996 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 */
8998 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008999deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000{
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009002 int cc;
9003
Bram Moolenaar65639032016-03-16 21:40:30 +01009004 if (partialp != NULL)
9005 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009006
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009007 cc = name[*lenp];
9008 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01009009 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 {
9015 *lenp = 0;
9016 return (char_u *)""; /* just in case */
9017 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009018 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 }
9021
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009022 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9023 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009024 partial_T *pt = v->di_tv.vval.v_partial;
9025
9026 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009027 {
9028 *lenp = 0;
9029 return (char_u *)""; /* just in case */
9030 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009031 if (partialp != NULL)
9032 *partialp = pt;
9033 *lenp = (int)STRLEN(pt->pt_name);
9034 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009035 }
9036
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009037 return name;
9038}
9039
9040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 * Allocate a variable for the result of a function.
9042 * Return OK or FAIL.
9043 */
9044 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009045get_func_tv(
9046 char_u *name, /* name of the function */
9047 int len, /* length of "name" */
9048 typval_T *rettv,
9049 char_u **arg, /* argument, pointing to the '(' */
9050 linenr_T firstline, /* first line of range */
9051 linenr_T lastline, /* last line of range */
9052 int *doesrange, /* return: function handled range */
9053 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009054 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009055 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057 char_u *argp;
9058 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009059 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 int argcount = 0; /* number of arguments found */
9061
9062 /*
9063 * Get the arguments.
9064 */
9065 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009066 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 {
9068 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9069 if (*argp == ')' || *argp == ',' || *argp == NUL)
9070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9072 {
9073 ret = FAIL;
9074 break;
9075 }
9076 ++argcount;
9077 if (*argp != ',')
9078 break;
9079 }
9080 if (*argp == ')')
9081 ++argp;
9082 else
9083 ret = FAIL;
9084
9085 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009086 {
9087 int i = 0;
9088
9089 if (get_vim_var_nr(VV_TESTING))
9090 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009091 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009092 * what variables are used on the call stack. */
9093 if (funcargs.ga_itemsize == 0)
9094 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9095 for (i = 0; i < argcount; ++i)
9096 if (ga_grow(&funcargs, 1) == OK)
9097 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9098 &argvars[i];
9099 }
9100
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009102 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009103
9104 funcargs.ga_len -= i;
9105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 {
9108 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009109 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009111 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
9114 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116
9117 *arg = skipwhite(argp);
9118 return ret;
9119}
9120
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009121#define ERROR_UNKNOWN 0
9122#define ERROR_TOOMANY 1
9123#define ERROR_TOOFEW 2
9124#define ERROR_SCRIPT 3
9125#define ERROR_DICT 4
9126#define ERROR_NONE 5
9127#define ERROR_OTHER 6
9128#define FLEN_FIXED 40
9129
9130/*
9131 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9132 * Change <SNR>123_name() to K_SNR 123_name().
9133 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9134 * (slow).
9135 */
9136 static char_u *
9137fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9138{
9139 int llen;
9140 char_u *fname;
9141 int i;
9142
9143 llen = eval_fname_script(name);
9144 if (llen > 0)
9145 {
9146 fname_buf[0] = K_SPECIAL;
9147 fname_buf[1] = KS_EXTRA;
9148 fname_buf[2] = (int)KE_SNR;
9149 i = 3;
9150 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9151 {
9152 if (current_SID <= 0)
9153 *error = ERROR_SCRIPT;
9154 else
9155 {
9156 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9157 i = (int)STRLEN(fname_buf);
9158 }
9159 }
9160 if (i + STRLEN(name + llen) < FLEN_FIXED)
9161 {
9162 STRCPY(fname_buf + i, name + llen);
9163 fname = fname_buf;
9164 }
9165 else
9166 {
9167 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9168 if (fname == NULL)
9169 *error = ERROR_OTHER;
9170 else
9171 {
9172 *tofree = fname;
9173 mch_memmove(fname, fname_buf, (size_t)i);
9174 STRCPY(fname + i, name + llen);
9175 }
9176 }
9177 }
9178 else
9179 fname = name;
9180 return fname;
9181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
9183/*
9184 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009185 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009186 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009189call_func(
9190 char_u *funcname, /* name of the function */
9191 int len, /* length of "name" */
9192 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009193 int argcount_in, /* number of "argvars" */
9194 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009195 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009196 linenr_T firstline, /* first line of range */
9197 linenr_T lastline, /* last line of range */
9198 int *doesrange, /* return: function handled range */
9199 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009200 partial_T *partial, /* optional, can be NULL */
9201 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 int error = ERROR_NONE;
9205 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009208 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009210 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009211 int argcount = argcount_in;
9212 typval_T *argvars = argvars_in;
9213 dict_T *selfdict = selfdict_in;
9214 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9215 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009216
9217 /* Make a copy of the name, if it comes from a funcref variable it could
9218 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009219 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009220 if (name == NULL)
9221 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009223 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
9225 *doesrange = FALSE;
9226
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009227 if (partial != NULL)
9228 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009229 /* When the function has a partial with a dict and there is a dict
9230 * argument, use the dict argument. That is backwards compatible.
9231 * When the dict was bound explicitly use the one from the partial. */
9232 if (partial->pt_dict != NULL
9233 && (selfdict_in == NULL || !partial->pt_auto))
9234 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009235 if (error == ERROR_NONE && partial->pt_argc > 0)
9236 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009237 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9238 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9239 for (i = 0; i < argcount_in; ++i)
9240 argv[i + argv_clear] = argvars_in[i];
9241 argvars = argv;
9242 argcount = partial->pt_argc + argcount_in;
9243 }
9244 }
9245
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246
9247 /* execute the function if no errors detected and executing */
9248 if (evaluate && error == ERROR_NONE)
9249 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009250 char_u *rfname = fname;
9251
9252 /* Ignore "g:" before a function name. */
9253 if (fname[0] == 'g' && fname[1] == ':')
9254 rfname = fname + 2;
9255
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009256 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9257 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 error = ERROR_UNKNOWN;
9259
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009260 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 {
9262 /*
9263 * User defined function.
9264 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009265 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009266
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009268 /* Trigger FuncUndefined event, may load the function. */
9269 if (fp == NULL
9270 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009271 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009272 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009274 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009275 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 }
9277#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009278 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009279 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009280 {
9281 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009282 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009283 }
9284
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 if (fp != NULL)
9286 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009287 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009289 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009291 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009293 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009294 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 else
9296 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009297 int did_save_redo = FALSE;
9298
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 /*
9300 * Call the user function.
9301 * Save and restore search patterns, script variables and
9302 * redo buffer.
9303 */
9304 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009305#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009306 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009307#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009308 {
9309 saveRedobuff();
9310 did_save_redo = TRUE;
9311 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009312 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009313 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009314 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009315 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9316 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9317 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009318 /* Function was unreferenced while being used, free it
9319 * now. */
9320 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009321 if (did_save_redo)
9322 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 restore_search_patterns();
9324 error = ERROR_NONE;
9325 }
9326 }
9327 }
9328 else
9329 {
9330 /*
9331 * Find the function name in the table, call its implementation.
9332 */
9333 i = find_internal_func(fname);
9334 if (i >= 0)
9335 {
9336 if (argcount < functions[i].f_min_argc)
9337 error = ERROR_TOOFEW;
9338 else if (argcount > functions[i].f_max_argc)
9339 error = ERROR_TOOMANY;
9340 else
9341 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009342 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 error = ERROR_NONE;
9345 }
9346 }
9347 }
9348 /*
9349 * The function call (or "FuncUndefined" autocommand sequence) might
9350 * have been aborted by an error, an interrupt, or an explicitly thrown
9351 * exception that has not been caught so far. This situation can be
9352 * tested for by calling aborting(). For an error in an internal
9353 * function or for the "E132" error in call_user_func(), however, the
9354 * throw point at which the "force_abort" flag (temporarily reset by
9355 * emsg()) is normally updated has not been reached yet. We need to
9356 * update that flag first to make aborting() reliable.
9357 */
9358 update_force_abort();
9359 }
9360 if (error == ERROR_NONE)
9361 ret = OK;
9362
9363 /*
9364 * Report an error unless the argument evaluation or function call has been
9365 * cancelled due to an aborting error, an interrupt, or an exception.
9366 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009367 if (!aborting())
9368 {
9369 switch (error)
9370 {
9371 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009372 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009373 break;
9374 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009375 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009376 break;
9377 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009378 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009379 name);
9380 break;
9381 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009382 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009383 name);
9384 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009385 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009386 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387 name);
9388 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009389 }
9390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009392 while (argv_clear > 0)
9393 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009394 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009395 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396
9397 return ret;
9398}
9399
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009400/*
9401 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009402 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009403 */
9404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009405emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009406{
9407 char_u *p;
9408
9409 if (*name == K_SPECIAL)
9410 p = concat_str((char_u *)"<SNR>", name + 3);
9411 else
9412 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009413 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009414 if (p != name)
9415 vim_free(p);
9416}
9417
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009418/*
9419 * Return TRUE for a non-zero Number and a non-empty String.
9420 */
9421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009422non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009423{
9424 return ((argvars[0].v_type == VAR_NUMBER
9425 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02009426 || (argvars[0].v_type == VAR_SPECIAL
9427 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009428 || (argvars[0].v_type == VAR_STRING
9429 && argvars[0].vval.v_string != NULL
9430 && *argvars[0].vval.v_string != NUL));
9431}
9432
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433/*********************************************
9434 * Implementation of the built-in functions
9435 */
9436
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009437#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009438static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009439
9440/*
9441 * Get the float value of "argvars[0]" into "f".
9442 * Returns FAIL when the argument is not a Number or Float.
9443 */
9444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009445get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009446{
9447 if (argvars[0].v_type == VAR_FLOAT)
9448 {
9449 *f = argvars[0].vval.v_float;
9450 return OK;
9451 }
9452 if (argvars[0].v_type == VAR_NUMBER)
9453 {
9454 *f = (float_T)argvars[0].vval.v_number;
9455 return OK;
9456 }
9457 EMSG(_("E808: Number or Float required"));
9458 return FAIL;
9459}
9460
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009461/*
9462 * "abs(expr)" function
9463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009466{
9467 if (argvars[0].v_type == VAR_FLOAT)
9468 {
9469 rettv->v_type = VAR_FLOAT;
9470 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9471 }
9472 else
9473 {
9474 varnumber_T n;
9475 int error = FALSE;
9476
9477 n = get_tv_number_chk(&argvars[0], &error);
9478 if (error)
9479 rettv->vval.v_number = -1;
9480 else if (n > 0)
9481 rettv->vval.v_number = n;
9482 else
9483 rettv->vval.v_number = -n;
9484 }
9485}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009486
9487/*
9488 * "acos()" function
9489 */
9490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009492{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009493 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009494
9495 rettv->v_type = VAR_FLOAT;
9496 if (get_float_arg(argvars, &f) == OK)
9497 rettv->vval.v_float = acos(f);
9498 else
9499 rettv->vval.v_float = 0.0;
9500}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009501#endif
9502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009504 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 */
9506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009507f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaar33570922005-01-25 22:26:29 +00009509 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009514 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009515 && !tv_check_lock(l->lv_lock,
9516 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009517 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009519 }
9520 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009521 EMSG(_(e_listreq));
9522}
9523
9524/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009525 * "and(expr, expr)" function
9526 */
9527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009529{
9530 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9531 & get_tv_number_chk(&argvars[1], NULL);
9532}
9533
9534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009535 * "append(lnum, string/list)" function
9536 */
9537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009538f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009539{
9540 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009541 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009542 list_T *l = NULL;
9543 listitem_T *li = NULL;
9544 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009545 long added = 0;
9546
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009547 /* When coming here from Insert mode, sync undo, so that this can be
9548 * undone separately from what was previously inserted. */
9549 if (u_sync_once == 2)
9550 {
9551 u_sync_once = 1; /* notify that u_sync() was called */
9552 u_sync(TRUE);
9553 }
9554
Bram Moolenaar0d660222005-01-07 21:51:51 +00009555 lnum = get_tv_lnum(argvars);
9556 if (lnum >= 0
9557 && lnum <= curbuf->b_ml.ml_line_count
9558 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009560 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009561 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009562 l = argvars[1].vval.v_list;
9563 if (l == NULL)
9564 return;
9565 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009566 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009567 for (;;)
9568 {
9569 if (l == NULL)
9570 tv = &argvars[1]; /* append a string */
9571 else if (li == NULL)
9572 break; /* end of list */
9573 else
9574 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 line = get_tv_string_chk(tv);
9576 if (line == NULL) /* type error */
9577 {
9578 rettv->vval.v_number = 1; /* Failed */
9579 break;
9580 }
9581 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009582 ++added;
9583 if (l == NULL)
9584 break;
9585 li = li->li_next;
9586 }
9587
9588 appended_lines_mark(lnum, added);
9589 if (curwin->w_cursor.lnum > lnum)
9590 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 else
9593 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594}
9595
9596/*
9597 * "argc()" function
9598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009600f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * "argidx()" function
9607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009609f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612}
9613
9614/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009615 * "arglistid()" function
9616 */
9617 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009618f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009619{
9620 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009621
9622 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009623 wp = find_tabwin(&argvars[0], &argvars[1]);
9624 if (wp != NULL)
9625 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009626}
9627
9628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 * "argv(nr)" function
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 int idx;
9635
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009636 if (argvars[0].v_type != VAR_UNKNOWN)
9637 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009638 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009639 if (idx >= 0 && idx < ARGCOUNT)
9640 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9641 else
9642 rettv->vval.v_string = NULL;
9643 rettv->v_type = VAR_STRING;
9644 }
9645 else if (rettv_list_alloc(rettv) == OK)
9646 for (idx = 0; idx < ARGCOUNT; ++idx)
9647 list_append_string(rettv->vval.v_list,
9648 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649}
9650
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009651typedef enum
9652{
9653 ASSERT_EQUAL,
9654 ASSERT_NOTEQUAL,
9655 ASSERT_MATCH,
9656 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009657 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009658} assert_type_T;
9659
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009660static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009661static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009662static void assert_error(garray_T *gap);
9663static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009664
9665/*
9666 * Prepare "gap" for an assert error and add the sourcing position.
9667 */
9668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009669prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009670{
9671 char buf[NUMBUFLEN];
9672
9673 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009674 if (sourcing_name != NULL)
9675 {
9676 ga_concat(gap, sourcing_name);
9677 if (sourcing_lnum > 0)
9678 ga_concat(gap, (char_u *)" ");
9679 }
9680 if (sourcing_lnum > 0)
9681 {
9682 sprintf(buf, "line %ld", (long)sourcing_lnum);
9683 ga_concat(gap, (char_u *)buf);
9684 }
9685 if (sourcing_name != NULL || sourcing_lnum > 0)
9686 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009687}
9688
9689/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009690 * Append "str" to "gap", escaping unprintable characters.
9691 * Changes NL to \n, CR to \r, etc.
9692 */
9693 static void
9694ga_concat_esc(garray_T *gap, char_u *str)
9695{
9696 char_u *p;
9697 char_u buf[NUMBUFLEN];
9698
Bram Moolenaarf1551962016-03-15 12:55:58 +01009699 if (str == NULL)
9700 {
9701 ga_concat(gap, (char_u *)"NULL");
9702 return;
9703 }
9704
Bram Moolenaar23689172016-02-15 22:37:37 +01009705 for (p = str; *p != NUL; ++p)
9706 switch (*p)
9707 {
9708 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9709 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9710 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9711 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9712 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9713 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9714 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9715 default:
9716 if (*p < ' ')
9717 {
9718 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9719 ga_concat(gap, buf);
9720 }
9721 else
9722 ga_append(gap, *p);
9723 break;
9724 }
9725}
9726
9727/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009728 * Fill "gap" with information about an assert error.
9729 */
9730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009731fill_assert_error(
9732 garray_T *gap,
9733 typval_T *opt_msg_tv,
9734 char_u *exp_str,
9735 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009736 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009737 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009738{
9739 char_u numbuf[NUMBUFLEN];
9740 char_u *tofree;
9741
9742 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9743 {
9744 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9745 vim_free(tofree);
9746 }
9747 else
9748 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009749 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009750 ga_concat(gap, (char_u *)"Pattern ");
9751 else
9752 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009753 if (exp_str == NULL)
9754 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009755 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009756 vim_free(tofree);
9757 }
9758 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009759 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009760 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009761 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009762 else if (atype == ASSERT_NOTMATCH)
9763 ga_concat(gap, (char_u *)" does match ");
9764 else if (atype == ASSERT_NOTEQUAL)
9765 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009766 else
9767 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009768 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009769 vim_free(tofree);
9770 }
9771}
Bram Moolenaar43345542015-11-29 17:35:35 +01009772
9773/*
9774 * Add an assert error to v:errors.
9775 */
9776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009778{
9779 struct vimvar *vp = &vimvars[VV_ERRORS];
9780
9781 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9782 /* Make sure v:errors is a list. */
9783 set_vim_var_list(VV_ERRORS, list_alloc());
9784 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9785}
9786
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009787 static void
9788assert_equal_common(typval_T *argvars, assert_type_T atype)
9789{
9790 garray_T ga;
9791
9792 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9793 != (atype == ASSERT_EQUAL))
9794 {
9795 prepare_assert_error(&ga);
9796 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9797 atype);
9798 assert_error(&ga);
9799 ga_clear(&ga);
9800 }
9801}
9802
Bram Moolenaar43345542015-11-29 17:35:35 +01009803/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009804 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009805 */
9806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009807f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009808{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009809 assert_equal_common(argvars, ASSERT_EQUAL);
9810}
Bram Moolenaar43345542015-11-29 17:35:35 +01009811
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009812/*
9813 * "assert_notequal(expected, actual[, msg])" function
9814 */
9815 static void
9816f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9817{
9818 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009819}
9820
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009821/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009822 * "assert_exception(string[, msg])" function
9823 */
9824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009825f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009826{
9827 garray_T ga;
9828 char *error;
9829
9830 error = (char *)get_tv_string_chk(&argvars[0]);
9831 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9832 {
9833 prepare_assert_error(&ga);
9834 ga_concat(&ga, (char_u *)"v:exception is not set");
9835 assert_error(&ga);
9836 ga_clear(&ga);
9837 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009838 else if (error != NULL
9839 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009840 {
9841 prepare_assert_error(&ga);
9842 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009843 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009844 assert_error(&ga);
9845 ga_clear(&ga);
9846 }
9847}
9848
9849/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009850 * "assert_fails(cmd [, error])" function
9851 */
9852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009853f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009854{
9855 char_u *cmd = get_tv_string_chk(&argvars[0]);
9856 garray_T ga;
9857
9858 called_emsg = FALSE;
9859 suppress_errthrow = TRUE;
9860 emsg_silent = TRUE;
9861 do_cmdline_cmd(cmd);
9862 if (!called_emsg)
9863 {
9864 prepare_assert_error(&ga);
9865 ga_concat(&ga, (char_u *)"command did not fail: ");
9866 ga_concat(&ga, cmd);
9867 assert_error(&ga);
9868 ga_clear(&ga);
9869 }
9870 else if (argvars[1].v_type != VAR_UNKNOWN)
9871 {
9872 char_u buf[NUMBUFLEN];
9873 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9874
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009875 if (error == NULL
9876 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009877 {
9878 prepare_assert_error(&ga);
9879 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009880 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009881 assert_error(&ga);
9882 ga_clear(&ga);
9883 }
9884 }
9885
9886 called_emsg = FALSE;
9887 suppress_errthrow = FALSE;
9888 emsg_silent = FALSE;
9889 emsg_on_display = FALSE;
9890 set_vim_var_string(VV_ERRMSG, NULL, 0);
9891}
9892
9893/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009894 * Common for assert_true() and assert_false().
9895 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009897assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009898{
9899 int error = FALSE;
9900 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009901
Bram Moolenaar37127922016-02-06 20:29:28 +01009902 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009903 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009904 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009905 if (argvars[0].v_type != VAR_NUMBER
9906 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9907 || error)
9908 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009909 prepare_assert_error(&ga);
9910 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009911 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009912 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009913 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009914 ga_clear(&ga);
9915 }
9916}
9917
9918/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009919 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009920 */
9921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009923{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009924 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009925}
9926
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009927 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009928assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009929{
9930 garray_T ga;
9931 char_u buf1[NUMBUFLEN];
9932 char_u buf2[NUMBUFLEN];
9933 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9934 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9935
Bram Moolenaar72188e92016-03-28 22:48:29 +02009936 if (pat == NULL || text == NULL)
9937 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009938 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009939 {
9940 prepare_assert_error(&ga);
9941 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009942 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009943 assert_error(&ga);
9944 ga_clear(&ga);
9945 }
9946}
9947
9948/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009949 * "assert_match(pattern, actual[, msg])" function
9950 */
9951 static void
9952f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9953{
9954 assert_match_common(argvars, ASSERT_MATCH);
9955}
9956
9957/*
9958 * "assert_notmatch(pattern, actual[, msg])" function
9959 */
9960 static void
9961f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9962{
9963 assert_match_common(argvars, ASSERT_NOTMATCH);
9964}
9965
9966/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009967 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009968 */
9969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009970f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009971{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009972 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009973}
9974
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009976/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009977 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009978 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009980f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009981{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009982 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009983
9984 rettv->v_type = VAR_FLOAT;
9985 if (get_float_arg(argvars, &f) == OK)
9986 rettv->vval.v_float = asin(f);
9987 else
9988 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009989}
9990
9991/*
9992 * "atan()" function
9993 */
9994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009995f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009996{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009997 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009998
9999 rettv->v_type = VAR_FLOAT;
10000 if (get_float_arg(argvars, &f) == OK)
10001 rettv->vval.v_float = atan(f);
10002 else
10003 rettv->vval.v_float = 0.0;
10004}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010005
10006/*
10007 * "atan2()" function
10008 */
10009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010010f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010011{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010012 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010013
10014 rettv->v_type = VAR_FLOAT;
10015 if (get_float_arg(argvars, &fx) == OK
10016 && get_float_arg(&argvars[1], &fy) == OK)
10017 rettv->vval.v_float = atan2(fx, fy);
10018 else
10019 rettv->vval.v_float = 0.0;
10020}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010021#endif
10022
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023/*
10024 * "browse(save, title, initdir, default)" function
10025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010027f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
10029#ifdef FEAT_BROWSE
10030 int save;
10031 char_u *title;
10032 char_u *initdir;
10033 char_u *defname;
10034 char_u buf[NUMBUFLEN];
10035 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010036 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010038 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010039 title = get_tv_string_chk(&argvars[1]);
10040 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10041 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043 if (error || title == NULL || initdir == NULL || defname == NULL)
10044 rettv->vval.v_string = NULL;
10045 else
10046 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010047 do_browse(save ? BROWSE_SAVE : 0,
10048 title, defname, NULL, initdir, NULL, curbuf);
10049#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010051#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010053}
10054
10055/*
10056 * "browsedir(title, initdir)" function
10057 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010059f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010060{
10061#ifdef FEAT_BROWSE
10062 char_u *title;
10063 char_u *initdir;
10064 char_u buf[NUMBUFLEN];
10065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010066 title = get_tv_string_chk(&argvars[0]);
10067 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 if (title == NULL || initdir == NULL)
10070 rettv->vval.v_string = NULL;
10071 else
10072 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010073 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010080static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010081
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082/*
10083 * Find a buffer by number or exact name.
10084 */
10085 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010086find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010090 if (avar->v_type == VAR_NUMBER)
10091 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010092 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010094 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010095 if (buf == NULL)
10096 {
10097 /* No full path name match, try a match with a URL or a "nofile"
10098 * buffer, these don't use the full path. */
10099 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10100 if (buf->b_fname != NULL
10101 && (path_with_url(buf->b_fname)
10102#ifdef FEAT_QUICKFIX
10103 || bt_nofile(buf)
10104#endif
10105 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010106 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010107 break;
10108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 }
10110 return buf;
10111}
10112
10113/*
10114 * "bufexists(expr)" function
10115 */
10116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010117f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120}
10121
10122/*
10123 * "buflisted(expr)" function
10124 */
10125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010126f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128 buf_T *buf;
10129
10130 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132}
10133
10134/*
10135 * "bufloaded(expr)" function
10136 */
10137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010138f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
10140 buf_T *buf;
10141
10142 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144}
10145
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010146 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010147buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 int save_magic;
10150 char_u *save_cpo;
10151 buf_T *buf;
10152
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10154 save_magic = p_magic;
10155 p_magic = TRUE;
10156 save_cpo = p_cpo;
10157 p_cpo = (char_u *)"";
10158
10159 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010160 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
10162 p_magic = save_magic;
10163 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010164 return buf;
10165}
10166
10167/*
10168 * Get buffer by number or pattern.
10169 */
10170 static buf_T *
10171get_buf_tv(typval_T *tv, int curtab_only)
10172{
10173 char_u *name = tv->vval.v_string;
10174 buf_T *buf;
10175
10176 if (tv->v_type == VAR_NUMBER)
10177 return buflist_findnr((int)tv->vval.v_number);
10178 if (tv->v_type != VAR_STRING)
10179 return NULL;
10180 if (name == NULL || *name == NUL)
10181 return curbuf;
10182 if (name[0] == '$' && name[1] == NUL)
10183 return lastbuf;
10184
10185 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
10187 /* If not found, try expanding the name, like done for bufexists(). */
10188 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190
10191 return buf;
10192}
10193
10194/*
10195 * "bufname(expr)" function
10196 */
10197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010198f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199{
10200 buf_T *buf;
10201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010204 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010209 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 --emsg_off;
10211}
10212
10213/*
10214 * "bufnr(expr)" function
10215 */
10216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010217f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218{
10219 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010220 int error = FALSE;
10221 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010225 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010226 --emsg_off;
10227
10228 /* If the buffer isn't found and the second argument is not zero create a
10229 * new buffer. */
10230 if (buf == NULL
10231 && argvars[1].v_type != VAR_UNKNOWN
10232 && get_tv_number_chk(&argvars[1], &error) != 0
10233 && !error
10234 && (name = get_tv_string_chk(&argvars[0])) != NULL
10235 && !error)
10236 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10237
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242}
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010245buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246{
10247#ifdef FEAT_WINDOWS
10248 win_T *wp;
10249 int winnr = 0;
10250#endif
10251 buf_T *buf;
10252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010255 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256#ifdef FEAT_WINDOWS
10257 for (wp = firstwin; wp; wp = wp->w_next)
10258 {
10259 ++winnr;
10260 if (wp->w_buffer == buf)
10261 break;
10262 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010263 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010265 rettv->vval.v_number = (curwin->w_buffer == buf
10266 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267#endif
10268 --emsg_off;
10269}
10270
10271/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010272 * "bufwinid(nr)" function
10273 */
10274 static void
10275f_bufwinid(typval_T *argvars, typval_T *rettv)
10276{
10277 buf_win_common(argvars, rettv, FALSE);
10278}
10279
10280/*
10281 * "bufwinnr(nr)" function
10282 */
10283 static void
10284f_bufwinnr(typval_T *argvars, typval_T *rettv)
10285{
10286 buf_win_common(argvars, rettv, TRUE);
10287}
10288
10289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 * "byte2line(byte)" function
10291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010293f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
10295#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297#else
10298 long boff = 0;
10299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 (linenr_T)0, &boff);
10306#endif
10307}
10308
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010310byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010311{
10312#ifdef FEAT_MBYTE
10313 char_u *t;
10314#endif
10315 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010316 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010318 str = get_tv_string_chk(&argvars[0]);
10319 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010321 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010322 return;
10323
10324#ifdef FEAT_MBYTE
10325 t = str;
10326 for ( ; idx > 0; idx--)
10327 {
10328 if (*t == NUL) /* EOL reached */
10329 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010330 if (enc_utf8 && comp)
10331 t += utf_ptr2len(t);
10332 else
10333 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010334 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010335 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010336#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010337 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010339#endif
10340}
10341
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010342/*
10343 * "byteidx()" function
10344 */
10345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010346f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010347{
10348 byteidx(argvars, rettv, FALSE);
10349}
10350
10351/*
10352 * "byteidxcomp()" function
10353 */
10354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010355f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010356{
10357 byteidx(argvars, rettv, TRUE);
10358}
10359
Bram Moolenaardb913952012-06-29 12:54:53 +020010360 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010361func_call(
10362 char_u *name,
10363 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010364 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010365 dict_T *selfdict,
10366 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010367{
10368 listitem_T *item;
10369 typval_T argv[MAX_FUNC_ARGS + 1];
10370 int argc = 0;
10371 int dummy;
10372 int r = 0;
10373
10374 for (item = args->vval.v_list->lv_first; item != NULL;
10375 item = item->li_next)
10376 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010377 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010378 {
10379 EMSG(_("E699: Too many arguments"));
10380 break;
10381 }
10382 /* Make a copy of each argument. This is needed to be able to set
10383 * v_lock to VAR_FIXED in the copy without changing the original list.
10384 */
10385 copy_tv(&item->li_tv, &argv[argc++]);
10386 }
10387
10388 if (item == NULL)
10389 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10390 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010391 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010392
10393 /* Free the arguments. */
10394 while (argc > 0)
10395 clear_tv(&argv[--argc]);
10396
10397 return r;
10398}
10399
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010400/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010401 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010402 */
10403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010404f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010405{
10406 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010407 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010408 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010409
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010410 if (argvars[1].v_type != VAR_LIST)
10411 {
10412 EMSG(_(e_listreq));
10413 return;
10414 }
10415 if (argvars[1].vval.v_list == NULL)
10416 return;
10417
10418 if (argvars[0].v_type == VAR_FUNC)
10419 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010420 else if (argvars[0].v_type == VAR_PARTIAL)
10421 {
10422 partial = argvars[0].vval.v_partial;
10423 func = partial->pt_name;
10424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010425 else
10426 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 if (*func == NUL)
10428 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010429
Bram Moolenaare9a41262005-01-15 22:18:47 +000010430 if (argvars[2].v_type != VAR_UNKNOWN)
10431 {
10432 if (argvars[2].v_type != VAR_DICT)
10433 {
10434 EMSG(_(e_dictreq));
10435 return;
10436 }
10437 selfdict = argvars[2].vval.v_dict;
10438 }
10439
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010440 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010441}
10442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010443#ifdef FEAT_FLOAT
10444/*
10445 * "ceil({float})" function
10446 */
10447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010448f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010449{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010450 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010451
10452 rettv->v_type = VAR_FLOAT;
10453 if (get_float_arg(argvars, &f) == OK)
10454 rettv->vval.v_float = ceil(f);
10455 else
10456 rettv->vval.v_float = 0.0;
10457}
10458#endif
10459
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010460#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010461/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010462 * "ch_close()" function
10463 */
10464 static void
10465f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10466{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010467 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010468
10469 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010470 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010471 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010472 channel_clear(channel);
10473 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010474}
10475
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010476/*
10477 * "ch_getbufnr()" function
10478 */
10479 static void
10480f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10481{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010482 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010483
10484 rettv->vval.v_number = -1;
10485 if (channel != NULL)
10486 {
10487 char_u *what = get_tv_string(&argvars[1]);
10488 int part;
10489
10490 if (STRCMP(what, "err") == 0)
10491 part = PART_ERR;
10492 else if (STRCMP(what, "out") == 0)
10493 part = PART_OUT;
10494 else if (STRCMP(what, "in") == 0)
10495 part = PART_IN;
10496 else
10497 part = PART_SOCK;
10498 if (channel->ch_part[part].ch_buffer != NULL)
10499 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10500 }
10501}
10502
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010503/*
10504 * "ch_getjob()" function
10505 */
10506 static void
10507f_ch_getjob(typval_T *argvars, typval_T *rettv)
10508{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010509 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010510
10511 if (channel != NULL)
10512 {
10513 rettv->v_type = VAR_JOB;
10514 rettv->vval.v_job = channel->ch_job;
10515 if (channel->ch_job != NULL)
10516 ++channel->ch_job->jv_refcount;
10517 }
10518}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010519
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010520/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010521 * "ch_info()" function
10522 */
10523 static void
10524f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10525{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010526 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010527
10528 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10529 channel_info(channel, rettv->vval.v_dict);
10530}
10531
10532/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010533 * "ch_log()" function
10534 */
10535 static void
10536f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10537{
10538 char_u *msg = get_tv_string(&argvars[0]);
10539 channel_T *channel = NULL;
10540
10541 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010542 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010543
10544 ch_log(channel, (char *)msg);
10545}
10546
10547/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010548 * "ch_logfile()" function
10549 */
10550 static void
10551f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10552{
10553 char_u *fname;
10554 char_u *opt = (char_u *)"";
10555 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010556
10557 fname = get_tv_string(&argvars[0]);
10558 if (argvars[1].v_type == VAR_STRING)
10559 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010560 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010561}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010562
10563/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010564 * "ch_open()" function
10565 */
10566 static void
10567f_ch_open(typval_T *argvars, typval_T *rettv)
10568{
Bram Moolenaar77073442016-02-13 23:23:53 +010010569 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010570 if (check_restricted() || check_secure())
10571 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010572 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010573}
10574
10575/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010576 * "ch_read()" function
10577 */
10578 static void
10579f_ch_read(typval_T *argvars, typval_T *rettv)
10580{
10581 common_channel_read(argvars, rettv, FALSE);
10582}
10583
10584/*
10585 * "ch_readraw()" function
10586 */
10587 static void
10588f_ch_readraw(typval_T *argvars, typval_T *rettv)
10589{
10590 common_channel_read(argvars, rettv, TRUE);
10591}
10592
10593/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010594 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010595 */
10596 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010597f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10598{
10599 ch_expr_common(argvars, rettv, TRUE);
10600}
10601
10602/*
10603 * "ch_sendexpr()" function
10604 */
10605 static void
10606f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10607{
10608 ch_expr_common(argvars, rettv, FALSE);
10609}
10610
10611/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010612 * "ch_evalraw()" function
10613 */
10614 static void
10615f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10616{
10617 ch_raw_common(argvars, rettv, TRUE);
10618}
10619
10620/*
10621 * "ch_sendraw()" function
10622 */
10623 static void
10624f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10625{
10626 ch_raw_common(argvars, rettv, FALSE);
10627}
10628
10629/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010630 * "ch_setoptions()" function
10631 */
10632 static void
10633f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10634{
10635 channel_T *channel;
10636 jobopt_T opt;
10637
Bram Moolenaar437905c2016-04-26 19:01:05 +020010638 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010639 if (channel == NULL)
10640 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010641 clear_job_options(&opt);
10642 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010643 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10644 channel_set_options(channel, &opt);
10645 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010646}
10647
10648/*
10649 * "ch_status()" function
10650 */
10651 static void
10652f_ch_status(typval_T *argvars, typval_T *rettv)
10653{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010654 channel_T *channel;
10655
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010656 /* return an empty string by default */
10657 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010658 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010659
Bram Moolenaar437905c2016-04-26 19:01:05 +020010660 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010661 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010662}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010663#endif
10664
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010665/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010666 * "changenr()" function
10667 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010669f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010670{
10671 rettv->vval.v_number = curbuf->b_u_seq_cur;
10672}
10673
10674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 * "char2nr(string)" function
10676 */
10677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010678f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679{
10680#ifdef FEAT_MBYTE
10681 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010682 {
10683 int utf8 = 0;
10684
10685 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010686 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010010687
10688 if (utf8)
10689 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10690 else
10691 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 else
10694#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696}
10697
10698/*
10699 * "cindent(lnum)" function
10700 */
10701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010702f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703{
10704#ifdef FEAT_CINDENT
10705 pos_T pos;
10706 linenr_T lnum;
10707
10708 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10711 {
10712 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 curwin->w_cursor = pos;
10715 }
10716 else
10717#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719}
10720
10721/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010722 * "clearmatches()" function
10723 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010725f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010726{
10727#ifdef FEAT_SEARCH_EXTRA
10728 clear_matches(curwin);
10729#endif
10730}
10731
10732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 * "col(string)" function
10734 */
10735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010736f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737{
10738 colnr_T col = 0;
10739 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010740 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010742 fp = var2fpos(&argvars[0], FALSE, &fnum);
10743 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 {
10745 if (fp->col == MAXCOL)
10746 {
10747 /* '> can be MAXCOL, get the length of the line then */
10748 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010749 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 else
10751 col = MAXCOL;
10752 }
10753 else
10754 {
10755 col = fp->col + 1;
10756#ifdef FEAT_VIRTUALEDIT
10757 /* col(".") when the cursor is on the NUL at the end of the line
10758 * because of "coladd" can be seen as an extra column. */
10759 if (virtual_active() && fp == &curwin->w_cursor)
10760 {
10761 char_u *p = ml_get_cursor();
10762
10763 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10764 curwin->w_virtcol - curwin->w_cursor.coladd))
10765 {
10766# ifdef FEAT_MBYTE
10767 int l;
10768
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010769 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 col += l;
10771# else
10772 if (*p != NUL && p[1] == NUL)
10773 ++col;
10774# endif
10775 }
10776 }
10777#endif
10778 }
10779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010780 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781}
10782
Bram Moolenaar572cb562005-08-05 21:35:02 +000010783#if defined(FEAT_INS_EXPAND)
10784/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010785 * "complete()" function
10786 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010788f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010789{
10790 int startcol;
10791
10792 if ((State & INSERT) == 0)
10793 {
10794 EMSG(_("E785: complete() can only be used in Insert mode"));
10795 return;
10796 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010797
10798 /* Check for undo allowed here, because if something was already inserted
10799 * the line was already saved for undo and this check isn't done. */
10800 if (!undo_allowed())
10801 return;
10802
Bram Moolenaarade00832006-03-10 21:46:58 +000010803 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10804 {
10805 EMSG(_(e_invarg));
10806 return;
10807 }
10808
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010809 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +000010810 if (startcol <= 0)
10811 return;
10812
10813 set_completion(startcol - 1, argvars[1].vval.v_list);
10814}
10815
10816/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010817 * "complete_add()" function
10818 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010820f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010821{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010822 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010823}
10824
10825/*
10826 * "complete_check()" function
10827 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010829f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010830{
10831 int saved = RedrawingDisabled;
10832
10833 RedrawingDisabled = 0;
10834 ins_compl_check_keys(0);
10835 rettv->vval.v_number = compl_interrupted;
10836 RedrawingDisabled = saved;
10837}
10838#endif
10839
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840/*
10841 * "confirm(message, buttons[, default [, type]])" function
10842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010844f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845{
10846#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10847 char_u *message;
10848 char_u *buttons = NULL;
10849 char_u buf[NUMBUFLEN];
10850 char_u buf2[NUMBUFLEN];
10851 int def = 1;
10852 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 char_u *typestr;
10854 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 message = get_tv_string_chk(&argvars[0]);
10857 if (message == NULL)
10858 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010859 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10862 if (buttons == NULL)
10863 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010864 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010866 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010867 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010869 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10870 if (typestr == NULL)
10871 error = TRUE;
10872 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 switch (TOUPPER_ASC(*typestr))
10875 {
10876 case 'E': type = VIM_ERROR; break;
10877 case 'Q': type = VIM_QUESTION; break;
10878 case 'I': type = VIM_INFO; break;
10879 case 'W': type = VIM_WARNING; break;
10880 case 'G': type = VIM_GENERIC; break;
10881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 }
10883 }
10884 }
10885 }
10886
10887 if (buttons == NULL || *buttons == NUL)
10888 buttons = (char_u *)_("&Ok");
10889
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010890 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010892 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893#endif
10894}
10895
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010896/*
10897 * "copy()" function
10898 */
10899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010900f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010901{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010902 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010903}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010905#ifdef FEAT_FLOAT
10906/*
10907 * "cos()" function
10908 */
10909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010910f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010911{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010912 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010913
10914 rettv->v_type = VAR_FLOAT;
10915 if (get_float_arg(argvars, &f) == OK)
10916 rettv->vval.v_float = cos(f);
10917 else
10918 rettv->vval.v_float = 0.0;
10919}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010920
10921/*
10922 * "cosh()" function
10923 */
10924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010925f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010926{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010927 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010928
10929 rettv->v_type = VAR_FLOAT;
10930 if (get_float_arg(argvars, &f) == OK)
10931 rettv->vval.v_float = cosh(f);
10932 else
10933 rettv->vval.v_float = 0.0;
10934}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010935#endif
10936
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010938 * "count()" function
10939 */
10940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010941f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010942{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010943 long n = 0;
10944 int ic = FALSE;
10945
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010947 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010948 listitem_T *li;
10949 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010950 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010951
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 if ((l = argvars[0].vval.v_list) != NULL)
10953 {
10954 li = l->lv_first;
10955 if (argvars[2].v_type != VAR_UNKNOWN)
10956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 int error = FALSE;
10958
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010959 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010960 if (argvars[3].v_type != VAR_UNKNOWN)
10961 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010962 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 if (!error)
10964 {
10965 li = list_find(l, idx);
10966 if (li == NULL)
10967 EMSGN(_(e_listidx), idx);
10968 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 if (error)
10971 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010972 }
10973
10974 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010975 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 ++n;
10977 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010978 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 else if (argvars[0].v_type == VAR_DICT)
10980 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010981 int todo;
10982 dict_T *d;
10983 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010985 if ((d = argvars[0].vval.v_dict) != NULL)
10986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010987 int error = FALSE;
10988
Bram Moolenaare9a41262005-01-15 22:18:47 +000010989 if (argvars[2].v_type != VAR_UNKNOWN)
10990 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010991 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992 if (argvars[3].v_type != VAR_UNKNOWN)
10993 EMSG(_(e_invarg));
10994 }
10995
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010996 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010997 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010998 {
10999 if (!HASHITEM_EMPTY(hi))
11000 {
11001 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011002 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011003 ++n;
11004 }
11005 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 }
11007 }
11008 else
11009 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011010 rettv->vval.v_number = n;
11011}
11012
11013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11015 *
11016 * Checks the existence of a cscope connection.
11017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011019f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
11021#ifdef FEAT_CSCOPE
11022 int num = 0;
11023 char_u *dbpath = NULL;
11024 char_u *prepend = NULL;
11025 char_u buf[NUMBUFLEN];
11026
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011027 if (argvars[0].v_type != VAR_UNKNOWN
11028 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030 num = (int)get_tv_number(&argvars[0]);
11031 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011032 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 }
11035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037#endif
11038}
11039
11040/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011041 * "cursor(lnum, col)" function, or
11042 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011044 * Moves the cursor to the specified line and column.
11045 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011048f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049{
11050 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011051#ifdef FEAT_VIRTUALEDIT
11052 long coladd = 0;
11053#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011054 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011056 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011057 if (argvars[1].v_type == VAR_UNKNOWN)
11058 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011059 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011060 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011061
Bram Moolenaar493c1782014-05-28 14:34:46 +020011062 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011063 {
11064 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011065 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011066 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011067 line = pos.lnum;
11068 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011069#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011070 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011071#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011072 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011073 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011074 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011075 set_curswant = FALSE;
11076 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011077 }
11078 else
11079 {
11080 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011081 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011082#ifdef FEAT_VIRTUALEDIT
11083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011084 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011085#endif
11086 }
11087 if (line < 0 || col < 0
11088#ifdef FEAT_VIRTUALEDIT
11089 || coladd < 0
11090#endif
11091 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 if (line > 0)
11094 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 if (col > 0)
11096 curwin->w_cursor.col = col - 1;
11097#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011098 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099#endif
11100
11101 /* Make sure the cursor is in a valid position. */
11102 check_cursor();
11103#ifdef FEAT_MBYTE
11104 /* Correct cursor for multi-byte character. */
11105 if (has_mbyte)
11106 mb_adjust_cursor();
11107#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011108
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011109 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011110 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111}
11112
11113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011114 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 */
11116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011117f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011119 int noref = 0;
11120
11121 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011122 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011123 if (noref < 0 || noref > 1)
11124 EMSG(_(e_invarg));
11125 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011126 {
11127 current_copyID += COPYID_INC;
11128 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130}
11131
11132/*
11133 * "delete()" function
11134 */
11135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011136f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011138 char_u nbuf[NUMBUFLEN];
11139 char_u *name;
11140 char_u *flags;
11141
11142 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011144 return;
11145
11146 name = get_tv_string(&argvars[0]);
11147 if (name == NULL || *name == NUL)
11148 {
11149 EMSG(_(e_invarg));
11150 return;
11151 }
11152
11153 if (argvars[1].v_type != VAR_UNKNOWN)
11154 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011156 flags = (char_u *)"";
11157
11158 if (*flags == NUL)
11159 /* delete a file */
11160 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11161 else if (STRCMP(flags, "d") == 0)
11162 /* delete an empty directory */
11163 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11164 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011165 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011166 rettv->vval.v_number = delete_recursive(name);
11167 else
11168 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169}
11170
11171/*
11172 * "did_filetype()" function
11173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011175f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176{
11177#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179#endif
11180}
11181
11182/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011183 * "diff_filler()" function
11184 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011186f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011187{
11188#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011190#endif
11191}
11192
11193/*
11194 * "diff_hlID()" function
11195 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011197f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011198{
11199#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011201 static linenr_T prev_lnum = 0;
11202 static int changedtick = 0;
11203 static int fnum = 0;
11204 static int change_start = 0;
11205 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011206 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011207 int filler_lines;
11208 int col;
11209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 if (lnum < 0) /* ignore type error in {lnum} arg */
11211 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011212 if (lnum != prev_lnum
11213 || changedtick != curbuf->b_changedtick
11214 || fnum != curbuf->b_fnum)
11215 {
11216 /* New line, buffer, change: need to get the values. */
11217 filler_lines = diff_check(curwin, lnum);
11218 if (filler_lines < 0)
11219 {
11220 if (filler_lines == -1)
11221 {
11222 change_start = MAXCOL;
11223 change_end = -1;
11224 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11225 hlID = HLF_ADD; /* added line */
11226 else
11227 hlID = HLF_CHD; /* changed line */
11228 }
11229 else
11230 hlID = HLF_ADD; /* added line */
11231 }
11232 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011233 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011234 prev_lnum = lnum;
11235 changedtick = curbuf->b_changedtick;
11236 fnum = curbuf->b_fnum;
11237 }
11238
11239 if (hlID == HLF_CHD || hlID == HLF_TXD)
11240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011242 if (col >= change_start && col <= change_end)
11243 hlID = HLF_TXD; /* changed text */
11244 else
11245 hlID = HLF_CHD; /* changed line */
11246 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011247 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011248#endif
11249}
11250
11251/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011252 * "empty({expr})" function
11253 */
11254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011255f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011256{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011257 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011258
11259 switch (argvars[0].v_type)
11260 {
11261 case VAR_STRING:
11262 case VAR_FUNC:
11263 n = argvars[0].vval.v_string == NULL
11264 || *argvars[0].vval.v_string == NUL;
11265 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011266 case VAR_PARTIAL:
11267 n = FALSE;
11268 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011269 case VAR_NUMBER:
11270 n = argvars[0].vval.v_number == 0;
11271 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011272 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011273#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011274 n = argvars[0].vval.v_float == 0.0;
11275 break;
11276#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011277 case VAR_LIST:
11278 n = argvars[0].vval.v_list == NULL
11279 || argvars[0].vval.v_list->lv_first == NULL;
11280 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011281 case VAR_DICT:
11282 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011284 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011285 case VAR_SPECIAL:
11286 n = argvars[0].vval.v_number != VVAL_TRUE;
11287 break;
11288
Bram Moolenaar835dc632016-02-07 14:27:38 +010011289 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011290#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011291 n = argvars[0].vval.v_job == NULL
11292 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11293 break;
11294#endif
11295 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011296#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011297 n = argvars[0].vval.v_channel == NULL
11298 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011299 break;
11300#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011301 case VAR_UNKNOWN:
11302 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11303 n = TRUE;
11304 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011305 }
11306
11307 rettv->vval.v_number = n;
11308}
11309
11310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 * "escape({string}, {chars})" function
11312 */
11313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011314f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315{
11316 char_u buf[NUMBUFLEN];
11317
Bram Moolenaar758711c2005-02-02 23:11:38 +000011318 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11319 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321}
11322
11323/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011324 * "eval()" function
11325 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011327f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011328{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011329 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 s = get_tv_string_chk(&argvars[0]);
11332 if (s != NULL)
11333 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011334
Bram Moolenaar615b9972015-01-14 17:15:05 +010011335 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011336 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11337 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011338 if (p != NULL && !aborting())
11339 EMSG2(_(e_invexpr2), p);
11340 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011342 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011344 else if (*s != NUL)
11345 EMSG(_(e_trailing));
11346}
11347
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011348static garray_T redir_evalcmd_ga;
11349
11350/*
11351 * Append "value[value_len]" to the evalcmd() output.
11352 */
11353 void
11354evalcmd_redir_str(char_u *value, int value_len)
11355{
11356 int len;
11357
11358 if (value_len == -1)
11359 len = (int)STRLEN(value); /* Append the entire string */
11360 else
11361 len = value_len; /* Append only "value_len" characters */
11362 if (ga_grow(&redir_evalcmd_ga, len) == OK)
11363 {
11364 mch_memmove((char *)redir_evalcmd_ga.ga_data
11365 + redir_evalcmd_ga.ga_len, value, len);
11366 redir_evalcmd_ga.ga_len += len;
11367 }
11368}
11369
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011370/*
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011371 * "evalcmd()" function
11372 */
11373 static void
11374f_evalcmd(typval_T *argvars, typval_T *rettv)
11375{
11376 char_u *s;
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011377 int save_msg_silent = msg_silent;
11378 int save_redir_evalcmd = redir_evalcmd;
11379 garray_T save_ga;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011380
11381 rettv->vval.v_string = NULL;
11382 rettv->v_type = VAR_STRING;
11383
11384 s = get_tv_string_chk(&argvars[0]);
11385 if (s != NULL)
11386 {
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011387 if (redir_evalcmd)
11388 save_ga = redir_evalcmd_ga;
11389 ga_init2(&redir_evalcmd_ga, (int)sizeof(char), 500);
11390 redir_evalcmd = TRUE;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011391
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011392 ++msg_silent;
11393 do_cmdline_cmd(s);
11394 rettv->vval.v_string = redir_evalcmd_ga.ga_data;
11395 msg_silent = save_msg_silent;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011396
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011397 redir_evalcmd = save_redir_evalcmd;
11398 if (redir_evalcmd)
11399 redir_evalcmd_ga = save_ga;
Bram Moolenaaree1deb42016-07-08 23:06:21 +020011400
11401 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
11402 * line. Put it back in the first column. */
11403 msg_col = 0;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011404 }
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011405}
11406
11407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 * "eventhandler()" function
11409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011411f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414}
11415
11416/*
11417 * "executable()" function
11418 */
11419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011420f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011422 char_u *name = get_tv_string(&argvars[0]);
11423
11424 /* Check in $PATH and also check directly if there is a directory name. */
11425 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11426 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011427}
11428
11429/*
11430 * "exepath()" function
11431 */
11432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011433f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011434{
11435 char_u *p = NULL;
11436
Bram Moolenaarb5971142015-03-21 17:32:19 +010011437 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011438 rettv->v_type = VAR_STRING;
11439 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440}
11441
11442/*
11443 * "exists()" function
11444 */
11445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011446f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447{
11448 char_u *p;
11449 char_u *name;
11450 int n = FALSE;
11451 int len = 0;
11452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 if (*p == '$') /* environment variable */
11455 {
11456 /* first try "normal" environment variables (fast) */
11457 if (mch_getenv(p + 1) != NULL)
11458 n = TRUE;
11459 else
11460 {
11461 /* try expanding things like $VIM and ${HOME} */
11462 p = expand_env_save(p);
11463 if (p != NULL && *p != '$')
11464 n = TRUE;
11465 vim_free(p);
11466 }
11467 }
11468 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011471 if (*skipwhite(p) != NUL)
11472 n = FALSE; /* trailing garbage */
11473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 else if (*p == '*') /* internal or user defined function */
11475 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011476 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 }
11478 else if (*p == ':')
11479 {
11480 n = cmd_exists(p + 1);
11481 }
11482 else if (*p == '#')
11483 {
11484#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011485 if (p[1] == '#')
11486 n = autocmd_supported(p + 2);
11487 else
11488 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489#endif
11490 }
11491 else /* internal variable */
11492 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011493 char_u *tofree;
11494 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011496 /* get_name_len() takes care of expanding curly braces */
11497 name = p;
11498 len = get_name_len(&p, &tofree, TRUE, FALSE);
11499 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011501 if (tofree != NULL)
11502 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011503 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011504 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011506 /* handle d.key, l[idx], f(expr) */
11507 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11508 if (n)
11509 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 }
11511 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011512 if (*p != NUL)
11513 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011515 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 }
11517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519}
11520
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011521#ifdef FEAT_FLOAT
11522/*
11523 * "exp()" function
11524 */
11525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011526f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011527{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011528 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011529
11530 rettv->v_type = VAR_FLOAT;
11531 if (get_float_arg(argvars, &f) == OK)
11532 rettv->vval.v_float = exp(f);
11533 else
11534 rettv->vval.v_float = 0.0;
11535}
11536#endif
11537
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538/*
11539 * "expand()" function
11540 */
11541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011542f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543{
11544 char_u *s;
11545 int len;
11546 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011547 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011549 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011550 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011553 if (argvars[1].v_type != VAR_UNKNOWN
11554 && argvars[2].v_type != VAR_UNKNOWN
11555 && get_tv_number_chk(&argvars[2], &error)
11556 && !error)
11557 {
11558 rettv->v_type = VAR_LIST;
11559 rettv->vval.v_list = NULL;
11560 }
11561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 if (*s == '%' || *s == '#' || *s == '<')
11564 {
11565 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011566 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011568 if (rettv->v_type == VAR_LIST)
11569 {
11570 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11571 list_append_string(rettv->vval.v_list, result, -1);
11572 else
11573 vim_free(result);
11574 }
11575 else
11576 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 }
11578 else
11579 {
11580 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011581 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011582 if (argvars[1].v_type != VAR_UNKNOWN
11583 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011584 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011585 if (!error)
11586 {
11587 ExpandInit(&xpc);
11588 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011589 if (p_wic)
11590 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011591 if (rettv->v_type == VAR_STRING)
11592 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11593 options, WILD_ALL);
11594 else if (rettv_list_alloc(rettv) != FAIL)
11595 {
11596 int i;
11597
11598 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11599 for (i = 0; i < xpc.xp_numfiles; i++)
11600 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11601 ExpandCleanup(&xpc);
11602 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011603 }
11604 else
11605 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 }
11607}
11608
11609/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011610 * Go over all entries in "d2" and add them to "d1".
11611 * When "action" is "error" then a duplicate key is an error.
11612 * When "action" is "force" then a duplicate key is overwritten.
11613 * Otherwise duplicate keys are ignored ("action" is "keep").
11614 */
11615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011616dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011617{
11618 dictitem_T *di1;
11619 hashitem_T *hi2;
11620 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011621 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011622
11623 todo = (int)d2->dv_hashtab.ht_used;
11624 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11625 {
11626 if (!HASHITEM_EMPTY(hi2))
11627 {
11628 --todo;
11629 di1 = dict_find(d1, hi2->hi_key, -1);
11630 if (d1->dv_scope != 0)
11631 {
11632 /* Disallow replacing a builtin function in l: and g:.
11633 * Check the key to be valid when adding to any
11634 * scope. */
11635 if (d1->dv_scope == VAR_DEF_SCOPE
11636 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11637 && var_check_func_name(hi2->hi_key,
11638 di1 == NULL))
11639 break;
11640 if (!valid_varname(hi2->hi_key))
11641 break;
11642 }
11643 if (di1 == NULL)
11644 {
11645 di1 = dictitem_copy(HI2DI(hi2));
11646 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11647 dictitem_free(di1);
11648 }
11649 else if (*action == 'e')
11650 {
11651 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11652 break;
11653 }
11654 else if (*action == 'f' && HI2DI(hi2) != di1)
11655 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011656 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11657 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011658 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011659 clear_tv(&di1->di_tv);
11660 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11661 }
11662 }
11663 }
11664}
11665
11666/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011667 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011668 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011669 */
11670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011671f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011672{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011673 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011674
Bram Moolenaare9a41262005-01-15 22:18:47 +000011675 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011676 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 list_T *l1, *l2;
11678 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011679 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011681
Bram Moolenaare9a41262005-01-15 22:18:47 +000011682 l1 = argvars[0].vval.v_list;
11683 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011684 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011685 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011686 {
11687 if (argvars[2].v_type != VAR_UNKNOWN)
11688 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011689 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011690 if (error)
11691 return; /* type error; errmsg already given */
11692
Bram Moolenaar758711c2005-02-02 23:11:38 +000011693 if (before == l1->lv_len)
11694 item = NULL;
11695 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011696 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011697 item = list_find(l1, before);
11698 if (item == NULL)
11699 {
11700 EMSGN(_(e_listidx), before);
11701 return;
11702 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011703 }
11704 }
11705 else
11706 item = NULL;
11707 list_extend(l1, l2, item);
11708
Bram Moolenaare9a41262005-01-15 22:18:47 +000011709 copy_tv(&argvars[0], rettv);
11710 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011711 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011712 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11713 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011714 dict_T *d1, *d2;
11715 char_u *action;
11716 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011717
11718 d1 = argvars[0].vval.v_dict;
11719 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011720 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011721 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011722 {
11723 /* Check the third argument. */
11724 if (argvars[2].v_type != VAR_UNKNOWN)
11725 {
11726 static char *(av[]) = {"keep", "force", "error"};
11727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011728 action = get_tv_string_chk(&argvars[2]);
11729 if (action == NULL)
11730 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011731 for (i = 0; i < 3; ++i)
11732 if (STRCMP(action, av[i]) == 0)
11733 break;
11734 if (i == 3)
11735 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011736 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011737 return;
11738 }
11739 }
11740 else
11741 action = (char_u *)"force";
11742
Bram Moolenaara9922d62013-05-30 13:01:18 +020011743 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744
Bram Moolenaare9a41262005-01-15 22:18:47 +000011745 copy_tv(&argvars[0], rettv);
11746 }
11747 }
11748 else
11749 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011750}
11751
11752/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011753 * "feedkeys()" function
11754 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011756f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011757{
11758 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011759 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011760 char_u *keys, *flags;
11761 char_u nbuf[NUMBUFLEN];
11762 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011763 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011764 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011765 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011766
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011767 /* This is not allowed in the sandbox. If the commands would still be
11768 * executed in the sandbox it would be OK, but it probably happens later,
11769 * when "sandbox" is no longer set. */
11770 if (check_secure())
11771 return;
11772
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011773 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011774
11775 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011776 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011777 flags = get_tv_string_buf(&argvars[1], nbuf);
11778 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011779 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011780 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011781 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011782 case 'n': remap = FALSE; break;
11783 case 'm': remap = TRUE; break;
11784 case 't': typed = TRUE; break;
11785 case 'i': insert = TRUE; break;
11786 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011787 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011788 }
11789 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011790 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011791
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011792 if (*keys != NUL || execute)
11793 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011794 /* Need to escape K_SPECIAL and CSI before putting the string in the
11795 * typeahead buffer. */
11796 keys_esc = vim_strsave_escape_csi(keys);
11797 if (keys_esc != NULL)
11798 {
11799 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011800 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011801 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011802 if (vgetc_busy)
11803 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011804 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011805 {
11806 int save_msg_scroll = msg_scroll;
11807
11808 /* Avoid a 1 second delay when the keys start Insert mode. */
11809 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011810
Bram Moolenaar245c4102016-04-20 17:37:41 +020011811 if (!dangerous)
11812 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011813 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011814 if (!dangerous)
11815 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011816 msg_scroll |= save_msg_scroll;
11817 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011818 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011819 }
11820}
11821
11822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 * "filereadable()" function
11824 */
11825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011826f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011828 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 char_u *p;
11830 int n;
11831
Bram Moolenaarc236c162008-07-13 17:41:49 +000011832#ifndef O_NONBLOCK
11833# define O_NONBLOCK 0
11834#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011836 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11837 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 {
11839 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011840 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 }
11842 else
11843 n = FALSE;
11844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846}
11847
11848/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011849 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 * rights to write into.
11851 */
11852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011853f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011855 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856}
11857
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011858 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011859findfilendir(
11860 typval_T *argvars UNUSED,
11861 typval_T *rettv,
11862 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011863{
11864#ifdef FEAT_SEARCHPATH
11865 char_u *fname;
11866 char_u *fresult = NULL;
11867 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11868 char_u *p;
11869 char_u pathbuf[NUMBUFLEN];
11870 int count = 1;
11871 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011872 int error = FALSE;
11873#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011874
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011875 rettv->vval.v_string = NULL;
11876 rettv->v_type = VAR_STRING;
11877
11878#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011880
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011881 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011883 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11884 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011885 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011886 else
11887 {
11888 if (*p != NUL)
11889 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011892 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011893 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011894 }
11895
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011896 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11897 error = TRUE;
11898
11899 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011901 do
11902 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011903 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011904 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011905 fresult = find_file_in_path_option(first ? fname : NULL,
11906 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011907 0, first, path,
11908 find_what,
11909 curbuf->b_ffname,
11910 find_what == FINDFILE_DIR
11911 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011912 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011913
11914 if (fresult != NULL && rettv->v_type == VAR_LIST)
11915 list_append_string(rettv->vval.v_list, fresult, -1);
11916
11917 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011919
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011920 if (rettv->v_type == VAR_STRING)
11921 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011922#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011923}
11924
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011925static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011926static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011927
11928/*
11929 * Implementation of map() and filter().
11930 */
11931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011932filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011933{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011934 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 listitem_T *li, *nli;
11936 list_T *l = NULL;
11937 dictitem_T *di;
11938 hashtab_T *ht;
11939 hashitem_T *hi;
11940 dict_T *d = NULL;
11941 typval_T save_val;
11942 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011943 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011944 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011945 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011946 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011947 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011948 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011949 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011950
Bram Moolenaare9a41262005-01-15 22:18:47 +000011951 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011952 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011953 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011954 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011955 return;
11956 }
11957 else if (argvars[0].v_type == VAR_DICT)
11958 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011959 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011960 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011961 return;
11962 }
11963 else
11964 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011965 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011966 return;
11967 }
11968
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011969 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970 /* On type errors, the preceding call has already displayed an error
11971 * message. Avoid a misleading error message for an empty string that
11972 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011973 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011975 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011976
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011977 /* We reset "did_emsg" to be able to detect whether an error
11978 * occurred during evaluation of the expression. */
11979 save_did_emsg = did_emsg;
11980 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011981
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011982 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011983 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985 vimvars[VV_KEY].vv_type = VAR_STRING;
11986
11987 ht = &d->dv_hashtab;
11988 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011989 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992 if (!HASHITEM_EMPTY(hi))
11993 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011994 int r;
11995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011996 --todo;
11997 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011998 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011999 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12000 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012001 break;
12002 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012003 r = filter_map_one(&di->di_tv, expr, map, &rem);
12004 clear_tv(&vimvars[VV_KEY].vv_tv);
12005 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012006 break;
12007 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012008 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012009 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12010 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012011 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012012 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012013 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012014 }
12015 }
12016 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012017 }
12018 else
12019 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012020 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012022 for (li = l->lv_first; li != NULL; li = nli)
12023 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012024 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012025 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012026 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012027 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012028 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012029 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012030 break;
12031 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012032 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012033 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012034 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012035 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012036
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012037 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012038 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012039
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012040 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012041 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012042
12043 copy_tv(&argvars[0], rettv);
12044}
12045
12046 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012047filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012048{
Bram Moolenaar33570922005-01-25 22:26:29 +000012049 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012050 typval_T argv[3];
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012051 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000012052 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012053 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012054 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012055
Bram Moolenaar33570922005-01-25 22:26:29 +000012056 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012057 argv[0] = vimvars[VV_KEY].vv_tv;
12058 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012059 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012060 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012061 s = expr->vval.v_string;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012062 if (call_func(s, (int)STRLEN(s),
12063 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
12064 goto theend;
12065 }
12066 else if (expr->v_type == VAR_PARTIAL)
12067 {
12068 partial_T *partial = expr->vval.v_partial;
12069
12070 s = partial->pt_name;
12071 if (call_func(s, (int)STRLEN(s),
12072 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12073 == FAIL)
12074 goto theend;
12075 }
12076 else
12077 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012078 s = get_tv_string_buf_chk(expr, buf);
12079 if (s == NULL)
12080 goto theend;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012081 s = skipwhite(s);
12082 if (eval1(&s, &rettv, TRUE) == FAIL)
12083 goto theend;
12084 if (*s != NUL) /* check for trailing chars after expr */
12085 {
12086 EMSG2(_(e_invexpr2), s);
12087 goto theend;
12088 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012089 }
12090 if (map)
12091 {
12092 /* map(): replace the list item value */
12093 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012094 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012095 *tv = rettv;
12096 }
12097 else
12098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012099 int error = FALSE;
12100
Bram Moolenaare9a41262005-01-15 22:18:47 +000012101 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012102 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012103 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012104 /* On type error, nothing has been removed; return FAIL to stop the
12105 * loop. The error message was given by get_tv_number_chk(). */
12106 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012107 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012108 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012109 retval = OK;
12110theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012111 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012112 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012113}
12114
12115/*
12116 * "filter()" function
12117 */
12118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012119f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012120{
12121 filter_map(argvars, rettv, FALSE);
12122}
12123
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012124/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012125 * "finddir({fname}[, {path}[, {count}]])" function
12126 */
12127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012128f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012129{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012130 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012131}
12132
12133/*
12134 * "findfile({fname}[, {path}[, {count}]])" function
12135 */
12136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012137f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012138{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012139 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140}
12141
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012142#ifdef FEAT_FLOAT
12143/*
12144 * "float2nr({float})" function
12145 */
12146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012147f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012148{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012149 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012150
12151 if (get_float_arg(argvars, &f) == OK)
12152 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012153# ifdef FEAT_NUM64
12154 if (f < -0x7fffffffffffffff)
12155 rettv->vval.v_number = -0x7fffffffffffffff;
12156 else if (f > 0x7fffffffffffffff)
12157 rettv->vval.v_number = 0x7fffffffffffffff;
12158 else
12159 rettv->vval.v_number = (varnumber_T)f;
12160# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012161 if (f < -0x7fffffff)
12162 rettv->vval.v_number = -0x7fffffff;
12163 else if (f > 0x7fffffff)
12164 rettv->vval.v_number = 0x7fffffff;
12165 else
12166 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012167# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012168 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012169}
12170
12171/*
12172 * "floor({float})" function
12173 */
12174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012175f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012176{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012177 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012178
12179 rettv->v_type = VAR_FLOAT;
12180 if (get_float_arg(argvars, &f) == OK)
12181 rettv->vval.v_float = floor(f);
12182 else
12183 rettv->vval.v_float = 0.0;
12184}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012185
12186/*
12187 * "fmod()" function
12188 */
12189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012190f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012191{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012192 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012193
12194 rettv->v_type = VAR_FLOAT;
12195 if (get_float_arg(argvars, &fx) == OK
12196 && get_float_arg(&argvars[1], &fy) == OK)
12197 rettv->vval.v_float = fmod(fx, fy);
12198 else
12199 rettv->vval.v_float = 0.0;
12200}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012201#endif
12202
Bram Moolenaar0d660222005-01-07 21:51:51 +000012203/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012204 * "fnameescape({string})" function
12205 */
12206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012207f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012208{
12209 rettv->vval.v_string = vim_strsave_fnameescape(
12210 get_tv_string(&argvars[0]), FALSE);
12211 rettv->v_type = VAR_STRING;
12212}
12213
12214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 * "fnamemodify({fname}, {mods})" function
12216 */
12217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012218f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219{
12220 char_u *fname;
12221 char_u *mods;
12222 int usedlen = 0;
12223 int len;
12224 char_u *fbuf = NULL;
12225 char_u buf[NUMBUFLEN];
12226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012227 fname = get_tv_string_chk(&argvars[0]);
12228 mods = get_tv_string_buf_chk(&argvars[1], buf);
12229 if (fname == NULL || mods == NULL)
12230 fname = NULL;
12231 else
12232 {
12233 len = (int)STRLEN(fname);
12234 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012241 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 vim_free(fbuf);
12243}
12244
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012245static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246
12247/*
12248 * "foldclosed()" function
12249 */
12250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012251foldclosed_both(
12252 typval_T *argvars UNUSED,
12253 typval_T *rettv,
12254 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255{
12256#ifdef FEAT_FOLDING
12257 linenr_T lnum;
12258 linenr_T first, last;
12259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12262 {
12263 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12264 {
12265 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012266 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 return;
12270 }
12271 }
12272#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274}
12275
12276/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012277 * "foldclosed()" function
12278 */
12279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012280f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012281{
12282 foldclosed_both(argvars, rettv, FALSE);
12283}
12284
12285/*
12286 * "foldclosedend()" function
12287 */
12288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012289f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012290{
12291 foldclosed_both(argvars, rettv, TRUE);
12292}
12293
12294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 * "foldlevel()" function
12296 */
12297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012298f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299{
12300#ifdef FEAT_FOLDING
12301 linenr_T lnum;
12302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012303 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012305 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307}
12308
12309/*
12310 * "foldtext()" function
12311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012313f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314{
12315#ifdef FEAT_FOLDING
12316 linenr_T lnum;
12317 char_u *s;
12318 char_u *r;
12319 int len;
12320 char *txt;
12321#endif
12322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->v_type = VAR_STRING;
12324 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012326 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12327 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12328 <= curbuf->b_ml.ml_line_count
12329 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 {
12331 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012332 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12333 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334 {
12335 if (!linewhite(lnum))
12336 break;
12337 ++lnum;
12338 }
12339
12340 /* Find interesting text in this line. */
12341 s = skipwhite(ml_get(lnum));
12342 /* skip C comment-start */
12343 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012346 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012347 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012348 {
12349 s = skipwhite(ml_get(lnum + 1));
12350 if (*s == '*')
12351 s = skipwhite(s + 1);
12352 }
12353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 txt = _("+-%s%3ld lines: ");
12355 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012356 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 + 20 /* for %3ld */
12358 + STRLEN(s))); /* concatenated */
12359 if (r != NULL)
12360 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012361 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12362 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12363 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 len = (int)STRLEN(r);
12365 STRCAT(r, s);
12366 /* remove 'foldmarker' and 'commentstring' */
12367 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 }
12370 }
12371#endif
12372}
12373
12374/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012375 * "foldtextresult(lnum)" function
12376 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012378f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012379{
12380#ifdef FEAT_FOLDING
12381 linenr_T lnum;
12382 char_u *text;
12383 char_u buf[51];
12384 foldinfo_T foldinfo;
12385 int fold_count;
12386#endif
12387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 rettv->v_type = VAR_STRING;
12389 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012390#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012392 /* treat illegal types and illegal string values for {lnum} the same */
12393 if (lnum < 0)
12394 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012395 fold_count = foldedCount(curwin, lnum, &foldinfo);
12396 if (fold_count > 0)
12397 {
12398 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12399 &foldinfo, buf);
12400 if (text == buf)
12401 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012402 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012403 }
12404#endif
12405}
12406
12407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 * "foreground()" function
12409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012411f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413#ifdef FEAT_GUI
12414 if (gui.in_use)
12415 gui_mch_set_foreground();
12416#else
12417# ifdef WIN32
12418 win32_set_foreground();
12419# endif
12420#endif
12421}
12422
12423/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012424 * "function()" function
12425 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012427f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012428{
12429 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012430 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012431 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012432 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012433
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012434 if (argvars[0].v_type == VAR_FUNC)
12435 {
12436 /* function(MyFunc, [arg], dict) */
12437 s = argvars[0].vval.v_string;
12438 }
12439 else if (argvars[0].v_type == VAR_PARTIAL
12440 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012441 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012442 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012443 arg_pt = argvars[0].vval.v_partial;
12444 s = arg_pt->pt_name;
12445 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012446 else
12447 {
12448 /* function('MyFunc', [arg], dict) */
12449 s = get_tv_string(&argvars[0]);
12450 use_string = TRUE;
12451 }
12452
12453 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012454 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012455 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012456 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12457 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012458 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012459 else
12460 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012461 int dict_idx = 0;
12462 int arg_idx = 0;
12463 list_T *list = NULL;
12464
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012465 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012466 {
12467 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012468 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012469
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012470 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12471 * also be called from another script. Using trans_function_name()
12472 * would also work, but some plugins depend on the name being
12473 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012474 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012475 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12476 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012477 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012478 STRCPY(name, sid_buf);
12479 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012480 }
12481 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012482 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012483 name = vim_strsave(s);
12484
12485 if (argvars[1].v_type != VAR_UNKNOWN)
12486 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012487 if (argvars[2].v_type != VAR_UNKNOWN)
12488 {
12489 /* function(name, [args], dict) */
12490 arg_idx = 1;
12491 dict_idx = 2;
12492 }
12493 else if (argvars[1].v_type == VAR_DICT)
12494 /* function(name, dict) */
12495 dict_idx = 1;
12496 else
12497 /* function(name, [args]) */
12498 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012499 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012500 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012501 if (argvars[dict_idx].v_type != VAR_DICT)
12502 {
12503 EMSG(_("E922: expected a dict"));
12504 vim_free(name);
12505 return;
12506 }
12507 if (argvars[dict_idx].vval.v_dict == NULL)
12508 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012509 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012510 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012511 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012512 if (argvars[arg_idx].v_type != VAR_LIST)
12513 {
12514 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12515 vim_free(name);
12516 return;
12517 }
12518 list = argvars[arg_idx].vval.v_list;
12519 if (list == NULL || list->lv_len == 0)
12520 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012521 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012522 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012523 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012524 {
12525 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012526
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012527 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012528 if (pt == NULL)
12529 vim_free(name);
12530 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012531 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012532 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012533 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012534 listitem_T *li;
12535 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012536 int arg_len = 0;
12537 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012538
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012539 if (arg_pt != NULL)
12540 arg_len = arg_pt->pt_argc;
12541 if (list != NULL)
12542 lv_len = list->lv_len;
12543 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012544 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012545 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012546 if (pt->pt_argv == NULL)
12547 {
12548 vim_free(pt);
12549 vim_free(name);
12550 return;
12551 }
12552 else
12553 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012554 for (i = 0; i < arg_len; i++)
12555 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12556 if (lv_len > 0)
12557 for (li = list->lv_first; li != NULL;
12558 li = li->li_next)
12559 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012560 }
12561 }
12562
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012563 /* For "function(dict.func, [], dict)" and "func" is a partial
12564 * use "dict". That is backwards compatible. */
12565 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012566 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012567 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012568 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12569 ++pt->pt_dict->dv_refcount;
12570 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012571 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012572 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012573 /* If the dict was bound automatically the result is also
12574 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012575 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012576 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012577 if (pt->pt_dict != NULL)
12578 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012579 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012580
12581 pt->pt_refcount = 1;
12582 pt->pt_name = name;
12583 func_ref(pt->pt_name);
12584 }
12585 rettv->v_type = VAR_PARTIAL;
12586 rettv->vval.v_partial = pt;
12587 }
12588 else
12589 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012590 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012591 rettv->v_type = VAR_FUNC;
12592 rettv->vval.v_string = name;
12593 func_ref(name);
12594 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012595 }
12596}
12597
12598/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012599 * "garbagecollect()" function
12600 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012602f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012603{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012604 /* This is postponed until we are back at the toplevel, because we may be
12605 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12606 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012607
12608 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12609 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012610}
12611
12612/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613 * "get()" function
12614 */
12615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012616f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012617{
Bram Moolenaar33570922005-01-25 22:26:29 +000012618 listitem_T *li;
12619 list_T *l;
12620 dictitem_T *di;
12621 dict_T *d;
12622 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012623
Bram Moolenaare9a41262005-01-15 22:18:47 +000012624 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012625 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012626 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012628 int error = FALSE;
12629
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012630 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012631 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012632 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012633 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012634 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012635 else if (argvars[0].v_type == VAR_DICT)
12636 {
12637 if ((d = argvars[0].vval.v_dict) != NULL)
12638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012639 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012640 if (di != NULL)
12641 tv = &di->di_tv;
12642 }
12643 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012644 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012645 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012646 partial_T *pt;
12647 partial_T fref_pt;
12648
12649 if (argvars[0].v_type == VAR_PARTIAL)
12650 pt = argvars[0].vval.v_partial;
12651 else
12652 {
12653 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12654 fref_pt.pt_name = argvars[0].vval.v_string;
12655 pt = &fref_pt;
12656 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012657
12658 if (pt != NULL)
12659 {
12660 char_u *what = get_tv_string(&argvars[1]);
12661
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012662 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012663 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012664 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012665 if (pt->pt_name == NULL)
12666 rettv->vval.v_string = NULL;
12667 else
12668 rettv->vval.v_string = vim_strsave(pt->pt_name);
12669 }
12670 else if (STRCMP(what, "dict") == 0)
12671 {
12672 rettv->v_type = VAR_DICT;
12673 rettv->vval.v_dict = pt->pt_dict;
12674 if (pt->pt_dict != NULL)
12675 ++pt->pt_dict->dv_refcount;
12676 }
12677 else if (STRCMP(what, "args") == 0)
12678 {
12679 rettv->v_type = VAR_LIST;
12680 if (rettv_list_alloc(rettv) == OK)
12681 {
12682 int i;
12683
12684 for (i = 0; i < pt->pt_argc; ++i)
12685 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12686 }
12687 }
12688 else
12689 EMSG2(_(e_invarg2), what);
12690 return;
12691 }
12692 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012693 else
12694 EMSG2(_(e_listdictarg), "get()");
12695
12696 if (tv == NULL)
12697 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012698 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012699 copy_tv(&argvars[2], rettv);
12700 }
12701 else
12702 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012703}
12704
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012705static 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 +000012706
12707/*
12708 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012709 * Return a range (from start to end) of lines in rettv from the specified
12710 * buffer.
12711 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012712 */
12713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012714get_buffer_lines(
12715 buf_T *buf,
12716 linenr_T start,
12717 linenr_T end,
12718 int retlist,
12719 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012720{
12721 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012722
Bram Moolenaar959a1432013-12-14 12:17:38 +010012723 rettv->v_type = VAR_STRING;
12724 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012725 if (retlist && rettv_list_alloc(rettv) == FAIL)
12726 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012727
12728 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12729 return;
12730
12731 if (!retlist)
12732 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012733 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12734 p = ml_get_buf(buf, start, FALSE);
12735 else
12736 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012737 rettv->vval.v_string = vim_strsave(p);
12738 }
12739 else
12740 {
12741 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012742 return;
12743
12744 if (start < 1)
12745 start = 1;
12746 if (end > buf->b_ml.ml_line_count)
12747 end = buf->b_ml.ml_line_count;
12748 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012749 if (list_append_string(rettv->vval.v_list,
12750 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012751 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012752 }
12753}
12754
12755/*
12756 * "getbufline()" function
12757 */
12758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012759f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012760{
12761 linenr_T lnum;
12762 linenr_T end;
12763 buf_T *buf;
12764
12765 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12766 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012767 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012768 --emsg_off;
12769
Bram Moolenaar661b1822005-07-28 22:36:45 +000012770 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012771 if (argvars[2].v_type == VAR_UNKNOWN)
12772 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012773 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012774 end = get_tv_lnum_buf(&argvars[2], buf);
12775
Bram Moolenaar342337a2005-07-21 21:11:17 +000012776 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012777}
12778
Bram Moolenaar0d660222005-01-07 21:51:51 +000012779/*
12780 * "getbufvar()" function
12781 */
12782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012783f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012784{
12785 buf_T *buf;
12786 buf_T *save_curbuf;
12787 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012789 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012791 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12792 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012793 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012794 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012795
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012796 rettv->v_type = VAR_STRING;
12797 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012798
12799 if (buf != NULL && varname != NULL)
12800 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012801 /* set curbuf to be our buf, temporarily */
12802 save_curbuf = curbuf;
12803 curbuf = buf;
12804
Bram Moolenaar0d660222005-01-07 21:51:51 +000012805 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012806 {
12807 if (get_option_tv(&varname, rettv, TRUE) == OK)
12808 done = TRUE;
12809 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012810 else if (STRCMP(varname, "changedtick") == 0)
12811 {
12812 rettv->v_type = VAR_NUMBER;
12813 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012814 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012815 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012816 else
12817 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012818 /* Look up the variable. */
12819 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12820 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12821 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012822 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012823 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012825 done = TRUE;
12826 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012827 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012828
12829 /* restore previous notion of curbuf */
12830 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012831 }
12832
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012833 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12834 /* use the default value */
12835 copy_tv(&argvars[2], rettv);
12836
Bram Moolenaar0d660222005-01-07 21:51:51 +000012837 --emsg_off;
12838}
12839
12840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 * "getchar()" function
12842 */
12843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012844f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845{
12846 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012847 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012849 /* Position the cursor. Needed after a message that ends in a space. */
12850 windgoto(msg_row, msg_col);
12851
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 ++no_mapping;
12853 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012854 for (;;)
12855 {
12856 if (argvars[0].v_type == VAR_UNKNOWN)
12857 /* getchar(): blocking wait. */
12858 n = safe_vgetc();
12859 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12860 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012861 n = vpeekc_any();
12862 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012863 /* illegal argument or getchar(0) and no char avail: return zero */
12864 n = 0;
12865 else
12866 /* getchar(0) and char avail: return char */
12867 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012868
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012869 if (n == K_IGNORE)
12870 continue;
12871 break;
12872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 --no_mapping;
12874 --allow_keys;
12875
Bram Moolenaar219b8702006-11-01 14:32:36 +000012876 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012877 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012878 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12879 vimvars[VV_MOUSE_COL].vv_nr = 0;
12880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 if (IS_SPECIAL(n) || mod_mask != 0)
12883 {
12884 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12885 int i = 0;
12886
12887 /* Turn a special key into three bytes, plus modifier. */
12888 if (mod_mask != 0)
12889 {
12890 temp[i++] = K_SPECIAL;
12891 temp[i++] = KS_MODIFIER;
12892 temp[i++] = mod_mask;
12893 }
12894 if (IS_SPECIAL(n))
12895 {
12896 temp[i++] = K_SPECIAL;
12897 temp[i++] = K_SECOND(n);
12898 temp[i++] = K_THIRD(n);
12899 }
12900#ifdef FEAT_MBYTE
12901 else if (has_mbyte)
12902 i += (*mb_char2bytes)(n, temp + i);
12903#endif
12904 else
12905 temp[i++] = n;
12906 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 rettv->v_type = VAR_STRING;
12908 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012909
12910#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012911 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012912 {
12913 int row = mouse_row;
12914 int col = mouse_col;
12915 win_T *win;
12916 linenr_T lnum;
12917# ifdef FEAT_WINDOWS
12918 win_T *wp;
12919# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012920 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012921
12922 if (row >= 0 && col >= 0)
12923 {
12924 /* Find the window at the mouse coordinates and compute the
12925 * text position. */
12926 win = mouse_find_win(&row, &col);
12927 (void)mouse_comp_pos(win, &row, &col, &lnum);
12928# ifdef FEAT_WINDOWS
12929 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012930 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012931# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012932 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012933 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012934 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12935 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12936 }
12937 }
12938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939 }
12940}
12941
12942/*
12943 * "getcharmod()" function
12944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012946f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949}
12950
12951/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012952 * "getcharsearch()" function
12953 */
12954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012955f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012956{
12957 if (rettv_dict_alloc(rettv) != FAIL)
12958 {
12959 dict_T *dict = rettv->vval.v_dict;
12960
12961 dict_add_nr_str(dict, "char", 0L, last_csearch());
12962 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12963 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12964 }
12965}
12966
12967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 * "getcmdline()" function
12969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012971f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012973 rettv->v_type = VAR_STRING;
12974 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975}
12976
12977/*
12978 * "getcmdpos()" function
12979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012981f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984}
12985
12986/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012987 * "getcmdtype()" function
12988 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012990f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012991{
12992 rettv->v_type = VAR_STRING;
12993 rettv->vval.v_string = alloc(2);
12994 if (rettv->vval.v_string != NULL)
12995 {
12996 rettv->vval.v_string[0] = get_cmdline_type();
12997 rettv->vval.v_string[1] = NUL;
12998 }
12999}
13000
13001/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013002 * "getcmdwintype()" function
13003 */
13004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013005f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013006{
13007 rettv->v_type = VAR_STRING;
13008 rettv->vval.v_string = NULL;
13009#ifdef FEAT_CMDWIN
13010 rettv->vval.v_string = alloc(2);
13011 if (rettv->vval.v_string != NULL)
13012 {
13013 rettv->vval.v_string[0] = cmdwin_type;
13014 rettv->vval.v_string[1] = NUL;
13015 }
13016#endif
13017}
13018
13019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 * "getcwd()" function
13021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013023f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013025 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013026 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013029 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010013030
13031 wp = find_tabwin(&argvars[0], &argvars[1]);
13032 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013034 if (wp->w_localdir != NULL)
13035 rettv->vval.v_string = vim_strsave(wp->w_localdir);
13036 else if(globaldir != NULL)
13037 rettv->vval.v_string = vim_strsave(globaldir);
13038 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020013039 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013040 cwd = alloc(MAXPATHL);
13041 if (cwd != NULL)
13042 {
13043 if (mch_dirname(cwd, MAXPATHL) != FAIL)
13044 rettv->vval.v_string = vim_strsave(cwd);
13045 vim_free(cwd);
13046 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020013047 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010013048#ifdef BACKSLASH_IN_FILENAME
13049 if (rettv->vval.v_string != NULL)
13050 slash_adjust(rettv->vval.v_string);
13051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 }
13053}
13054
13055/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013056 * "getfontname()" function
13057 */
13058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013059f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061 rettv->v_type = VAR_STRING;
13062 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013063#ifdef FEAT_GUI
13064 if (gui.in_use)
13065 {
13066 GuiFont font;
13067 char_u *name = NULL;
13068
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013069 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013070 {
13071 /* Get the "Normal" font. Either the name saved by
13072 * hl_set_font_name() or from the font ID. */
13073 font = gui.norm_font;
13074 name = hl_get_font_name();
13075 }
13076 else
13077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013078 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013079 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13080 return;
13081 font = gui_mch_get_font(name, FALSE);
13082 if (font == NOFONT)
13083 return; /* Invalid font name, return empty string. */
13084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013085 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013086 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013087 gui_mch_free_font(font);
13088 }
13089#endif
13090}
13091
13092/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013093 * "getfperm({fname})" function
13094 */
13095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013096f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013097{
13098 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013099 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013100 char_u *perm = NULL;
13101 char_u flags[] = "rwx";
13102 int i;
13103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013107 if (mch_stat((char *)fname, &st) >= 0)
13108 {
13109 perm = vim_strsave((char_u *)"---------");
13110 if (perm != NULL)
13111 {
13112 for (i = 0; i < 9; i++)
13113 {
13114 if (st.st_mode & (1 << (8 - i)))
13115 perm[i] = flags[i % 3];
13116 }
13117 }
13118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013119 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013120}
13121
13122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 * "getfsize({fname})" function
13124 */
13125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013126f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127{
13128 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013129 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134
13135 if (mch_stat((char *)fname, &st) >= 0)
13136 {
13137 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013138 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013142
13143 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013144 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013145 rettv->vval.v_number = -2;
13146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 }
13148 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150}
13151
13152/*
13153 * "getftime({fname})" function
13154 */
13155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013156f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157{
13158 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013159 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162
13163 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013164 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167}
13168
13169/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013170 * "getftype({fname})" function
13171 */
13172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013173f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013174{
13175 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013176 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013177 char_u *type = NULL;
13178 char *t;
13179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013182 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013183 if (mch_lstat((char *)fname, &st) >= 0)
13184 {
13185#ifdef S_ISREG
13186 if (S_ISREG(st.st_mode))
13187 t = "file";
13188 else if (S_ISDIR(st.st_mode))
13189 t = "dir";
13190# ifdef S_ISLNK
13191 else if (S_ISLNK(st.st_mode))
13192 t = "link";
13193# endif
13194# ifdef S_ISBLK
13195 else if (S_ISBLK(st.st_mode))
13196 t = "bdev";
13197# endif
13198# ifdef S_ISCHR
13199 else if (S_ISCHR(st.st_mode))
13200 t = "cdev";
13201# endif
13202# ifdef S_ISFIFO
13203 else if (S_ISFIFO(st.st_mode))
13204 t = "fifo";
13205# endif
13206# ifdef S_ISSOCK
13207 else if (S_ISSOCK(st.st_mode))
13208 t = "fifo";
13209# endif
13210 else
13211 t = "other";
13212#else
13213# ifdef S_IFMT
13214 switch (st.st_mode & S_IFMT)
13215 {
13216 case S_IFREG: t = "file"; break;
13217 case S_IFDIR: t = "dir"; break;
13218# ifdef S_IFLNK
13219 case S_IFLNK: t = "link"; break;
13220# endif
13221# ifdef S_IFBLK
13222 case S_IFBLK: t = "bdev"; break;
13223# endif
13224# ifdef S_IFCHR
13225 case S_IFCHR: t = "cdev"; break;
13226# endif
13227# ifdef S_IFIFO
13228 case S_IFIFO: t = "fifo"; break;
13229# endif
13230# ifdef S_IFSOCK
13231 case S_IFSOCK: t = "socket"; break;
13232# endif
13233 default: t = "other";
13234 }
13235# else
13236 if (mch_isdir(fname))
13237 t = "dir";
13238 else
13239 t = "file";
13240# endif
13241#endif
13242 type = vim_strsave((char_u *)t);
13243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013244 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013245}
13246
13247/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013248 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013249 */
13250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013251f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013252{
13253 linenr_T lnum;
13254 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013255 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013256
13257 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013258 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013259 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013260 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013261 retlist = FALSE;
13262 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013263 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013264 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013265 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013266 retlist = TRUE;
13267 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013268
Bram Moolenaar342337a2005-07-21 21:11:17 +000013269 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013270}
13271
13272/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013273 * "getmatches()" function
13274 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013276f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013277{
13278#ifdef FEAT_SEARCH_EXTRA
13279 dict_T *dict;
13280 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013281 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013282
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013283 if (rettv_list_alloc(rettv) == OK)
13284 {
13285 while (cur != NULL)
13286 {
13287 dict = dict_alloc();
13288 if (dict == NULL)
13289 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013290 if (cur->match.regprog == NULL)
13291 {
13292 /* match added with matchaddpos() */
13293 for (i = 0; i < MAXPOSMATCH; ++i)
13294 {
13295 llpos_T *llpos;
13296 char buf[6];
13297 list_T *l;
13298
13299 llpos = &cur->pos.pos[i];
13300 if (llpos->lnum == 0)
13301 break;
13302 l = list_alloc();
13303 if (l == NULL)
13304 break;
13305 list_append_number(l, (varnumber_T)llpos->lnum);
13306 if (llpos->col > 0)
13307 {
13308 list_append_number(l, (varnumber_T)llpos->col);
13309 list_append_number(l, (varnumber_T)llpos->len);
13310 }
13311 sprintf(buf, "pos%d", i + 1);
13312 dict_add_list(dict, buf, l);
13313 }
13314 }
13315 else
13316 {
13317 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13318 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013319 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013320 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13321 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013322# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013323 if (cur->conceal_char)
13324 {
13325 char_u buf[MB_MAXBYTES + 1];
13326
13327 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13328 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13329 }
13330# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013331 list_append_dict(rettv->vval.v_list, dict);
13332 cur = cur->next;
13333 }
13334 }
13335#endif
13336}
13337
13338/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013339 * "getpid()" function
13340 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013342f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013343{
13344 rettv->vval.v_number = mch_get_pid();
13345}
13346
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013348getpos_both(
13349 typval_T *argvars,
13350 typval_T *rettv,
13351 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013352{
Bram Moolenaara5525202006-03-02 22:52:09 +000013353 pos_T *fp;
13354 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013355 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013356
13357 if (rettv_list_alloc(rettv) == OK)
13358 {
13359 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013360 if (getcurpos)
13361 fp = &curwin->w_cursor;
13362 else
13363 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013364 if (fnum != -1)
13365 list_append_number(l, (varnumber_T)fnum);
13366 else
13367 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013368 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13369 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013370 list_append_number(l, (fp != NULL)
13371 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013372 : (varnumber_T)0);
13373 list_append_number(l,
13374#ifdef FEAT_VIRTUALEDIT
13375 (fp != NULL) ? (varnumber_T)fp->coladd :
13376#endif
13377 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013378 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013379 {
13380 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013381 list_append_number(l, curwin->w_curswant == MAXCOL ?
13382 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013383 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013384 }
13385 else
13386 rettv->vval.v_number = FALSE;
13387}
13388
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013389
13390/*
13391 * "getcurpos()" function
13392 */
13393 static void
13394f_getcurpos(typval_T *argvars, typval_T *rettv)
13395{
13396 getpos_both(argvars, rettv, TRUE);
13397}
13398
13399/*
13400 * "getpos(string)" function
13401 */
13402 static void
13403f_getpos(typval_T *argvars, typval_T *rettv)
13404{
13405 getpos_both(argvars, rettv, FALSE);
13406}
13407
Bram Moolenaara5525202006-03-02 22:52:09 +000013408/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013409 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013410 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013412f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013413{
13414#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013415 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013416#endif
13417
Bram Moolenaar2641f772005-03-25 21:58:17 +000013418#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013419 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013420 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013421 wp = NULL;
13422 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13423 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013424 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013425 if (wp == NULL)
13426 return;
13427 }
13428
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013429 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013430 }
13431#endif
13432}
13433
13434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 * "getreg()" function
13436 */
13437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013438f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439{
13440 char_u *strregname;
13441 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013442 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013443 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013444 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013446 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013448 strregname = get_tv_string_chk(&argvars[0]);
13449 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013450 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013451 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013452 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013453 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013454 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013455 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013458 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013459
13460 if (error)
13461 return;
13462
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 regname = (strregname == NULL ? '"' : *strregname);
13464 if (regname == 0)
13465 regname = '"';
13466
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013467 if (return_list)
13468 {
13469 rettv->v_type = VAR_LIST;
13470 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13471 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013472 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013473 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013474 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013475 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013476 }
13477 else
13478 {
13479 rettv->v_type = VAR_STRING;
13480 rettv->vval.v_string = get_reg_contents(regname,
13481 arg2 ? GREG_EXPR_SRC : 0);
13482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483}
13484
13485/*
13486 * "getregtype()" function
13487 */
13488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013489f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490{
13491 char_u *strregname;
13492 int regname;
13493 char_u buf[NUMBUFLEN + 2];
13494 long reglen = 0;
13495
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013496 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 {
13498 strregname = get_tv_string_chk(&argvars[0]);
13499 if (strregname == NULL) /* type error; errmsg already given */
13500 {
13501 rettv->v_type = VAR_STRING;
13502 rettv->vval.v_string = NULL;
13503 return;
13504 }
13505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 else
13507 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013508 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509
13510 regname = (strregname == NULL ? '"' : *strregname);
13511 if (regname == 0)
13512 regname = '"';
13513
13514 buf[0] = NUL;
13515 buf[1] = NUL;
13516 switch (get_reg_type(regname, &reglen))
13517 {
13518 case MLINE: buf[0] = 'V'; break;
13519 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 case MBLOCK:
13521 buf[0] = Ctrl_V;
13522 sprintf((char *)buf + 1, "%ld", reglen + 1);
13523 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013525 rettv->v_type = VAR_STRING;
13526 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527}
13528
13529/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013530 * "gettabvar()" function
13531 */
13532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013533f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013534{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013535 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013536 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013537 dictitem_T *v;
13538 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013539 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013540
13541 rettv->v_type = VAR_STRING;
13542 rettv->vval.v_string = NULL;
13543
13544 varname = get_tv_string_chk(&argvars[1]);
13545 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13546 if (tp != NULL && varname != NULL)
13547 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013548 /* Set tp to be our tabpage, temporarily. Also set the window to the
13549 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013550 if (switch_win(&oldcurwin, &oldtabpage,
13551 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013552 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013553 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013554 /* look up the variable */
13555 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13556 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13557 if (v != NULL)
13558 {
13559 copy_tv(&v->di_tv, rettv);
13560 done = TRUE;
13561 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013562 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013563
13564 /* restore previous notion of curwin */
13565 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013566 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013567
13568 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013569 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013570}
13571
13572/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013573 * "gettabwinvar()" function
13574 */
13575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013576f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013577{
13578 getwinvar(argvars, rettv, 1);
13579}
13580
13581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 * "getwinposx()" function
13583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013585f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588#ifdef FEAT_GUI
13589 if (gui.in_use)
13590 {
13591 int x, y;
13592
13593 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 }
13596#endif
13597}
13598
13599/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013600 * "win_findbuf()" function
13601 */
13602 static void
13603f_win_findbuf(typval_T *argvars, typval_T *rettv)
13604{
13605 if (rettv_list_alloc(rettv) != FAIL)
13606 win_findbuf(argvars, rettv->vval.v_list);
13607}
13608
13609/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013610 * "win_getid()" function
13611 */
13612 static void
13613f_win_getid(typval_T *argvars, typval_T *rettv)
13614{
13615 rettv->vval.v_number = win_getid(argvars);
13616}
13617
13618/*
13619 * "win_gotoid()" function
13620 */
13621 static void
13622f_win_gotoid(typval_T *argvars, typval_T *rettv)
13623{
13624 rettv->vval.v_number = win_gotoid(argvars);
13625}
13626
13627/*
13628 * "win_id2tabwin()" function
13629 */
13630 static void
13631f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13632{
13633 if (rettv_list_alloc(rettv) != FAIL)
13634 win_id2tabwin(argvars, rettv->vval.v_list);
13635}
13636
13637/*
13638 * "win_id2win()" function
13639 */
13640 static void
13641f_win_id2win(typval_T *argvars, typval_T *rettv)
13642{
13643 rettv->vval.v_number = win_id2win(argvars);
13644}
13645
13646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 * "getwinposy()" function
13648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013650f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013652 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653#ifdef FEAT_GUI
13654 if (gui.in_use)
13655 {
13656 int x, y;
13657
13658 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013659 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 }
13661#endif
13662}
13663
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013664/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013665 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013666 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013667 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013668find_win_by_nr(
13669 typval_T *vp,
13670 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013671{
13672#ifdef FEAT_WINDOWS
13673 win_T *wp;
13674#endif
13675 int nr;
13676
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013677 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013678
13679#ifdef FEAT_WINDOWS
13680 if (nr < 0)
13681 return NULL;
13682 if (nr == 0)
13683 return curwin;
13684
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013685 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13686 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013687 if (nr >= LOWEST_WIN_ID)
13688 {
13689 if (wp->w_id == nr)
13690 return wp;
13691 }
13692 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013693 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013694 if (nr >= LOWEST_WIN_ID)
13695 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013696 return wp;
13697#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013698 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013699 return curwin;
13700 return NULL;
13701#endif
13702}
13703
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013705 * Find window specified by "wvp" in tabpage "tvp".
13706 */
13707 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013708find_tabwin(
13709 typval_T *wvp, /* VAR_UNKNOWN for current window */
13710 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013711{
13712 win_T *wp = NULL;
13713 tabpage_T *tp = NULL;
13714 long n;
13715
13716 if (wvp->v_type != VAR_UNKNOWN)
13717 {
13718 if (tvp->v_type != VAR_UNKNOWN)
13719 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013720 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013721 if (n >= 0)
13722 tp = find_tabpage(n);
13723 }
13724 else
13725 tp = curtab;
13726
13727 if (tp != NULL)
13728 wp = find_win_by_nr(wvp, tp);
13729 }
13730 else
13731 wp = curwin;
13732
13733 return wp;
13734}
13735
13736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 * "getwinvar()" function
13738 */
13739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013740f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013742 getwinvar(argvars, rettv, 0);
13743}
13744
13745/*
13746 * getwinvar() and gettabwinvar()
13747 */
13748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013749getwinvar(
13750 typval_T *argvars,
13751 typval_T *rettv,
13752 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013753{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013754 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013756 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013757 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013758 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013759#ifdef FEAT_WINDOWS
13760 win_T *oldcurwin;
13761 tabpage_T *oldtabpage;
13762 int need_switch_win;
13763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013765#ifdef FEAT_WINDOWS
13766 if (off == 1)
13767 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13768 else
13769 tp = curtab;
13770#endif
13771 win = find_win_by_nr(&argvars[off], tp);
13772 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013775 rettv->v_type = VAR_STRING;
13776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777
13778 if (win != NULL && varname != NULL)
13779 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013780#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013781 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013782 * otherwise the window is not valid. Only do this when needed,
13783 * autocommands get blocked. */
13784 need_switch_win = !(tp == curtab && win == curwin);
13785 if (!need_switch_win
13786 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13787#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013788 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013789 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013790 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013791 if (get_option_tv(&varname, rettv, 1) == OK)
13792 done = TRUE;
13793 }
13794 else
13795 {
13796 /* Look up the variable. */
13797 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13798 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13799 varname, FALSE);
13800 if (v != NULL)
13801 {
13802 copy_tv(&v->di_tv, rettv);
13803 done = TRUE;
13804 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013807
Bram Moolenaarba117c22015-09-29 16:53:22 +020013808#ifdef FEAT_WINDOWS
13809 if (need_switch_win)
13810 /* restore previous notion of curwin */
13811 restore_win(oldcurwin, oldtabpage, TRUE);
13812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 }
13814
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013815 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13816 /* use the default return value */
13817 copy_tv(&argvars[off + 2], rettv);
13818
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 --emsg_off;
13820}
13821
13822/*
13823 * "glob()" function
13824 */
13825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013826f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013828 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013830 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013832 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013833 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013835 if (argvars[1].v_type != VAR_UNKNOWN)
13836 {
13837 if (get_tv_number_chk(&argvars[1], &error))
13838 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013839 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013840 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013841 if (get_tv_number_chk(&argvars[2], &error))
13842 {
13843 rettv->v_type = VAR_LIST;
13844 rettv->vval.v_list = NULL;
13845 }
13846 if (argvars[3].v_type != VAR_UNKNOWN
13847 && get_tv_number_chk(&argvars[3], &error))
13848 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013849 }
13850 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013851 if (!error)
13852 {
13853 ExpandInit(&xpc);
13854 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013855 if (p_wic)
13856 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013857 if (rettv->v_type == VAR_STRING)
13858 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013859 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013860 else if (rettv_list_alloc(rettv) != FAIL)
13861 {
13862 int i;
13863
13864 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13865 NULL, options, WILD_ALL_KEEP);
13866 for (i = 0; i < xpc.xp_numfiles; i++)
13867 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13868
13869 ExpandCleanup(&xpc);
13870 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013871 }
13872 else
13873 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874}
13875
13876/*
13877 * "globpath()" function
13878 */
13879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013880f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013882 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013884 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013885 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013886 garray_T ga;
13887 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013889 /* When the optional second argument is non-zero, don't remove matches
13890 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013892 if (argvars[2].v_type != VAR_UNKNOWN)
13893 {
13894 if (get_tv_number_chk(&argvars[2], &error))
13895 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013896 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013897 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013898 if (get_tv_number_chk(&argvars[3], &error))
13899 {
13900 rettv->v_type = VAR_LIST;
13901 rettv->vval.v_list = NULL;
13902 }
13903 if (argvars[4].v_type != VAR_UNKNOWN
13904 && get_tv_number_chk(&argvars[4], &error))
13905 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013906 }
13907 }
13908 if (file != NULL && !error)
13909 {
13910 ga_init2(&ga, (int)sizeof(char_u *), 10);
13911 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13912 if (rettv->v_type == VAR_STRING)
13913 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13914 else if (rettv_list_alloc(rettv) != FAIL)
13915 for (i = 0; i < ga.ga_len; ++i)
13916 list_append_string(rettv->vval.v_list,
13917 ((char_u **)(ga.ga_data))[i], -1);
13918 ga_clear_strings(&ga);
13919 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013920 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013921 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922}
13923
13924/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013925 * "glob2regpat()" function
13926 */
13927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013928f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013929{
13930 char_u *pat = get_tv_string_chk(&argvars[0]);
13931
13932 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013933 rettv->vval.v_string = (pat == NULL)
13934 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013935}
13936
13937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 * "has()" function
13939 */
13940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013941f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942{
13943 int i;
13944 char_u *name;
13945 int n = FALSE;
13946 static char *(has_list[]) =
13947 {
13948#ifdef AMIGA
13949 "amiga",
13950# ifdef FEAT_ARP
13951 "arp",
13952# endif
13953#endif
13954#ifdef __BEOS__
13955 "beos",
13956#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013957#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 "mac",
13959#endif
13960#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013961 "macunix", /* built with 'darwin' enabled */
13962#endif
13963#if defined(__APPLE__) && __APPLE__ == 1
13964 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966#ifdef __QNX__
13967 "qnx",
13968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969#ifdef UNIX
13970 "unix",
13971#endif
13972#ifdef VMS
13973 "vms",
13974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975#ifdef WIN32
13976 "win32",
13977#endif
13978#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13979 "win32unix",
13980#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013981#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 "win64",
13983#endif
13984#ifdef EBCDIC
13985 "ebcdic",
13986#endif
13987#ifndef CASE_INSENSITIVE_FILENAME
13988 "fname_case",
13989#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013990#ifdef HAVE_ACL
13991 "acl",
13992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993#ifdef FEAT_ARABIC
13994 "arabic",
13995#endif
13996#ifdef FEAT_AUTOCMD
13997 "autocmd",
13998#endif
13999#ifdef FEAT_BEVAL
14000 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000014001# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
14002 "balloon_multiline",
14003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004#endif
14005#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
14006 "builtin_terms",
14007# ifdef ALL_BUILTIN_TCAPS
14008 "all_builtin_terms",
14009# endif
14010#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020014011#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
14012 || defined(FEAT_GUI_W32) \
14013 || defined(FEAT_GUI_MOTIF))
14014 "browsefilter",
14015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016#ifdef FEAT_BYTEOFF
14017 "byte_offset",
14018#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014019#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010014020 "channel",
14021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022#ifdef FEAT_CINDENT
14023 "cindent",
14024#endif
14025#ifdef FEAT_CLIENTSERVER
14026 "clientserver",
14027#endif
14028#ifdef FEAT_CLIPBOARD
14029 "clipboard",
14030#endif
14031#ifdef FEAT_CMDL_COMPL
14032 "cmdline_compl",
14033#endif
14034#ifdef FEAT_CMDHIST
14035 "cmdline_hist",
14036#endif
14037#ifdef FEAT_COMMENTS
14038 "comments",
14039#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014040#ifdef FEAT_CONCEAL
14041 "conceal",
14042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043#ifdef FEAT_CRYPT
14044 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010014045 "crypt-blowfish",
14046 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047#endif
14048#ifdef FEAT_CSCOPE
14049 "cscope",
14050#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014051#ifdef FEAT_CURSORBIND
14052 "cursorbind",
14053#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014054#ifdef CURSOR_SHAPE
14055 "cursorshape",
14056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057#ifdef DEBUG
14058 "debug",
14059#endif
14060#ifdef FEAT_CON_DIALOG
14061 "dialog_con",
14062#endif
14063#ifdef FEAT_GUI_DIALOG
14064 "dialog_gui",
14065#endif
14066#ifdef FEAT_DIFF
14067 "diff",
14068#endif
14069#ifdef FEAT_DIGRAPHS
14070 "digraphs",
14071#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014072#ifdef FEAT_DIRECTX
14073 "directx",
14074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075#ifdef FEAT_DND
14076 "dnd",
14077#endif
14078#ifdef FEAT_EMACS_TAGS
14079 "emacs_tags",
14080#endif
14081 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014082 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083#ifdef FEAT_SEARCH_EXTRA
14084 "extra_search",
14085#endif
14086#ifdef FEAT_FKMAP
14087 "farsi",
14088#endif
14089#ifdef FEAT_SEARCHPATH
14090 "file_in_path",
14091#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014092#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014093 "filterpipe",
14094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095#ifdef FEAT_FIND_ID
14096 "find_in_path",
14097#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014098#ifdef FEAT_FLOAT
14099 "float",
14100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101#ifdef FEAT_FOLDING
14102 "folding",
14103#endif
14104#ifdef FEAT_FOOTER
14105 "footer",
14106#endif
14107#if !defined(USE_SYSTEM) && defined(UNIX)
14108 "fork",
14109#endif
14110#ifdef FEAT_GETTEXT
14111 "gettext",
14112#endif
14113#ifdef FEAT_GUI
14114 "gui",
14115#endif
14116#ifdef FEAT_GUI_ATHENA
14117# ifdef FEAT_GUI_NEXTAW
14118 "gui_neXtaw",
14119# else
14120 "gui_athena",
14121# endif
14122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123#ifdef FEAT_GUI_GTK
14124 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014125# ifdef USE_GTK3
14126 "gui_gtk3",
14127# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014131#ifdef FEAT_GUI_GNOME
14132 "gui_gnome",
14133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134#ifdef FEAT_GUI_MAC
14135 "gui_mac",
14136#endif
14137#ifdef FEAT_GUI_MOTIF
14138 "gui_motif",
14139#endif
14140#ifdef FEAT_GUI_PHOTON
14141 "gui_photon",
14142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143#ifdef FEAT_GUI_W32
14144 "gui_win32",
14145#endif
14146#ifdef FEAT_HANGULIN
14147 "hangul_input",
14148#endif
14149#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14150 "iconv",
14151#endif
14152#ifdef FEAT_INS_EXPAND
14153 "insert_expand",
14154#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014155#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014156 "job",
14157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158#ifdef FEAT_JUMPLIST
14159 "jumplist",
14160#endif
14161#ifdef FEAT_KEYMAP
14162 "keymap",
14163#endif
14164#ifdef FEAT_LANGMAP
14165 "langmap",
14166#endif
14167#ifdef FEAT_LIBCALL
14168 "libcall",
14169#endif
14170#ifdef FEAT_LINEBREAK
14171 "linebreak",
14172#endif
14173#ifdef FEAT_LISP
14174 "lispindent",
14175#endif
14176#ifdef FEAT_LISTCMDS
14177 "listcmds",
14178#endif
14179#ifdef FEAT_LOCALMAP
14180 "localmap",
14181#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014182#ifdef FEAT_LUA
14183# ifndef DYNAMIC_LUA
14184 "lua",
14185# endif
14186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187#ifdef FEAT_MENU
14188 "menu",
14189#endif
14190#ifdef FEAT_SESSION
14191 "mksession",
14192#endif
14193#ifdef FEAT_MODIFY_FNAME
14194 "modify_fname",
14195#endif
14196#ifdef FEAT_MOUSE
14197 "mouse",
14198#endif
14199#ifdef FEAT_MOUSESHAPE
14200 "mouseshape",
14201#endif
14202#if defined(UNIX) || defined(VMS)
14203# ifdef FEAT_MOUSE_DEC
14204 "mouse_dec",
14205# endif
14206# ifdef FEAT_MOUSE_GPM
14207 "mouse_gpm",
14208# endif
14209# ifdef FEAT_MOUSE_JSB
14210 "mouse_jsbterm",
14211# endif
14212# ifdef FEAT_MOUSE_NET
14213 "mouse_netterm",
14214# endif
14215# ifdef FEAT_MOUSE_PTERM
14216 "mouse_pterm",
14217# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014218# ifdef FEAT_MOUSE_SGR
14219 "mouse_sgr",
14220# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014221# ifdef FEAT_SYSMOUSE
14222 "mouse_sysmouse",
14223# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014224# ifdef FEAT_MOUSE_URXVT
14225 "mouse_urxvt",
14226# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227# ifdef FEAT_MOUSE_XTERM
14228 "mouse_xterm",
14229# endif
14230#endif
14231#ifdef FEAT_MBYTE
14232 "multi_byte",
14233#endif
14234#ifdef FEAT_MBYTE_IME
14235 "multi_byte_ime",
14236#endif
14237#ifdef FEAT_MULTI_LANG
14238 "multi_lang",
14239#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014240#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014241#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014242 "mzscheme",
14243#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014244#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014245#ifdef FEAT_NUM64
14246 "num64",
14247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248#ifdef FEAT_OLE
14249 "ole",
14250#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014251 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252#ifdef FEAT_PATH_EXTRA
14253 "path_extra",
14254#endif
14255#ifdef FEAT_PERL
14256#ifndef DYNAMIC_PERL
14257 "perl",
14258#endif
14259#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014260#ifdef FEAT_PERSISTENT_UNDO
14261 "persistent_undo",
14262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263#ifdef FEAT_PYTHON
14264#ifndef DYNAMIC_PYTHON
14265 "python",
14266#endif
14267#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014268#ifdef FEAT_PYTHON3
14269#ifndef DYNAMIC_PYTHON3
14270 "python3",
14271#endif
14272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273#ifdef FEAT_POSTSCRIPT
14274 "postscript",
14275#endif
14276#ifdef FEAT_PRINTER
14277 "printer",
14278#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014279#ifdef FEAT_PROFILE
14280 "profile",
14281#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014282#ifdef FEAT_RELTIME
14283 "reltime",
14284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285#ifdef FEAT_QUICKFIX
14286 "quickfix",
14287#endif
14288#ifdef FEAT_RIGHTLEFT
14289 "rightleft",
14290#endif
14291#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14292 "ruby",
14293#endif
14294#ifdef FEAT_SCROLLBIND
14295 "scrollbind",
14296#endif
14297#ifdef FEAT_CMDL_INFO
14298 "showcmd",
14299 "cmdline_info",
14300#endif
14301#ifdef FEAT_SIGNS
14302 "signs",
14303#endif
14304#ifdef FEAT_SMARTINDENT
14305 "smartindent",
14306#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014307#ifdef STARTUPTIME
14308 "startuptime",
14309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310#ifdef FEAT_STL_OPT
14311 "statusline",
14312#endif
14313#ifdef FEAT_SUN_WORKSHOP
14314 "sun_workshop",
14315#endif
14316#ifdef FEAT_NETBEANS_INTG
14317 "netbeans_intg",
14318#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014319#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014320 "spell",
14321#endif
14322#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 "syntax",
14324#endif
14325#if defined(USE_SYSTEM) || !defined(UNIX)
14326 "system",
14327#endif
14328#ifdef FEAT_TAG_BINS
14329 "tag_binary",
14330#endif
14331#ifdef FEAT_TAG_OLDSTATIC
14332 "tag_old_static",
14333#endif
14334#ifdef FEAT_TAG_ANYWHITE
14335 "tag_any_white",
14336#endif
14337#ifdef FEAT_TCL
14338# ifndef DYNAMIC_TCL
14339 "tcl",
14340# endif
14341#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014342#ifdef FEAT_TERMGUICOLORS
14343 "termguicolors",
14344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345#ifdef TERMINFO
14346 "terminfo",
14347#endif
14348#ifdef FEAT_TERMRESPONSE
14349 "termresponse",
14350#endif
14351#ifdef FEAT_TEXTOBJ
14352 "textobjects",
14353#endif
14354#ifdef HAVE_TGETENT
14355 "tgetent",
14356#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014357#ifdef FEAT_TIMERS
14358 "timers",
14359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360#ifdef FEAT_TITLE
14361 "title",
14362#endif
14363#ifdef FEAT_TOOLBAR
14364 "toolbar",
14365#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014366#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14367 "unnamedplus",
14368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369#ifdef FEAT_USR_CMDS
14370 "user-commands", /* was accidentally included in 5.4 */
14371 "user_commands",
14372#endif
14373#ifdef FEAT_VIMINFO
14374 "viminfo",
14375#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014376#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 "vertsplit",
14378#endif
14379#ifdef FEAT_VIRTUALEDIT
14380 "virtualedit",
14381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383#ifdef FEAT_VISUALEXTRA
14384 "visualextra",
14385#endif
14386#ifdef FEAT_VREPLACE
14387 "vreplace",
14388#endif
14389#ifdef FEAT_WILDIGN
14390 "wildignore",
14391#endif
14392#ifdef FEAT_WILDMENU
14393 "wildmenu",
14394#endif
14395#ifdef FEAT_WINDOWS
14396 "windows",
14397#endif
14398#ifdef FEAT_WAK
14399 "winaltkeys",
14400#endif
14401#ifdef FEAT_WRITEBACKUP
14402 "writebackup",
14403#endif
14404#ifdef FEAT_XIM
14405 "xim",
14406#endif
14407#ifdef FEAT_XFONTSET
14408 "xfontset",
14409#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014410#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014411 "xpm",
14412 "xpm_w32", /* for backward compatibility */
14413#else
14414# if defined(HAVE_XPM)
14415 "xpm",
14416# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418#ifdef USE_XSMP
14419 "xsmp",
14420#endif
14421#ifdef USE_XSMP_INTERACT
14422 "xsmp_interact",
14423#endif
14424#ifdef FEAT_XCLIPBOARD
14425 "xterm_clipboard",
14426#endif
14427#ifdef FEAT_XTERM_SAVE
14428 "xterm_save",
14429#endif
14430#if defined(UNIX) && defined(FEAT_X11)
14431 "X11",
14432#endif
14433 NULL
14434 };
14435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 for (i = 0; has_list[i] != NULL; ++i)
14438 if (STRICMP(name, has_list[i]) == 0)
14439 {
14440 n = TRUE;
14441 break;
14442 }
14443
14444 if (n == FALSE)
14445 {
14446 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014447 {
14448 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014449 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014450 && vim_isdigit(name[6])
14451 && vim_isdigit(name[8])
14452 && vim_isdigit(name[10]))
14453 {
14454 int major = atoi((char *)name + 6);
14455 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014456
14457 /* Expect "patch-9.9.01234". */
14458 n = (major < VIM_VERSION_MAJOR
14459 || (major == VIM_VERSION_MAJOR
14460 && (minor < VIM_VERSION_MINOR
14461 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014462 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014463 }
14464 else
14465 n = has_patch(atoi((char *)name + 5));
14466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 else if (STRICMP(name, "vim_starting") == 0)
14468 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014469#ifdef FEAT_MBYTE
14470 else if (STRICMP(name, "multi_byte_encoding") == 0)
14471 n = has_mbyte;
14472#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014473#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14474 else if (STRICMP(name, "balloon_multiline") == 0)
14475 n = multiline_balloon_available();
14476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477#ifdef DYNAMIC_TCL
14478 else if (STRICMP(name, "tcl") == 0)
14479 n = tcl_enabled(FALSE);
14480#endif
14481#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14482 else if (STRICMP(name, "iconv") == 0)
14483 n = iconv_enabled(FALSE);
14484#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014485#ifdef DYNAMIC_LUA
14486 else if (STRICMP(name, "lua") == 0)
14487 n = lua_enabled(FALSE);
14488#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014489#ifdef DYNAMIC_MZSCHEME
14490 else if (STRICMP(name, "mzscheme") == 0)
14491 n = mzscheme_enabled(FALSE);
14492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493#ifdef DYNAMIC_RUBY
14494 else if (STRICMP(name, "ruby") == 0)
14495 n = ruby_enabled(FALSE);
14496#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014497#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498#ifdef DYNAMIC_PYTHON
14499 else if (STRICMP(name, "python") == 0)
14500 n = python_enabled(FALSE);
14501#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014502#endif
14503#ifdef FEAT_PYTHON3
14504#ifdef DYNAMIC_PYTHON3
14505 else if (STRICMP(name, "python3") == 0)
14506 n = python3_enabled(FALSE);
14507#endif
14508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509#ifdef DYNAMIC_PERL
14510 else if (STRICMP(name, "perl") == 0)
14511 n = perl_enabled(FALSE);
14512#endif
14513#ifdef FEAT_GUI
14514 else if (STRICMP(name, "gui_running") == 0)
14515 n = (gui.in_use || gui.starting);
14516# ifdef FEAT_GUI_W32
14517 else if (STRICMP(name, "gui_win32s") == 0)
14518 n = gui_is_win32s();
14519# endif
14520# ifdef FEAT_BROWSE
14521 else if (STRICMP(name, "browse") == 0)
14522 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14523# endif
14524#endif
14525#ifdef FEAT_SYN_HL
14526 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014527 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528#endif
14529#if defined(WIN3264)
14530 else if (STRICMP(name, "win95") == 0)
14531 n = mch_windows95();
14532#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014533#ifdef FEAT_NETBEANS_INTG
14534 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014535 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 }
14538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540}
14541
14542/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014543 * "has_key()" function
14544 */
14545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014546f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014547{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014548 if (argvars[0].v_type != VAR_DICT)
14549 {
14550 EMSG(_(e_dictreq));
14551 return;
14552 }
14553 if (argvars[0].vval.v_dict == NULL)
14554 return;
14555
14556 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014557 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014558}
14559
14560/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014561 * "haslocaldir()" function
14562 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014564f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014565{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014566 win_T *wp = NULL;
14567
14568 wp = find_tabwin(&argvars[0], &argvars[1]);
14569 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014570}
14571
14572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 * "hasmapto()" function
14574 */
14575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014576f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577{
14578 char_u *name;
14579 char_u *mode;
14580 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014581 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014583 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014584 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 mode = (char_u *)"nvo";
14586 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014590 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592
Bram Moolenaar2c932302006-03-18 21:42:09 +000014593 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014594 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014596 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597}
14598
14599/*
14600 * "histadd()" function
14601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014603f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604{
14605#ifdef FEAT_CMDHIST
14606 int histype;
14607 char_u *str;
14608 char_u buf[NUMBUFLEN];
14609#endif
14610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 if (check_restricted() || check_secure())
14613 return;
14614#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014615 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14616 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 if (histype >= 0)
14618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014619 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620 if (*str != NUL)
14621 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014622 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 return;
14626 }
14627 }
14628#endif
14629}
14630
14631/*
14632 * "histdel()" function
14633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014635f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636{
14637#ifdef FEAT_CMDHIST
14638 int n;
14639 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014640 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14643 if (str == NULL)
14644 n = 0;
14645 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014647 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014648 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014650 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652 else
14653 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014655 get_tv_string_buf(&argvars[1], buf));
14656 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657#endif
14658}
14659
14660/*
14661 * "histget()" function
14662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014664f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665{
14666#ifdef FEAT_CMDHIST
14667 int type;
14668 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14672 if (str == NULL)
14673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 {
14676 type = get_histtype(str);
14677 if (argvars[1].v_type == VAR_UNKNOWN)
14678 idx = get_history_idx(type);
14679 else
14680 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14681 /* -1 on type error */
14682 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688}
14689
14690/*
14691 * "histnr()" function
14692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014694f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695{
14696 int i;
14697
14698#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014699 char_u *history = get_tv_string_chk(&argvars[0]);
14700
14701 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702 if (i >= HIST_CMD && i < HIST_COUNT)
14703 i = get_history_idx(i);
14704 else
14705#endif
14706 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014707 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708}
14709
14710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 * "highlightID(name)" function
14712 */
14713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014714f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014716 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717}
14718
14719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720 * "highlight_exists()" function
14721 */
14722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014723f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014724{
14725 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14726}
14727
14728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 * "hostname()" function
14730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014732f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733{
14734 char_u hostname[256];
14735
14736 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014737 rettv->v_type = VAR_STRING;
14738 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739}
14740
14741/*
14742 * iconv() function
14743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014745f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746{
14747#ifdef FEAT_MBYTE
14748 char_u buf1[NUMBUFLEN];
14749 char_u buf2[NUMBUFLEN];
14750 char_u *from, *to, *str;
14751 vimconv_T vimconv;
14752#endif
14753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 rettv->v_type = VAR_STRING;
14755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756
14757#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 str = get_tv_string(&argvars[0]);
14759 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14760 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 vimconv.vc_type = CONV_NONE;
14762 convert_setup(&vimconv, from, to);
14763
14764 /* If the encodings are equal, no conversion needed. */
14765 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014768 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769
14770 convert_setup(&vimconv, NULL, NULL);
14771 vim_free(from);
14772 vim_free(to);
14773#endif
14774}
14775
14776/*
14777 * "indent()" function
14778 */
14779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014780f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781{
14782 linenr_T lnum;
14783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789}
14790
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014791/*
14792 * "index()" function
14793 */
14794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014795f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014796{
Bram Moolenaar33570922005-01-25 22:26:29 +000014797 list_T *l;
14798 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014799 long idx = 0;
14800 int ic = FALSE;
14801
14802 rettv->vval.v_number = -1;
14803 if (argvars[0].v_type != VAR_LIST)
14804 {
14805 EMSG(_(e_listreq));
14806 return;
14807 }
14808 l = argvars[0].vval.v_list;
14809 if (l != NULL)
14810 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014811 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014812 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014814 int error = FALSE;
14815
Bram Moolenaar758711c2005-02-02 23:11:38 +000014816 /* Start at specified item. Use the cached index that list_find()
14817 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014818 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014819 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014820 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014821 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014822 if (error)
14823 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014824 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014825
Bram Moolenaar758711c2005-02-02 23:11:38 +000014826 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014827 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014828 {
14829 rettv->vval.v_number = idx;
14830 break;
14831 }
14832 }
14833}
14834
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835static int inputsecret_flag = 0;
14836
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014837static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014838
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014840 * This function is used by f_input() and f_inputdialog() functions. The third
14841 * argument to f_input() specifies the type of completion to use at the
14842 * prompt. The third argument to f_inputdialog() specifies the value to return
14843 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 */
14845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014846get_user_input(
14847 typval_T *argvars,
14848 typval_T *rettv,
14849 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014851 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 char_u *p = NULL;
14853 int c;
14854 char_u buf[NUMBUFLEN];
14855 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014856 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014857 int xp_type = EXPAND_NOTHING;
14858 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014860 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014861 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862
14863#ifdef NO_CONSOLE_INPUT
14864 /* While starting up, there is no place to enter text. */
14865 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867#endif
14868
14869 cmd_silent = FALSE; /* Want to see the prompt. */
14870 if (prompt != NULL)
14871 {
14872 /* Only the part of the message after the last NL is considered as
14873 * prompt for the command line */
14874 p = vim_strrchr(prompt, '\n');
14875 if (p == NULL)
14876 p = prompt;
14877 else
14878 {
14879 ++p;
14880 c = *p;
14881 *p = NUL;
14882 msg_start();
14883 msg_clr_eos();
14884 msg_puts_attr(prompt, echo_attr);
14885 msg_didout = FALSE;
14886 msg_starthere();
14887 *p = c;
14888 }
14889 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014891 if (argvars[1].v_type != VAR_UNKNOWN)
14892 {
14893 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14894 if (defstr != NULL)
14895 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014897 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014898 {
14899 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014900 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014901 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014902
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014903 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014904 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014905
Bram Moolenaar4463f292005-09-25 22:20:24 +000014906 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14907 if (xp_name == NULL)
14908 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014909
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014910 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014911
Bram Moolenaar4463f292005-09-25 22:20:24 +000014912 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14913 &xp_arg) == FAIL)
14914 return;
14915 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014916 }
14917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014919 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014920 int save_ex_normal_busy = ex_normal_busy;
14921 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014922 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014923 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14924 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014925 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014926 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014927 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014928 && argvars[1].v_type != VAR_UNKNOWN
14929 && argvars[2].v_type != VAR_UNKNOWN)
14930 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14931 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014932
14933 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014935 /* since the user typed this, no need to wait for return */
14936 need_wait_return = FALSE;
14937 msg_didout = FALSE;
14938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 cmd_silent = cmd_silent_save;
14940}
14941
14942/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014943 * "input()" function
14944 * Also handles inputsecret() when inputsecret is set.
14945 */
14946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014947f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014948{
14949 get_user_input(argvars, rettv, FALSE);
14950}
14951
14952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 * "inputdialog()" function
14954 */
14955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014956f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957{
14958#if defined(FEAT_GUI_TEXTDIALOG)
14959 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14960 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14961 {
14962 char_u *message;
14963 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014964 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014966 message = get_tv_string_chk(&argvars[0]);
14967 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014968 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014969 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 else
14971 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014972 if (message != NULL && defstr != NULL
14973 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014974 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014975 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 else
14977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014978 if (message != NULL && defstr != NULL
14979 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014980 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014981 rettv->vval.v_string = vim_strsave(
14982 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014986 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987 }
14988 else
14989#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014990 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991}
14992
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014993/*
14994 * "inputlist()" function
14995 */
14996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014997f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014998{
14999 listitem_T *li;
15000 int selected;
15001 int mouse_used;
15002
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015003#ifdef NO_CONSOLE_INPUT
15004 /* While starting up, there is no place to enter text. */
15005 if (no_console_input())
15006 return;
15007#endif
15008 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
15009 {
15010 EMSG2(_(e_listarg), "inputlist()");
15011 return;
15012 }
15013
15014 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000015015 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015016 lines_left = Rows; /* avoid more prompt */
15017 msg_scroll = TRUE;
15018 msg_clr_eos();
15019
15020 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
15021 {
15022 msg_puts(get_tv_string(&li->li_tv));
15023 msg_putchar('\n');
15024 }
15025
15026 /* Ask for choice. */
15027 selected = prompt_for_number(&mouse_used);
15028 if (mouse_used)
15029 selected -= lines_left;
15030
15031 rettv->vval.v_number = selected;
15032}
15033
15034
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
15036
15037/*
15038 * "inputrestore()" function
15039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015041f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042{
15043 if (ga_userinput.ga_len > 0)
15044 {
15045 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
15047 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015048 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 }
15050 else if (p_verbose > 1)
15051 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000015052 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 }
15055}
15056
15057/*
15058 * "inputsave()" function
15059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015061f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015063 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 if (ga_grow(&ga_userinput, 1) == OK)
15065 {
15066 save_typeahead((tasave_T *)(ga_userinput.ga_data)
15067 + ga_userinput.ga_len);
15068 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015069 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 }
15071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015072 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073}
15074
15075/*
15076 * "inputsecret()" function
15077 */
15078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015079f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080{
15081 ++cmdline_star;
15082 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 --cmdline_star;
15085 --inputsecret_flag;
15086}
15087
15088/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015089 * "insert()" function
15090 */
15091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015092f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015093{
15094 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015095 listitem_T *item;
15096 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015097 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015098
15099 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015100 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015101 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015102 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015103 {
15104 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015105 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015106 if (error)
15107 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015108
Bram Moolenaar758711c2005-02-02 23:11:38 +000015109 if (before == l->lv_len)
15110 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015111 else
15112 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015113 item = list_find(l, before);
15114 if (item == NULL)
15115 {
15116 EMSGN(_(e_listidx), before);
15117 l = NULL;
15118 }
15119 }
15120 if (l != NULL)
15121 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015122 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015123 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015124 }
15125 }
15126}
15127
15128/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015129 * "invert(expr)" function
15130 */
15131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015132f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015133{
15134 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15135}
15136
15137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 * "isdirectory()" function
15139 */
15140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015141f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144}
15145
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015146/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015147 * "islocked()" function
15148 */
15149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015150f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015151{
15152 lval_T lv;
15153 char_u *end;
15154 dictitem_T *di;
15155
15156 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015157 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15158 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015159 if (end != NULL && lv.ll_name != NULL)
15160 {
15161 if (*end != NUL)
15162 EMSG(_(e_trailing));
15163 else
15164 {
15165 if (lv.ll_tv == NULL)
15166 {
15167 if (check_changedtick(lv.ll_name))
15168 rettv->vval.v_number = 1; /* always locked */
15169 else
15170 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015171 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015172 if (di != NULL)
15173 {
15174 /* Consider a variable locked when:
15175 * 1. the variable itself is locked
15176 * 2. the value of the variable is locked.
15177 * 3. the List or Dict value is locked.
15178 */
15179 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15180 || tv_islocked(&di->di_tv));
15181 }
15182 }
15183 }
15184 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015185 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015186 else if (lv.ll_newkey != NULL)
15187 EMSG2(_(e_dictkey), lv.ll_newkey);
15188 else if (lv.ll_list != NULL)
15189 /* List item. */
15190 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15191 else
15192 /* Dictionary item. */
15193 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15194 }
15195 }
15196
15197 clear_lval(&lv);
15198}
15199
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015200#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15201/*
15202 * "isnan()" function
15203 */
15204 static void
15205f_isnan(typval_T *argvars, typval_T *rettv)
15206{
15207 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15208 && isnan(argvars[0].vval.v_float);
15209}
15210#endif
15211
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015212static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015213
15214/*
15215 * Turn a dict into a list:
15216 * "what" == 0: list of keys
15217 * "what" == 1: list of values
15218 * "what" == 2: list of items
15219 */
15220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015221dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015222{
Bram Moolenaar33570922005-01-25 22:26:29 +000015223 list_T *l2;
15224 dictitem_T *di;
15225 hashitem_T *hi;
15226 listitem_T *li;
15227 listitem_T *li2;
15228 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015229 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015230
Bram Moolenaar8c711452005-01-14 21:53:12 +000015231 if (argvars[0].v_type != VAR_DICT)
15232 {
15233 EMSG(_(e_dictreq));
15234 return;
15235 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015236 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015237 return;
15238
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015239 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015240 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015241
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015242 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015243 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015244 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015245 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015246 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015247 --todo;
15248 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015249
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015250 li = listitem_alloc();
15251 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015252 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015253 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015254
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015255 if (what == 0)
15256 {
15257 /* keys() */
15258 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015259 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015260 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15261 }
15262 else if (what == 1)
15263 {
15264 /* values() */
15265 copy_tv(&di->di_tv, &li->li_tv);
15266 }
15267 else
15268 {
15269 /* items() */
15270 l2 = list_alloc();
15271 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015272 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015273 li->li_tv.vval.v_list = l2;
15274 if (l2 == NULL)
15275 break;
15276 ++l2->lv_refcount;
15277
15278 li2 = listitem_alloc();
15279 if (li2 == NULL)
15280 break;
15281 list_append(l2, li2);
15282 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015283 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015284 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15285
15286 li2 = listitem_alloc();
15287 if (li2 == NULL)
15288 break;
15289 list_append(l2, li2);
15290 copy_tv(&di->di_tv, &li2->li_tv);
15291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015292 }
15293 }
15294}
15295
15296/*
15297 * "items(dict)" function
15298 */
15299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015300f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015301{
15302 dict_list(argvars, rettv, 2);
15303}
15304
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015305#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015306/*
15307 * Get the job from the argument.
15308 * Returns NULL if the job is invalid.
15309 */
15310 static job_T *
15311get_job_arg(typval_T *tv)
15312{
15313 job_T *job;
15314
15315 if (tv->v_type != VAR_JOB)
15316 {
15317 EMSG2(_(e_invarg2), get_tv_string(tv));
15318 return NULL;
15319 }
15320 job = tv->vval.v_job;
15321
15322 if (job == NULL)
15323 EMSG(_("E916: not a valid job"));
15324 return job;
15325}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015326
Bram Moolenaar835dc632016-02-07 14:27:38 +010015327/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015328 * "job_getchannel()" function
15329 */
15330 static void
15331f_job_getchannel(typval_T *argvars, typval_T *rettv)
15332{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015333 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015334
Bram Moolenaar65edff82016-02-21 16:40:11 +010015335 if (job != NULL)
15336 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015337 rettv->v_type = VAR_CHANNEL;
15338 rettv->vval.v_channel = job->jv_channel;
15339 if (job->jv_channel != NULL)
15340 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015341 }
15342}
15343
15344/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015345 * "job_info()" function
15346 */
15347 static void
15348f_job_info(typval_T *argvars, typval_T *rettv)
15349{
15350 job_T *job = get_job_arg(&argvars[0]);
15351
15352 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15353 job_info(job, rettv->vval.v_dict);
15354}
15355
15356/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015357 * "job_setoptions()" function
15358 */
15359 static void
15360f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15361{
15362 job_T *job = get_job_arg(&argvars[0]);
15363 jobopt_T opt;
15364
15365 if (job == NULL)
15366 return;
15367 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015368 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15369 job_set_options(job, &opt);
15370 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015371}
15372
15373/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015374 * "job_start()" function
15375 */
15376 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015377f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015378{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015379 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015380 if (check_restricted() || check_secure())
15381 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015382 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015383}
15384
15385/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015386 * "job_status()" function
15387 */
15388 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015389f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015390{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015391 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015392
Bram Moolenaar65edff82016-02-21 16:40:11 +010015393 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015394 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015395 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015396 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015397 }
15398}
15399
15400/*
15401 * "job_stop()" function
15402 */
15403 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015404f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015405{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015406 job_T *job = get_job_arg(&argvars[0]);
15407
15408 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015409 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015410}
15411#endif
15412
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015414 * "join()" function
15415 */
15416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015417f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015418{
15419 garray_T ga;
15420 char_u *sep;
15421
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015422 if (argvars[0].v_type != VAR_LIST)
15423 {
15424 EMSG(_(e_listreq));
15425 return;
15426 }
15427 if (argvars[0].vval.v_list == NULL)
15428 return;
15429 if (argvars[1].v_type == VAR_UNKNOWN)
15430 sep = (char_u *)" ";
15431 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015432 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015433
15434 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015435
15436 if (sep != NULL)
15437 {
15438 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015439 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015440 ga_append(&ga, NUL);
15441 rettv->vval.v_string = (char_u *)ga.ga_data;
15442 }
15443 else
15444 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015445}
15446
15447/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015448 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015449 */
15450 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015451f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015452{
15453 js_read_T reader;
15454
15455 reader.js_buf = get_tv_string(&argvars[0]);
15456 reader.js_fill = NULL;
15457 reader.js_used = 0;
15458 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15459 EMSG(_(e_invarg));
15460}
15461
15462/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015463 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015464 */
15465 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015466f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015467{
15468 rettv->v_type = VAR_STRING;
15469 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15470}
15471
15472/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015473 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015474 */
15475 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015476f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015477{
15478 js_read_T reader;
15479
15480 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015481 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015482 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015483 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015484 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015485}
15486
15487/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015488 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015489 */
15490 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015491f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015492{
15493 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015494 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015495}
15496
15497/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015498 * "keys()" function
15499 */
15500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015501f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015502{
15503 dict_list(argvars, rettv, 0);
15504}
15505
15506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 * "last_buffer_nr()" function.
15508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015510f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511{
15512 int n = 0;
15513 buf_T *buf;
15514
15515 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15516 if (n < buf->b_fnum)
15517 n = buf->b_fnum;
15518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015519 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015520}
15521
15522/*
15523 * "len()" function
15524 */
15525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015526f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015527{
15528 switch (argvars[0].v_type)
15529 {
15530 case VAR_STRING:
15531 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015532 rettv->vval.v_number = (varnumber_T)STRLEN(
15533 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015534 break;
15535 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015537 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015538 case VAR_DICT:
15539 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15540 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015541 case VAR_UNKNOWN:
15542 case VAR_SPECIAL:
15543 case VAR_FLOAT:
15544 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015545 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015546 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015547 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015548 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015549 break;
15550 }
15551}
15552
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015553static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015554
15555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015556libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015557{
15558#ifdef FEAT_LIBCALL
15559 char_u *string_in;
15560 char_u **string_result;
15561 int nr_result;
15562#endif
15563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015564 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015565 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015567
15568 if (check_restricted() || check_secure())
15569 return;
15570
15571#ifdef FEAT_LIBCALL
15572 /* The first two args must be strings, otherwise its meaningless */
15573 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15574 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015575 string_in = NULL;
15576 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015577 string_in = argvars[2].vval.v_string;
15578 if (type == VAR_NUMBER)
15579 string_result = NULL;
15580 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015581 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015582 if (mch_libcall(argvars[0].vval.v_string,
15583 argvars[1].vval.v_string,
15584 string_in,
15585 argvars[2].vval.v_number,
15586 string_result,
15587 &nr_result) == OK
15588 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015589 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015590 }
15591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592}
15593
15594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015595 * "libcall()" function
15596 */
15597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015598f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015599{
15600 libcall_common(argvars, rettv, VAR_STRING);
15601}
15602
15603/*
15604 * "libcallnr()" function
15605 */
15606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015607f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015608{
15609 libcall_common(argvars, rettv, VAR_NUMBER);
15610}
15611
15612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613 * "line(string)" function
15614 */
15615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015616f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617{
15618 linenr_T lnum = 0;
15619 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015620 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015622 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 if (fp != NULL)
15624 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015625 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626}
15627
15628/*
15629 * "line2byte(lnum)" function
15630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015632f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633{
15634#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015635 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636#else
15637 linenr_T lnum;
15638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015639 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015641 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015643 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15644 if (rettv->vval.v_number >= 0)
15645 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646#endif
15647}
15648
15649/*
15650 * "lispindent(lnum)" function
15651 */
15652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015653f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654{
15655#ifdef FEAT_LISP
15656 pos_T pos;
15657 linenr_T lnum;
15658
15659 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15662 {
15663 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015664 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 curwin->w_cursor = pos;
15666 }
15667 else
15668#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015669 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670}
15671
15672/*
15673 * "localtime()" function
15674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015676f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015678 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679}
15680
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015681static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682
15683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015684get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685{
15686 char_u *keys;
15687 char_u *which;
15688 char_u buf[NUMBUFLEN];
15689 char_u *keys_buf = NULL;
15690 char_u *rhs;
15691 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015692 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015693 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015694 mapblock_T *mp;
15695 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696
15697 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015698 rettv->v_type = VAR_STRING;
15699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015701 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 if (*keys == NUL)
15703 return;
15704
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015705 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015707 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015708 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015709 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015710 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015711 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015712 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015713 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 else
15716 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015717 if (which == NULL)
15718 return;
15719
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 mode = get_map_mode(&which, 0);
15721
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015722 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015723 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015725
15726 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015728 /* Return a string. */
15729 if (rhs != NULL)
15730 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731
Bram Moolenaarbd743252010-10-20 21:23:33 +020015732 }
15733 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15734 {
15735 /* Return a dictionary. */
15736 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15737 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15738 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739
Bram Moolenaarbd743252010-10-20 21:23:33 +020015740 dict_add_nr_str(dict, "lhs", 0L, lhs);
15741 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15742 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15743 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15744 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15745 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15746 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015747 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015748 dict_add_nr_str(dict, "mode", 0L, mapmode);
15749
15750 vim_free(lhs);
15751 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 }
15753}
15754
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015755#ifdef FEAT_FLOAT
15756/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015757 * "log()" function
15758 */
15759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015760f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015761{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015762 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015763
15764 rettv->v_type = VAR_FLOAT;
15765 if (get_float_arg(argvars, &f) == OK)
15766 rettv->vval.v_float = log(f);
15767 else
15768 rettv->vval.v_float = 0.0;
15769}
15770
15771/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015772 * "log10()" function
15773 */
15774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015775f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015776{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015777 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015778
15779 rettv->v_type = VAR_FLOAT;
15780 if (get_float_arg(argvars, &f) == OK)
15781 rettv->vval.v_float = log10(f);
15782 else
15783 rettv->vval.v_float = 0.0;
15784}
15785#endif
15786
Bram Moolenaar1dced572012-04-05 16:54:08 +020015787#ifdef FEAT_LUA
15788/*
15789 * "luaeval()" function
15790 */
15791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015792f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015793{
15794 char_u *str;
15795 char_u buf[NUMBUFLEN];
15796
15797 str = get_tv_string_buf(&argvars[0], buf);
15798 do_luaeval(str, argvars + 1, rettv);
15799}
15800#endif
15801
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015803 * "map()" function
15804 */
15805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015806f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015807{
15808 filter_map(argvars, rettv, TRUE);
15809}
15810
15811/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015812 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813 */
15814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015815f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015817 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818}
15819
15820/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822 */
15823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015824f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015826 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827}
15828
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015829static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830
15831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015832find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015834 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015835 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015836 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837 char_u *pat;
15838 regmatch_T regmatch;
15839 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015840 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 char_u *save_cpo;
15842 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015843 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015844 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015845 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015846 list_T *l = NULL;
15847 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015848 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015849 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850
15851 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15852 save_cpo = p_cpo;
15853 p_cpo = (char_u *)"";
15854
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015855 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015856 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015857 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015858 /* type 3: return empty list when there are no matches.
15859 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015860 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015861 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015862 if (type == 4
15863 && (list_append_string(rettv->vval.v_list,
15864 (char_u *)"", 0) == FAIL
15865 || list_append_number(rettv->vval.v_list,
15866 (varnumber_T)-1) == FAIL
15867 || list_append_number(rettv->vval.v_list,
15868 (varnumber_T)-1) == FAIL
15869 || list_append_number(rettv->vval.v_list,
15870 (varnumber_T)-1) == FAIL))
15871 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015872 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015873 rettv->vval.v_list = NULL;
15874 goto theend;
15875 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015876 }
15877 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015879 rettv->v_type = VAR_STRING;
15880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015883 if (argvars[0].v_type == VAR_LIST)
15884 {
15885 if ((l = argvars[0].vval.v_list) == NULL)
15886 goto theend;
15887 li = l->lv_first;
15888 }
15889 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015890 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015891 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015892 len = (long)STRLEN(str);
15893 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15896 if (pat == NULL)
15897 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015898
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015899 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015901 int error = FALSE;
15902
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015903 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015904 if (error)
15905 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015906 if (l != NULL)
15907 {
15908 li = list_find(l, start);
15909 if (li == NULL)
15910 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015911 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015912 }
15913 else
15914 {
15915 if (start < 0)
15916 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015917 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015918 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015919 /* When "count" argument is there ignore matches before "start",
15920 * otherwise skip part of the string. Differs when pattern is "^"
15921 * or "\<". */
15922 if (argvars[3].v_type != VAR_UNKNOWN)
15923 startcol = start;
15924 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015925 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015926 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015927 len -= start;
15928 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015929 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015930
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015931 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015932 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015933 if (error)
15934 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935 }
15936
15937 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15938 if (regmatch.regprog != NULL)
15939 {
15940 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015941
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015942 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015943 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015944 if (l != NULL)
15945 {
15946 if (li == NULL)
15947 {
15948 match = FALSE;
15949 break;
15950 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015951 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015952 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015953 if (str == NULL)
15954 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015955 }
15956
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015957 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015958
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015959 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015960 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015961 if (l == NULL && !match)
15962 break;
15963
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015964 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015965 if (l != NULL)
15966 {
15967 li = li->li_next;
15968 ++idx;
15969 }
15970 else
15971 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015972#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015973 startcol = (colnr_T)(regmatch.startp[0]
15974 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015975#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015976 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015977#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015978 if (startcol > (colnr_T)len
15979 || str + startcol <= regmatch.startp[0])
15980 {
15981 match = FALSE;
15982 break;
15983 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015984 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015985 }
15986
15987 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015989 if (type == 4)
15990 {
15991 listitem_T *li1 = rettv->vval.v_list->lv_first;
15992 listitem_T *li2 = li1->li_next;
15993 listitem_T *li3 = li2->li_next;
15994 listitem_T *li4 = li3->li_next;
15995
Bram Moolenaar3c809342016-06-01 22:34:48 +020015996 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015997 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15998 (int)(regmatch.endp[0] - regmatch.startp[0]));
15999 li3->li_tv.vval.v_number =
16000 (varnumber_T)(regmatch.startp[0] - expr);
16001 li4->li_tv.vval.v_number =
16002 (varnumber_T)(regmatch.endp[0] - expr);
16003 if (l != NULL)
16004 li2->li_tv.vval.v_number = (varnumber_T)idx;
16005 }
16006 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016007 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016008 int i;
16009
16010 /* return list with matched string and submatches */
16011 for (i = 0; i < NSUBEXP; ++i)
16012 {
16013 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016014 {
16015 if (list_append_string(rettv->vval.v_list,
16016 (char_u *)"", 0) == FAIL)
16017 break;
16018 }
16019 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016020 regmatch.startp[i],
16021 (int)(regmatch.endp[i] - regmatch.startp[i]))
16022 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016023 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016024 }
16025 }
16026 else if (type == 2)
16027 {
16028 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016029 if (l != NULL)
16030 copy_tv(&li->li_tv, rettv);
16031 else
16032 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016034 }
16035 else if (l != NULL)
16036 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 else
16038 {
16039 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016040 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 (varnumber_T)(regmatch.startp[0] - str);
16042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016043 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016045 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046 }
16047 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016048 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 }
16050
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016051 if (type == 4 && l == NULL)
16052 /* matchstrpos() without a list: drop the second item. */
16053 listitem_remove(rettv->vval.v_list,
16054 rettv->vval.v_list->lv_first->li_next);
16055
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016057 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 p_cpo = save_cpo;
16059}
16060
16061/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016062 * "match()" function
16063 */
16064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016065f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016066{
16067 find_some_match(argvars, rettv, 1);
16068}
16069
16070/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016071 * "matchadd()" function
16072 */
16073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016074f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016075{
16076#ifdef FEAT_SEARCH_EXTRA
16077 char_u buf[NUMBUFLEN];
16078 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16079 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16080 int prio = 10; /* default priority */
16081 int id = -1;
16082 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016083 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016084
16085 rettv->vval.v_number = -1;
16086
16087 if (grp == NULL || pat == NULL)
16088 return;
16089 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016090 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016091 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016092 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016093 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016094 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016095 if (argvars[4].v_type != VAR_UNKNOWN)
16096 {
16097 if (argvars[4].v_type != VAR_DICT)
16098 {
16099 EMSG(_(e_dictreq));
16100 return;
16101 }
16102 if (dict_find(argvars[4].vval.v_dict,
16103 (char_u *)"conceal", -1) != NULL)
16104 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16105 (char_u *)"conceal", FALSE);
16106 }
16107 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016108 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016109 if (error == TRUE)
16110 return;
16111 if (id >= 1 && id <= 3)
16112 {
16113 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16114 return;
16115 }
16116
Bram Moolenaar6561d522015-07-21 15:48:27 +020016117 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16118 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016119#endif
16120}
16121
16122/*
16123 * "matchaddpos()" function
16124 */
16125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016126f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016127{
16128#ifdef FEAT_SEARCH_EXTRA
16129 char_u buf[NUMBUFLEN];
16130 char_u *group;
16131 int prio = 10;
16132 int id = -1;
16133 int error = FALSE;
16134 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016135 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016136
16137 rettv->vval.v_number = -1;
16138
16139 group = get_tv_string_buf_chk(&argvars[0], buf);
16140 if (group == NULL)
16141 return;
16142
16143 if (argvars[1].v_type != VAR_LIST)
16144 {
16145 EMSG2(_(e_listarg), "matchaddpos()");
16146 return;
16147 }
16148 l = argvars[1].vval.v_list;
16149 if (l == NULL)
16150 return;
16151
16152 if (argvars[2].v_type != VAR_UNKNOWN)
16153 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016154 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016155 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016156 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016157 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016158 if (argvars[4].v_type != VAR_UNKNOWN)
16159 {
16160 if (argvars[4].v_type != VAR_DICT)
16161 {
16162 EMSG(_(e_dictreq));
16163 return;
16164 }
16165 if (dict_find(argvars[4].vval.v_dict,
16166 (char_u *)"conceal", -1) != NULL)
16167 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16168 (char_u *)"conceal", FALSE);
16169 }
16170 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016171 }
16172 if (error == TRUE)
16173 return;
16174
16175 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16176 if (id == 1 || id == 2)
16177 {
16178 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16179 return;
16180 }
16181
Bram Moolenaar6561d522015-07-21 15:48:27 +020016182 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16183 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016184#endif
16185}
16186
16187/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016188 * "matcharg()" function
16189 */
16190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016191f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016192{
16193 if (rettv_list_alloc(rettv) == OK)
16194 {
16195#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016196 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016197 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016198
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016199 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016200 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016201 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16202 {
16203 list_append_string(rettv->vval.v_list,
16204 syn_id2name(m->hlg_id), -1);
16205 list_append_string(rettv->vval.v_list, m->pattern, -1);
16206 }
16207 else
16208 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016209 list_append_string(rettv->vval.v_list, NULL, -1);
16210 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016211 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016212 }
16213#endif
16214 }
16215}
16216
16217/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016218 * "matchdelete()" function
16219 */
16220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016221f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016222{
16223#ifdef FEAT_SEARCH_EXTRA
16224 rettv->vval.v_number = match_delete(curwin,
16225 (int)get_tv_number(&argvars[0]), TRUE);
16226#endif
16227}
16228
16229/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016230 * "matchend()" function
16231 */
16232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016233f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016234{
16235 find_some_match(argvars, rettv, 0);
16236}
16237
16238/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016239 * "matchlist()" function
16240 */
16241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016242f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016243{
16244 find_some_match(argvars, rettv, 3);
16245}
16246
16247/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016248 * "matchstr()" function
16249 */
16250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252{
16253 find_some_match(argvars, rettv, 2);
16254}
16255
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016256/*
16257 * "matchstrpos()" function
16258 */
16259 static void
16260f_matchstrpos(typval_T *argvars, typval_T *rettv)
16261{
16262 find_some_match(argvars, rettv, 4);
16263}
16264
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016265static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016266
16267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016268max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016269{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016270 varnumber_T n = 0;
16271 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016272 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016273
16274 if (argvars[0].v_type == VAR_LIST)
16275 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016276 list_T *l;
16277 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016278
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016279 l = argvars[0].vval.v_list;
16280 if (l != NULL)
16281 {
16282 li = l->lv_first;
16283 if (li != NULL)
16284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016285 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016286 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016287 {
16288 li = li->li_next;
16289 if (li == NULL)
16290 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016291 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016292 if (domax ? i > n : i < n)
16293 n = i;
16294 }
16295 }
16296 }
16297 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016298 else if (argvars[0].v_type == VAR_DICT)
16299 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016300 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016301 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016303 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016304
16305 d = argvars[0].vval.v_dict;
16306 if (d != NULL)
16307 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016308 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016309 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016311 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016312 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016313 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016314 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016315 if (first)
16316 {
16317 n = i;
16318 first = FALSE;
16319 }
16320 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016321 n = i;
16322 }
16323 }
16324 }
16325 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016326 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016327 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016328 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016329}
16330
16331/*
16332 * "max()" function
16333 */
16334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016335f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016336{
16337 max_min(argvars, rettv, TRUE);
16338}
16339
16340/*
16341 * "min()" function
16342 */
16343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016344f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016345{
16346 max_min(argvars, rettv, FALSE);
16347}
16348
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016349static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016350
16351/*
16352 * Create the directory in which "dir" is located, and higher levels when
16353 * needed.
16354 */
16355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016356mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016357{
16358 char_u *p;
16359 char_u *updir;
16360 int r = FAIL;
16361
16362 /* Get end of directory name in "dir".
16363 * We're done when it's "/" or "c:/". */
16364 p = gettail_sep(dir);
16365 if (p <= get_past_head(dir))
16366 return OK;
16367
16368 /* If the directory exists we're done. Otherwise: create it.*/
16369 updir = vim_strnsave(dir, (int)(p - dir));
16370 if (updir == NULL)
16371 return FAIL;
16372 if (mch_isdir(updir))
16373 r = OK;
16374 else if (mkdir_recurse(updir, prot) == OK)
16375 r = vim_mkdir_emsg(updir, prot);
16376 vim_free(updir);
16377 return r;
16378}
16379
16380#ifdef vim_mkdir
16381/*
16382 * "mkdir()" function
16383 */
16384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016385f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016386{
16387 char_u *dir;
16388 char_u buf[NUMBUFLEN];
16389 int prot = 0755;
16390
16391 rettv->vval.v_number = FAIL;
16392 if (check_restricted() || check_secure())
16393 return;
16394
16395 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016396 if (*dir == NUL)
16397 rettv->vval.v_number = FAIL;
16398 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016399 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016400 if (*gettail(dir) == NUL)
16401 /* remove trailing slashes */
16402 *gettail_sep(dir) = NUL;
16403
16404 if (argvars[1].v_type != VAR_UNKNOWN)
16405 {
16406 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016407 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016408 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16409 mkdir_recurse(dir, prot);
16410 }
16411 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016412 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016413}
16414#endif
16415
Bram Moolenaar0d660222005-01-07 21:51:51 +000016416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 * "mode()" function
16418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016420f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016422 char_u buf[3];
16423
16424 buf[1] = NUL;
16425 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016427 if (time_for_testing == 93784)
16428 {
16429 /* Testing the two-character code. */
16430 buf[0] = 'x';
16431 buf[1] = '!';
16432 }
16433 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434 {
16435 if (VIsual_select)
16436 buf[0] = VIsual_mode + 's' - 'v';
16437 else
16438 buf[0] = VIsual_mode;
16439 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016440 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016441 || State == CONFIRM)
16442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016444 if (State == ASKMORE)
16445 buf[1] = 'm';
16446 else if (State == CONFIRM)
16447 buf[1] = '?';
16448 }
16449 else if (State == EXTERNCMD)
16450 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 else if (State & INSERT)
16452 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016453#ifdef FEAT_VREPLACE
16454 if (State & VREPLACE_FLAG)
16455 {
16456 buf[0] = 'R';
16457 buf[1] = 'v';
16458 }
16459 else
16460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461 if (State & REPLACE_FLAG)
16462 buf[0] = 'R';
16463 else
16464 buf[0] = 'i';
16465 }
16466 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016469 if (exmode_active)
16470 buf[1] = 'v';
16471 }
16472 else if (exmode_active)
16473 {
16474 buf[0] = 'c';
16475 buf[1] = 'e';
16476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016480 if (finish_op)
16481 buf[1] = 'o';
16482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016484 /* Clear out the minor mode when the argument is not a non-zero number or
16485 * non-empty string. */
16486 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016487 buf[1] = NUL;
16488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016489 rettv->vval.v_string = vim_strsave(buf);
16490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491}
16492
Bram Moolenaar429fa852013-04-15 12:27:36 +020016493#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016494/*
16495 * "mzeval()" function
16496 */
16497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016498f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016499{
16500 char_u *str;
16501 char_u buf[NUMBUFLEN];
16502
16503 str = get_tv_string_buf(&argvars[0], buf);
16504 do_mzeval(str, rettv);
16505}
Bram Moolenaar75676462013-01-30 14:55:42 +010016506
16507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016508mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016509{
16510 typval_T argvars[3];
16511
16512 argvars[0].v_type = VAR_STRING;
16513 argvars[0].vval.v_string = name;
16514 copy_tv(args, &argvars[1]);
16515 argvars[2].v_type = VAR_UNKNOWN;
16516 f_call(argvars, rettv);
16517 clear_tv(&argvars[1]);
16518}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016519#endif
16520
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 * "nextnonblank()" function
16523 */
16524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016525f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016526{
16527 linenr_T lnum;
16528
16529 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532 {
16533 lnum = 0;
16534 break;
16535 }
16536 if (*skipwhite(ml_get(lnum)) != NUL)
16537 break;
16538 }
16539 rettv->vval.v_number = lnum;
16540}
16541
16542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 * "nr2char()" function
16544 */
16545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016546f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547{
16548 char_u buf[NUMBUFLEN];
16549
16550#ifdef FEAT_MBYTE
16551 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016552 {
16553 int utf8 = 0;
16554
16555 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016556 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016557 if (utf8)
16558 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16559 else
16560 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 else
16563#endif
16564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016565 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 buf[1] = NUL;
16567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016568 rettv->v_type = VAR_STRING;
16569 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016570}
16571
16572/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016573 * "or(expr, expr)" function
16574 */
16575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016576f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016577{
16578 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16579 | get_tv_number_chk(&argvars[1], NULL);
16580}
16581
16582/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016583 * "pathshorten()" function
16584 */
16585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016586f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016587{
16588 char_u *p;
16589
16590 rettv->v_type = VAR_STRING;
16591 p = get_tv_string_chk(&argvars[0]);
16592 if (p == NULL)
16593 rettv->vval.v_string = NULL;
16594 else
16595 {
16596 p = vim_strsave(p);
16597 rettv->vval.v_string = p;
16598 if (p != NULL)
16599 shorten_dir(p);
16600 }
16601}
16602
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016603#ifdef FEAT_PERL
16604/*
16605 * "perleval()" function
16606 */
16607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016608f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016609{
16610 char_u *str;
16611 char_u buf[NUMBUFLEN];
16612
16613 str = get_tv_string_buf(&argvars[0], buf);
16614 do_perleval(str, rettv);
16615}
16616#endif
16617
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016618#ifdef FEAT_FLOAT
16619/*
16620 * "pow()" function
16621 */
16622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016623f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016624{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016625 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016626
16627 rettv->v_type = VAR_FLOAT;
16628 if (get_float_arg(argvars, &fx) == OK
16629 && get_float_arg(&argvars[1], &fy) == OK)
16630 rettv->vval.v_float = pow(fx, fy);
16631 else
16632 rettv->vval.v_float = 0.0;
16633}
16634#endif
16635
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016636/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016637 * "prevnonblank()" function
16638 */
16639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016640f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016641{
16642 linenr_T lnum;
16643
16644 lnum = get_tv_lnum(argvars);
16645 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16646 lnum = 0;
16647 else
16648 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16649 --lnum;
16650 rettv->vval.v_number = lnum;
16651}
16652
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016653/* This dummy va_list is here because:
16654 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16655 * - locally in the function results in a "used before set" warning
16656 * - using va_start() to initialize it gives "function with fixed args" error */
16657static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016658
Bram Moolenaar8c711452005-01-14 21:53:12 +000016659/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016660 * "printf()" function
16661 */
16662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016663f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016664{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016665 char_u buf[NUMBUFLEN];
16666 int len;
16667 char_u *s;
16668 int saved_did_emsg = did_emsg;
16669 char *fmt;
16670
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016671 rettv->v_type = VAR_STRING;
16672 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016673
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016674 /* Get the required length, allocate the buffer and do it for real. */
16675 did_emsg = FALSE;
16676 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16677 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16678 if (!did_emsg)
16679 {
16680 s = alloc(len + 1);
16681 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016682 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016683 rettv->vval.v_string = s;
16684 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016685 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016686 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016687 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016688}
16689
16690/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016691 * "pumvisible()" function
16692 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016694f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016695{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016696#ifdef FEAT_INS_EXPAND
16697 if (pum_visible())
16698 rettv->vval.v_number = 1;
16699#endif
16700}
16701
Bram Moolenaardb913952012-06-29 12:54:53 +020016702#ifdef FEAT_PYTHON3
16703/*
16704 * "py3eval()" function
16705 */
16706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016707f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016708{
16709 char_u *str;
16710 char_u buf[NUMBUFLEN];
16711
16712 str = get_tv_string_buf(&argvars[0], buf);
16713 do_py3eval(str, rettv);
16714}
16715#endif
16716
16717#ifdef FEAT_PYTHON
16718/*
16719 * "pyeval()" function
16720 */
16721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016722f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016723{
16724 char_u *str;
16725 char_u buf[NUMBUFLEN];
16726
16727 str = get_tv_string_buf(&argvars[0], buf);
16728 do_pyeval(str, rettv);
16729}
16730#endif
16731
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016732/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016733 * "range()" function
16734 */
16735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016736f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016737{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016738 varnumber_T start;
16739 varnumber_T end;
16740 varnumber_T stride = 1;
16741 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016744 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016745 if (argvars[1].v_type == VAR_UNKNOWN)
16746 {
16747 end = start - 1;
16748 start = 0;
16749 }
16750 else
16751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016752 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016753 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016754 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016755 }
16756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016757 if (error)
16758 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016759 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016760 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016761 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016762 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016763 else
16764 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016765 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016766 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016767 if (list_append_number(rettv->vval.v_list,
16768 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016769 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016770 }
16771}
16772
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016773/*
16774 * "readfile()" function
16775 */
16776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016777f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016778{
16779 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016780 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016781 char_u *fname;
16782 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016783 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16784 int io_size = sizeof(buf);
16785 int readlen; /* size of last fread() */
16786 char_u *prev = NULL; /* previously read bytes, if any */
16787 long prevlen = 0; /* length of data in prev */
16788 long prevsize = 0; /* size of prev buffer */
16789 long maxline = MAXLNUM;
16790 long cnt = 0;
16791 char_u *p; /* position in buf */
16792 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016793
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016794 if (argvars[1].v_type != VAR_UNKNOWN)
16795 {
16796 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16797 binary = TRUE;
16798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016799 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016800 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016801
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016802 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016803 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016804
16805 /* Always open the file in binary mode, library functions have a mind of
16806 * their own about CR-LF conversion. */
16807 fname = get_tv_string(&argvars[0]);
16808 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16809 {
16810 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16811 return;
16812 }
16813
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016814 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016815 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016816 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016817
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016818 /* This for loop processes what was read, but is also entered at end
16819 * of file so that either:
16820 * - an incomplete line gets written
16821 * - a "binary" file gets an empty line at the end if it ends in a
16822 * newline. */
16823 for (p = buf, start = buf;
16824 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16825 ++p)
16826 {
16827 if (*p == '\n' || readlen <= 0)
16828 {
16829 listitem_T *li;
16830 char_u *s = NULL;
16831 long_u len = p - start;
16832
16833 /* Finished a line. Remove CRs before NL. */
16834 if (readlen > 0 && !binary)
16835 {
16836 while (len > 0 && start[len - 1] == '\r')
16837 --len;
16838 /* removal may cross back to the "prev" string */
16839 if (len == 0)
16840 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16841 --prevlen;
16842 }
16843 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016844 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016845 else
16846 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016847 /* Change "prev" buffer to be the right size. This way
16848 * the bytes are only copied once, and very long lines are
16849 * allocated only once. */
16850 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016851 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016852 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016853 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016854 prev = NULL; /* the list will own the string */
16855 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016856 }
16857 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016858 if (s == NULL)
16859 {
16860 do_outofmem_msg((long_u) prevlen + len + 1);
16861 failed = TRUE;
16862 break;
16863 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016864
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016865 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016866 {
16867 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016868 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016869 break;
16870 }
16871 li->li_tv.v_type = VAR_STRING;
16872 li->li_tv.v_lock = 0;
16873 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016874 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016875
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016876 start = p + 1; /* step over newline */
16877 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016878 break;
16879 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016880 else if (*p == NUL)
16881 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016882#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016883 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16884 * when finding the BF and check the previous two bytes. */
16885 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016886 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016887 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16888 * + 1, these may be in the "prev" string. */
16889 char_u back1 = p >= buf + 1 ? p[-1]
16890 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16891 char_u back2 = p >= buf + 2 ? p[-2]
16892 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16893 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016894
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016895 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016896 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016897 char_u *dest = p - 2;
16898
16899 /* Usually a BOM is at the beginning of a file, and so at
16900 * the beginning of a line; then we can just step over it.
16901 */
16902 if (start == dest)
16903 start = p + 1;
16904 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016905 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016906 /* have to shuffle buf to close gap */
16907 int adjust_prevlen = 0;
16908
16909 if (dest < buf)
16910 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016911 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016912 dest = buf;
16913 }
16914 if (readlen > p - buf + 1)
16915 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16916 readlen -= 3 - adjust_prevlen;
16917 prevlen -= adjust_prevlen;
16918 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016919 }
16920 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016921 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016922#endif
16923 } /* for */
16924
16925 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16926 break;
16927 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016928 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016929 /* There's part of a line in buf, store it in "prev". */
16930 if (p - start + prevlen >= prevsize)
16931 {
16932 /* need bigger "prev" buffer */
16933 char_u *newprev;
16934
16935 /* A common use case is ordinary text files and "prev" gets a
16936 * fragment of a line, so the first allocation is made
16937 * small, to avoid repeatedly 'allocing' large and
16938 * 'reallocing' small. */
16939 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016940 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016941 else
16942 {
16943 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016944 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016945 prevsize = grow50pc > growmin ? grow50pc : growmin;
16946 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016947 newprev = prev == NULL ? alloc(prevsize)
16948 : vim_realloc(prev, prevsize);
16949 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016950 {
16951 do_outofmem_msg((long_u)prevsize);
16952 failed = TRUE;
16953 break;
16954 }
16955 prev = newprev;
16956 }
16957 /* Add the line part to end of "prev". */
16958 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016959 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016960 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016961 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016962
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016963 /*
16964 * For a negative line count use only the lines at the end of the file,
16965 * free the rest.
16966 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016967 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016968 while (cnt > -maxline)
16969 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016970 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016971 --cnt;
16972 }
16973
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016974 if (failed)
16975 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016976 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016977 /* readfile doc says an empty list is returned on error */
16978 rettv->vval.v_list = list_alloc();
16979 }
16980
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016981 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016982 fclose(fd);
16983}
16984
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016985#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016986static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016987
16988/*
16989 * Convert a List to proftime_T.
16990 * Return FAIL when there is something wrong.
16991 */
16992 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016993list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016994{
16995 long n1, n2;
16996 int error = FALSE;
16997
16998 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16999 || arg->vval.v_list->lv_len != 2)
17000 return FAIL;
17001 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
17002 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
17003# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017004 tm->HighPart = n1;
17005 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017006# else
17007 tm->tv_sec = n1;
17008 tm->tv_usec = n2;
17009# endif
17010 return error ? FAIL : OK;
17011}
17012#endif /* FEAT_RELTIME */
17013
17014/*
17015 * "reltime()" function
17016 */
17017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017018f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017019{
17020#ifdef FEAT_RELTIME
17021 proftime_T res;
17022 proftime_T start;
17023
17024 if (argvars[0].v_type == VAR_UNKNOWN)
17025 {
17026 /* No arguments: get current time. */
17027 profile_start(&res);
17028 }
17029 else if (argvars[1].v_type == VAR_UNKNOWN)
17030 {
17031 if (list2proftime(&argvars[0], &res) == FAIL)
17032 return;
17033 profile_end(&res);
17034 }
17035 else
17036 {
17037 /* Two arguments: compute the difference. */
17038 if (list2proftime(&argvars[0], &start) == FAIL
17039 || list2proftime(&argvars[1], &res) == FAIL)
17040 return;
17041 profile_sub(&res, &start);
17042 }
17043
17044 if (rettv_list_alloc(rettv) == OK)
17045 {
17046 long n1, n2;
17047
17048# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017049 n1 = res.HighPart;
17050 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017051# else
17052 n1 = res.tv_sec;
17053 n2 = res.tv_usec;
17054# endif
17055 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17056 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17057 }
17058#endif
17059}
17060
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017061#ifdef FEAT_FLOAT
17062/*
17063 * "reltimefloat()" function
17064 */
17065 static void
17066f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17067{
17068# ifdef FEAT_RELTIME
17069 proftime_T tm;
17070# endif
17071
17072 rettv->v_type = VAR_FLOAT;
17073 rettv->vval.v_float = 0;
17074# ifdef FEAT_RELTIME
17075 if (list2proftime(&argvars[0], &tm) == OK)
17076 rettv->vval.v_float = profile_float(&tm);
17077# endif
17078}
17079#endif
17080
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017081/*
17082 * "reltimestr()" function
17083 */
17084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017085f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017086{
17087#ifdef FEAT_RELTIME
17088 proftime_T tm;
17089#endif
17090
17091 rettv->v_type = VAR_STRING;
17092 rettv->vval.v_string = NULL;
17093#ifdef FEAT_RELTIME
17094 if (list2proftime(&argvars[0], &tm) == OK)
17095 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17096#endif
17097}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017098
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017100static void make_connection(void);
17101static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017102
17103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017104make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105{
17106 if (X_DISPLAY == NULL
17107# ifdef FEAT_GUI
17108 && !gui.in_use
17109# endif
17110 )
17111 {
17112 x_force_connect = TRUE;
17113 setup_term_clip();
17114 x_force_connect = FALSE;
17115 }
17116}
17117
17118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017119check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017120{
17121 make_connection();
17122 if (X_DISPLAY == NULL)
17123 {
17124 EMSG(_("E240: No connection to Vim server"));
17125 return FAIL;
17126 }
17127 return OK;
17128}
17129#endif
17130
17131#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017133remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017134{
17135 char_u *server_name;
17136 char_u *keys;
17137 char_u *r = NULL;
17138 char_u buf[NUMBUFLEN];
17139# ifdef WIN32
17140 HWND w;
17141# else
17142 Window w;
17143# endif
17144
17145 if (check_restricted() || check_secure())
17146 return;
17147
17148# ifdef FEAT_X11
17149 if (check_connection() == FAIL)
17150 return;
17151# endif
17152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017153 server_name = get_tv_string_chk(&argvars[0]);
17154 if (server_name == NULL)
17155 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156 keys = get_tv_string_buf(&argvars[1], buf);
17157# ifdef WIN32
17158 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17159# else
17160 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17161 < 0)
17162# endif
17163 {
17164 if (r != NULL)
17165 EMSG(r); /* sending worked but evaluation failed */
17166 else
17167 EMSG2(_("E241: Unable to send to %s"), server_name);
17168 return;
17169 }
17170
17171 rettv->vval.v_string = r;
17172
17173 if (argvars[2].v_type != VAR_UNKNOWN)
17174 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017175 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017176 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017177 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017178
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017179 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017180 v.di_tv.v_type = VAR_STRING;
17181 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 idvar = get_tv_string_chk(&argvars[2]);
17183 if (idvar != NULL)
17184 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017185 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017186 }
17187}
17188#endif
17189
17190/*
17191 * "remote_expr()" function
17192 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017194f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017195{
17196 rettv->v_type = VAR_STRING;
17197 rettv->vval.v_string = NULL;
17198#ifdef FEAT_CLIENTSERVER
17199 remote_common(argvars, rettv, TRUE);
17200#endif
17201}
17202
17203/*
17204 * "remote_foreground()" function
17205 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017207f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017208{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017209#ifdef FEAT_CLIENTSERVER
17210# ifdef WIN32
17211 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017212 {
17213 char_u *server_name = get_tv_string_chk(&argvars[0]);
17214
17215 if (server_name != NULL)
17216 serverForeground(server_name);
17217 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017218# else
17219 /* Send a foreground() expression to the server. */
17220 argvars[1].v_type = VAR_STRING;
17221 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17222 argvars[2].v_type = VAR_UNKNOWN;
17223 remote_common(argvars, rettv, TRUE);
17224 vim_free(argvars[1].vval.v_string);
17225# endif
17226#endif
17227}
17228
Bram Moolenaar0d660222005-01-07 21:51:51 +000017229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017230f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017231{
17232#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017233 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017234 char_u *s = NULL;
17235# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017236 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017237# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017238 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017239
17240 if (check_restricted() || check_secure())
17241 {
17242 rettv->vval.v_number = -1;
17243 return;
17244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017245 serverid = get_tv_string_chk(&argvars[0]);
17246 if (serverid == NULL)
17247 {
17248 rettv->vval.v_number = -1;
17249 return; /* type error; errmsg already given */
17250 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017251# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017252 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017253 if (n == 0)
17254 rettv->vval.v_number = -1;
17255 else
17256 {
17257 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17258 rettv->vval.v_number = (s != NULL);
17259 }
17260# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017261 if (check_connection() == FAIL)
17262 return;
17263
17264 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017265 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017266# endif
17267
17268 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017270 char_u *retvar;
17271
Bram Moolenaar33570922005-01-25 22:26:29 +000017272 v.di_tv.v_type = VAR_STRING;
17273 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017274 retvar = get_tv_string_chk(&argvars[1]);
17275 if (retvar != NULL)
17276 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017277 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017278 }
17279#else
17280 rettv->vval.v_number = -1;
17281#endif
17282}
17283
Bram Moolenaar0d660222005-01-07 21:51:51 +000017284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017285f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017286{
17287 char_u *r = NULL;
17288
17289#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017290 char_u *serverid = get_tv_string_chk(&argvars[0]);
17291
17292 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017293 {
17294# ifdef WIN32
17295 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017296 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017297
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017298 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017299 if (n != 0)
17300 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17301 if (r == NULL)
17302# else
17303 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017304 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017305# endif
17306 EMSG(_("E277: Unable to read a server reply"));
17307 }
17308#endif
17309 rettv->v_type = VAR_STRING;
17310 rettv->vval.v_string = r;
17311}
17312
17313/*
17314 * "remote_send()" function
17315 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017317f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017318{
17319 rettv->v_type = VAR_STRING;
17320 rettv->vval.v_string = NULL;
17321#ifdef FEAT_CLIENTSERVER
17322 remote_common(argvars, rettv, FALSE);
17323#endif
17324}
17325
17326/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017327 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017328 */
17329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017330f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017331{
Bram Moolenaar33570922005-01-25 22:26:29 +000017332 list_T *l;
17333 listitem_T *item, *item2;
17334 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017335 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017336 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017337 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017338 dict_T *d;
17339 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017340 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017341
Bram Moolenaar8c711452005-01-14 21:53:12 +000017342 if (argvars[0].v_type == VAR_DICT)
17343 {
17344 if (argvars[2].v_type != VAR_UNKNOWN)
17345 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017346 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017347 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017349 key = get_tv_string_chk(&argvars[1]);
17350 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017352 di = dict_find(d, key, -1);
17353 if (di == NULL)
17354 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017355 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17356 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017357 {
17358 *rettv = di->di_tv;
17359 init_tv(&di->di_tv);
17360 dictitem_remove(d, di);
17361 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017362 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017363 }
17364 }
17365 else if (argvars[0].v_type != VAR_LIST)
17366 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017367 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017368 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017370 int error = FALSE;
17371
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017372 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017373 if (error)
17374 ; /* type error: do nothing, errmsg already given */
17375 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017376 EMSGN(_(e_listidx), idx);
17377 else
17378 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017379 if (argvars[2].v_type == VAR_UNKNOWN)
17380 {
17381 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017382 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017383 *rettv = item->li_tv;
17384 vim_free(item);
17385 }
17386 else
17387 {
17388 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017389 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017390 if (error)
17391 ; /* type error: do nothing */
17392 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017393 EMSGN(_(e_listidx), end);
17394 else
17395 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017396 int cnt = 0;
17397
17398 for (li = item; li != NULL; li = li->li_next)
17399 {
17400 ++cnt;
17401 if (li == item2)
17402 break;
17403 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017404 if (li == NULL) /* didn't find "item2" after "item" */
17405 EMSG(_(e_invrange));
17406 else
17407 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017408 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017409 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017410 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017411 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017412 l->lv_first = item;
17413 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017414 item->li_prev = NULL;
17415 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017416 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017417 }
17418 }
17419 }
17420 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017421 }
17422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423}
17424
17425/*
17426 * "rename({from}, {to})" function
17427 */
17428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017429f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430{
17431 char_u buf[NUMBUFLEN];
17432
17433 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017434 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17437 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438}
17439
17440/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017441 * "repeat()" function
17442 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017444f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017445{
17446 char_u *p;
17447 int n;
17448 int slen;
17449 int len;
17450 char_u *r;
17451 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017452
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017453 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017454 if (argvars[0].v_type == VAR_LIST)
17455 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017456 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017457 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017458 if (list_extend(rettv->vval.v_list,
17459 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017460 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017461 }
17462 else
17463 {
17464 p = get_tv_string(&argvars[0]);
17465 rettv->v_type = VAR_STRING;
17466 rettv->vval.v_string = NULL;
17467
17468 slen = (int)STRLEN(p);
17469 len = slen * n;
17470 if (len <= 0)
17471 return;
17472
17473 r = alloc(len + 1);
17474 if (r != NULL)
17475 {
17476 for (i = 0; i < n; i++)
17477 mch_memmove(r + i * slen, p, (size_t)slen);
17478 r[len] = NUL;
17479 }
17480
17481 rettv->vval.v_string = r;
17482 }
17483}
17484
17485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 * "resolve()" function
17487 */
17488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017489f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490{
17491 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017492#ifdef HAVE_READLINK
17493 char_u *buf = NULL;
17494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017496 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497#ifdef FEAT_SHORTCUT
17498 {
17499 char_u *v = NULL;
17500
17501 v = mch_resolve_shortcut(p);
17502 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017503 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017505 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 }
17507#else
17508# ifdef HAVE_READLINK
17509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 char_u *cpy;
17511 int len;
17512 char_u *remain = NULL;
17513 char_u *q;
17514 int is_relative_to_current = FALSE;
17515 int has_trailing_pathsep = FALSE;
17516 int limit = 100;
17517
17518 p = vim_strsave(p);
17519
17520 if (p[0] == '.' && (vim_ispathsep(p[1])
17521 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17522 is_relative_to_current = TRUE;
17523
17524 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017525 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017528 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530
17531 q = getnextcomp(p);
17532 if (*q != NUL)
17533 {
17534 /* Separate the first path component in "p", and keep the
17535 * remainder (beginning with the path separator). */
17536 remain = vim_strsave(q - 1);
17537 q[-1] = NUL;
17538 }
17539
Bram Moolenaard9462e32011-04-11 21:35:11 +020017540 buf = alloc(MAXPATHL + 1);
17541 if (buf == NULL)
17542 goto fail;
17543
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544 for (;;)
17545 {
17546 for (;;)
17547 {
17548 len = readlink((char *)p, (char *)buf, MAXPATHL);
17549 if (len <= 0)
17550 break;
17551 buf[len] = NUL;
17552
17553 if (limit-- == 0)
17554 {
17555 vim_free(p);
17556 vim_free(remain);
17557 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 goto fail;
17560 }
17561
17562 /* Ensure that the result will have a trailing path separator
17563 * if the argument has one. */
17564 if (remain == NULL && has_trailing_pathsep)
17565 add_pathsep(buf);
17566
17567 /* Separate the first path component in the link value and
17568 * concatenate the remainders. */
17569 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17570 if (*q != NUL)
17571 {
17572 if (remain == NULL)
17573 remain = vim_strsave(q - 1);
17574 else
17575 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017576 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 if (cpy != NULL)
17578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 vim_free(remain);
17580 remain = cpy;
17581 }
17582 }
17583 q[-1] = NUL;
17584 }
17585
17586 q = gettail(p);
17587 if (q > p && *q == NUL)
17588 {
17589 /* Ignore trailing path separator. */
17590 q[-1] = NUL;
17591 q = gettail(p);
17592 }
17593 if (q > p && !mch_isFullName(buf))
17594 {
17595 /* symlink is relative to directory of argument */
17596 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17597 if (cpy != NULL)
17598 {
17599 STRCPY(cpy, p);
17600 STRCPY(gettail(cpy), buf);
17601 vim_free(p);
17602 p = cpy;
17603 }
17604 }
17605 else
17606 {
17607 vim_free(p);
17608 p = vim_strsave(buf);
17609 }
17610 }
17611
17612 if (remain == NULL)
17613 break;
17614
17615 /* Append the first path component of "remain" to "p". */
17616 q = getnextcomp(remain + 1);
17617 len = q - remain - (*q != NUL);
17618 cpy = vim_strnsave(p, STRLEN(p) + len);
17619 if (cpy != NULL)
17620 {
17621 STRNCAT(cpy, remain, len);
17622 vim_free(p);
17623 p = cpy;
17624 }
17625 /* Shorten "remain". */
17626 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017627 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 else
17629 {
17630 vim_free(remain);
17631 remain = NULL;
17632 }
17633 }
17634
17635 /* If the result is a relative path name, make it explicitly relative to
17636 * the current directory if and only if the argument had this form. */
17637 if (!vim_ispathsep(*p))
17638 {
17639 if (is_relative_to_current
17640 && *p != NUL
17641 && !(p[0] == '.'
17642 && (p[1] == NUL
17643 || vim_ispathsep(p[1])
17644 || (p[1] == '.'
17645 && (p[2] == NUL
17646 || vim_ispathsep(p[2]))))))
17647 {
17648 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017649 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 if (cpy != NULL)
17651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 vim_free(p);
17653 p = cpy;
17654 }
17655 }
17656 else if (!is_relative_to_current)
17657 {
17658 /* Strip leading "./". */
17659 q = p;
17660 while (q[0] == '.' && vim_ispathsep(q[1]))
17661 q += 2;
17662 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017663 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 }
17665 }
17666
17667 /* Ensure that the result will have no trailing path separator
17668 * if the argument had none. But keep "/" or "//". */
17669 if (!has_trailing_pathsep)
17670 {
17671 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017672 if (after_pathsep(p, q))
17673 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 }
17675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 }
17678# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680# endif
17681#endif
17682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017683 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684
17685#ifdef HAVE_READLINK
17686fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017687 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017689 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690}
17691
17692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017693 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 */
17695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017696f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697{
Bram Moolenaar33570922005-01-25 22:26:29 +000017698 list_T *l;
17699 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700
Bram Moolenaar0d660222005-01-07 21:51:51 +000017701 if (argvars[0].v_type != VAR_LIST)
17702 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017703 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017704 && !tv_check_lock(l->lv_lock,
17705 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017706 {
17707 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017708 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017709 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017710 while (li != NULL)
17711 {
17712 ni = li->li_prev;
17713 list_append(l, li);
17714 li = ni;
17715 }
17716 rettv->vval.v_list = l;
17717 rettv->v_type = VAR_LIST;
17718 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017719 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721}
17722
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017723#define SP_NOMOVE 0x01 /* don't move cursor */
17724#define SP_REPEAT 0x02 /* repeat to find outer pair */
17725#define SP_RETCOUNT 0x04 /* return matchcount */
17726#define SP_SETPCMARK 0x08 /* set previous context mark */
17727#define SP_START 0x10 /* accept match at start position */
17728#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17729#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017730#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017731
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017732static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017733
17734/*
17735 * Get flags for a search function.
17736 * Possibly sets "p_ws".
17737 * Returns BACKWARD, FORWARD or zero (for an error).
17738 */
17739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017740get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017741{
17742 int dir = FORWARD;
17743 char_u *flags;
17744 char_u nbuf[NUMBUFLEN];
17745 int mask;
17746
17747 if (varp->v_type != VAR_UNKNOWN)
17748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017749 flags = get_tv_string_buf_chk(varp, nbuf);
17750 if (flags == NULL)
17751 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017752 while (*flags != NUL)
17753 {
17754 switch (*flags)
17755 {
17756 case 'b': dir = BACKWARD; break;
17757 case 'w': p_ws = TRUE; break;
17758 case 'W': p_ws = FALSE; break;
17759 default: mask = 0;
17760 if (flagsp != NULL)
17761 switch (*flags)
17762 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017763 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017764 case 'e': mask = SP_END; break;
17765 case 'm': mask = SP_RETCOUNT; break;
17766 case 'n': mask = SP_NOMOVE; break;
17767 case 'p': mask = SP_SUBPAT; break;
17768 case 'r': mask = SP_REPEAT; break;
17769 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017770 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017771 }
17772 if (mask == 0)
17773 {
17774 EMSG2(_(e_invarg2), flags);
17775 dir = 0;
17776 }
17777 else
17778 *flagsp |= mask;
17779 }
17780 if (dir == 0)
17781 break;
17782 ++flags;
17783 }
17784 }
17785 return dir;
17786}
17787
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017789 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017792search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017794 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 char_u *pat;
17796 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017797 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 int save_p_ws = p_ws;
17799 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017800 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017801 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017802 proftime_T tm;
17803#ifdef FEAT_RELTIME
17804 long time_limit = 0;
17805#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017806 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017807 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017809 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017810 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017811 if (dir == 0)
17812 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017813 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017814 if (flags & SP_START)
17815 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017816 if (flags & SP_END)
17817 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017818 if (flags & SP_COLUMN)
17819 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017820
Bram Moolenaar76929292008-01-06 19:07:36 +000017821 /* Optional arguments: line number to stop searching and timeout. */
17822 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017823 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017824 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017825 if (lnum_stop < 0)
17826 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017827#ifdef FEAT_RELTIME
17828 if (argvars[3].v_type != VAR_UNKNOWN)
17829 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017830 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017831 if (time_limit < 0)
17832 goto theend;
17833 }
17834#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017835 }
17836
Bram Moolenaar76929292008-01-06 19:07:36 +000017837#ifdef FEAT_RELTIME
17838 /* Set the time limit, if there is one. */
17839 profile_setlimit(time_limit, &tm);
17840#endif
17841
Bram Moolenaar231334e2005-07-25 20:46:57 +000017842 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017843 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017844 * Check to make sure only those flags are set.
17845 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17846 * flags cannot be set. Check for that condition also.
17847 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017848 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017849 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017851 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017852 goto theend;
17853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017855 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017856 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017857 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017858 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017860 if (flags & SP_SUBPAT)
17861 retval = subpatnum;
17862 else
17863 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017864 if (flags & SP_SETPCMARK)
17865 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017867 if (match_pos != NULL)
17868 {
17869 /* Store the match cursor position */
17870 match_pos->lnum = pos.lnum;
17871 match_pos->col = pos.col + 1;
17872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 /* "/$" will put the cursor after the end of the line, may need to
17874 * correct that here */
17875 check_cursor();
17876 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017877
17878 /* If 'n' flag is used: restore cursor position. */
17879 if (flags & SP_NOMOVE)
17880 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017881 else
17882 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017883theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017885
17886 return retval;
17887}
17888
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017889#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017890
17891/*
17892 * round() is not in C90, use ceil() or floor() instead.
17893 */
17894 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017895vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017896{
17897 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17898}
17899
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017900/*
17901 * "round({float})" function
17902 */
17903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017904f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017905{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017906 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017907
17908 rettv->v_type = VAR_FLOAT;
17909 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017910 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017911 else
17912 rettv->vval.v_float = 0.0;
17913}
17914#endif
17915
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017916/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017917 * "screenattr()" function
17918 */
17919 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017920f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017921{
17922 int row;
17923 int col;
17924 int c;
17925
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017926 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17927 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017928 if (row < 0 || row >= screen_Rows
17929 || col < 0 || col >= screen_Columns)
17930 c = -1;
17931 else
17932 c = ScreenAttrs[LineOffset[row] + col];
17933 rettv->vval.v_number = c;
17934}
17935
17936/*
17937 * "screenchar()" function
17938 */
17939 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017940f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017941{
17942 int row;
17943 int col;
17944 int off;
17945 int c;
17946
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017947 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17948 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017949 if (row < 0 || row >= screen_Rows
17950 || col < 0 || col >= screen_Columns)
17951 c = -1;
17952 else
17953 {
17954 off = LineOffset[row] + col;
17955#ifdef FEAT_MBYTE
17956 if (enc_utf8 && ScreenLinesUC[off] != 0)
17957 c = ScreenLinesUC[off];
17958 else
17959#endif
17960 c = ScreenLines[off];
17961 }
17962 rettv->vval.v_number = c;
17963}
17964
17965/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017966 * "screencol()" function
17967 *
17968 * First column is 1 to be consistent with virtcol().
17969 */
17970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017971f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017972{
17973 rettv->vval.v_number = screen_screencol() + 1;
17974}
17975
17976/*
17977 * "screenrow()" function
17978 */
17979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017980f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017981{
17982 rettv->vval.v_number = screen_screenrow() + 1;
17983}
17984
17985/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017986 * "search()" function
17987 */
17988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017989f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017990{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017991 int flags = 0;
17992
17993 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994}
17995
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017997 * "searchdecl()" function
17998 */
17999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018000f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018001{
18002 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018003 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018004 int error = FALSE;
18005 char_u *name;
18006
18007 rettv->vval.v_number = 1; /* default: FAIL */
18008
18009 name = get_tv_string_chk(&argvars[0]);
18010 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000018011 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018012 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018013 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018014 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018015 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018016 if (!error && name != NULL)
18017 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018018 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018019}
18020
18021/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018022 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018025searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026{
18027 char_u *spat, *mpat, *epat;
18028 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 int dir;
18031 int flags = 0;
18032 char_u nbuf1[NUMBUFLEN];
18033 char_u nbuf2[NUMBUFLEN];
18034 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018035 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018036 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018037 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018040 spat = get_tv_string_chk(&argvars[0]);
18041 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18042 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18043 if (spat == NULL || mpat == NULL || epat == NULL)
18044 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 /* Handle the optional fourth argument: flags */
18047 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018048 if (dir == 0)
18049 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018050
18051 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018052 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18053 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018054 if ((flags & (SP_END | SP_SUBPAT)) != 0
18055 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018056 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018057 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018058 goto theend;
18059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018061 /* Using 'r' implies 'W', otherwise it doesn't work. */
18062 if (flags & SP_REPEAT)
18063 p_ws = FALSE;
18064
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018065 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018066 if (argvars[3].v_type == VAR_UNKNOWN
18067 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 skip = (char_u *)"";
18069 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018071 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018072 if (argvars[5].v_type != VAR_UNKNOWN)
18073 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018074 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018075 if (lnum_stop < 0)
18076 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018077#ifdef FEAT_RELTIME
18078 if (argvars[6].v_type != VAR_UNKNOWN)
18079 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018080 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018081 if (time_limit < 0)
18082 goto theend;
18083 }
18084#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018085 }
18086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018087 if (skip == NULL)
18088 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018090 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018091 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018092
18093theend:
18094 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018095
18096 return retval;
18097}
18098
18099/*
18100 * "searchpair()" function
18101 */
18102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018103f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018104{
18105 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18106}
18107
18108/*
18109 * "searchpairpos()" function
18110 */
18111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018112f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018113{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018114 pos_T match_pos;
18115 int lnum = 0;
18116 int col = 0;
18117
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018118 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018119 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018120
18121 if (searchpair_cmn(argvars, &match_pos) > 0)
18122 {
18123 lnum = match_pos.lnum;
18124 col = match_pos.col;
18125 }
18126
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018127 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18128 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018129}
18130
18131/*
18132 * Search for a start/middle/end thing.
18133 * Used by searchpair(), see its documentation for the details.
18134 * Returns 0 or -1 for no match,
18135 */
18136 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018137do_searchpair(
18138 char_u *spat, /* start pattern */
18139 char_u *mpat, /* middle pattern */
18140 char_u *epat, /* end pattern */
18141 int dir, /* BACKWARD or FORWARD */
18142 char_u *skip, /* skip expression */
18143 int flags, /* SP_SETPCMARK and other SP_ values */
18144 pos_T *match_pos,
18145 linenr_T lnum_stop, /* stop at this line if not zero */
18146 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018147{
18148 char_u *save_cpo;
18149 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18150 long retval = 0;
18151 pos_T pos;
18152 pos_T firstpos;
18153 pos_T foundpos;
18154 pos_T save_cursor;
18155 pos_T save_pos;
18156 int n;
18157 int r;
18158 int nest = 1;
18159 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018160 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018161 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018162
18163 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18164 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018165 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018166
Bram Moolenaar76929292008-01-06 19:07:36 +000018167#ifdef FEAT_RELTIME
18168 /* Set the time limit, if there is one. */
18169 profile_setlimit(time_limit, &tm);
18170#endif
18171
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018172 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18173 * start/middle/end (pat3, for the top pair). */
18174 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18175 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18176 if (pat2 == NULL || pat3 == NULL)
18177 goto theend;
18178 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18179 if (*mpat == NUL)
18180 STRCPY(pat3, pat2);
18181 else
18182 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18183 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018184 if (flags & SP_START)
18185 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018186
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 save_cursor = curwin->w_cursor;
18188 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018189 clearpos(&firstpos);
18190 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 pat = pat3;
18192 for (;;)
18193 {
18194 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018195 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18197 /* didn't find it or found the first match again: FAIL */
18198 break;
18199
18200 if (firstpos.lnum == 0)
18201 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018202 if (equalpos(pos, foundpos))
18203 {
18204 /* Found the same position again. Can happen with a pattern that
18205 * has "\zs" at the end and searching backwards. Advance one
18206 * character and try again. */
18207 if (dir == BACKWARD)
18208 decl(&pos);
18209 else
18210 incl(&pos);
18211 }
18212 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018214 /* clear the start flag to avoid getting stuck here */
18215 options &= ~SEARCH_START;
18216
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 /* If the skip pattern matches, ignore this match. */
18218 if (*skip != NUL)
18219 {
18220 save_pos = curwin->w_cursor;
18221 curwin->w_cursor = pos;
18222 r = eval_to_bool(skip, &err, NULL, FALSE);
18223 curwin->w_cursor = save_pos;
18224 if (err)
18225 {
18226 /* Evaluating {skip} caused an error, break here. */
18227 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018228 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 break;
18230 }
18231 if (r)
18232 continue;
18233 }
18234
18235 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18236 {
18237 /* Found end when searching backwards or start when searching
18238 * forward: nested pair. */
18239 ++nest;
18240 pat = pat2; /* nested, don't search for middle */
18241 }
18242 else
18243 {
18244 /* Found end when searching forward or start when searching
18245 * backward: end of (nested) pair; or found middle in outer pair. */
18246 if (--nest == 1)
18247 pat = pat3; /* outer level, search for middle */
18248 }
18249
18250 if (nest == 0)
18251 {
18252 /* Found the match: return matchcount or line number. */
18253 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018254 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018256 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018257 if (flags & SP_SETPCMARK)
18258 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 curwin->w_cursor = pos;
18260 if (!(flags & SP_REPEAT))
18261 break;
18262 nest = 1; /* search for next unmatched */
18263 }
18264 }
18265
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018266 if (match_pos != NULL)
18267 {
18268 /* Store the match cursor position */
18269 match_pos->lnum = curwin->w_cursor.lnum;
18270 match_pos->col = curwin->w_cursor.col + 1;
18271 }
18272
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018274 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 curwin->w_cursor = save_cursor;
18276
18277theend:
18278 vim_free(pat2);
18279 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018280 if (p_cpo == empty_option)
18281 p_cpo = save_cpo;
18282 else
18283 /* Darn, evaluating the {skip} expression changed the value. */
18284 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018285
18286 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287}
18288
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018289/*
18290 * "searchpos()" function
18291 */
18292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018293f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018294{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018295 pos_T match_pos;
18296 int lnum = 0;
18297 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018298 int n;
18299 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018300
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018301 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018302 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018303
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018304 n = search_cmn(argvars, &match_pos, &flags);
18305 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018306 {
18307 lnum = match_pos.lnum;
18308 col = match_pos.col;
18309 }
18310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018311 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18312 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018313 if (flags & SP_SUBPAT)
18314 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018315}
18316
Bram Moolenaar0d660222005-01-07 21:51:51 +000018317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018318f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018320#ifdef FEAT_CLIENTSERVER
18321 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018322 char_u *server = get_tv_string_chk(&argvars[0]);
18323 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018326 if (server == NULL || reply == NULL)
18327 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018328 if (check_restricted() || check_secure())
18329 return;
18330# ifdef FEAT_X11
18331 if (check_connection() == FAIL)
18332 return;
18333# endif
18334
18335 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018337 EMSG(_("E258: Unable to send to client"));
18338 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018340 rettv->vval.v_number = 0;
18341#else
18342 rettv->vval.v_number = -1;
18343#endif
18344}
18345
Bram Moolenaar0d660222005-01-07 21:51:51 +000018346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018347f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018348{
18349 char_u *r = NULL;
18350
18351#ifdef FEAT_CLIENTSERVER
18352# ifdef WIN32
18353 r = serverGetVimNames();
18354# else
18355 make_connection();
18356 if (X_DISPLAY != NULL)
18357 r = serverGetVimNames(X_DISPLAY);
18358# endif
18359#endif
18360 rettv->v_type = VAR_STRING;
18361 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362}
18363
18364/*
18365 * "setbufvar()" function
18366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018368f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369{
18370 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018373 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 char_u nbuf[NUMBUFLEN];
18375
18376 if (check_restricted() || check_secure())
18377 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018378 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18379 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018380 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381 varp = &argvars[2];
18382
18383 if (buf != NULL && varname != NULL && varp != NULL)
18384 {
18385 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387
18388 if (*varname == '&')
18389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018390 long numval;
18391 char_u *strval;
18392 int error = FALSE;
18393
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018395 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018396 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018397 if (!error && strval != NULL)
18398 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 }
18400 else
18401 {
18402 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18403 if (bufvarname != NULL)
18404 {
18405 STRCPY(bufvarname, "b:");
18406 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018407 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 vim_free(bufvarname);
18409 }
18410 }
18411
18412 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415}
18416
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018418f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018419{
18420 dict_T *d;
18421 dictitem_T *di;
18422 char_u *csearch;
18423
18424 if (argvars[0].v_type != VAR_DICT)
18425 {
18426 EMSG(_(e_dictreq));
18427 return;
18428 }
18429
18430 if ((d = argvars[0].vval.v_dict) != NULL)
18431 {
18432 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18433 if (csearch != NULL)
18434 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018435#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018436 if (enc_utf8)
18437 {
18438 int pcc[MAX_MCO];
18439 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018440
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018441 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18442 }
18443 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018444#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018445 set_last_csearch(PTR2CHAR(csearch),
18446 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018447 }
18448
18449 di = dict_find(d, (char_u *)"forward", -1);
18450 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018451 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018452 ? FORWARD : BACKWARD);
18453
18454 di = dict_find(d, (char_u *)"until", -1);
18455 if (di != NULL)
18456 set_csearch_until(!!get_tv_number(&di->di_tv));
18457 }
18458}
18459
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460/*
18461 * "setcmdpos()" function
18462 */
18463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018464f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018466 int pos = (int)get_tv_number(&argvars[0]) - 1;
18467
18468 if (pos >= 0)
18469 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470}
18471
18472/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018473 * "setfperm({fname}, {mode})" function
18474 */
18475 static void
18476f_setfperm(typval_T *argvars, typval_T *rettv)
18477{
18478 char_u *fname;
18479 char_u modebuf[NUMBUFLEN];
18480 char_u *mode_str;
18481 int i;
18482 int mask;
18483 int mode = 0;
18484
18485 rettv->vval.v_number = 0;
18486 fname = get_tv_string_chk(&argvars[0]);
18487 if (fname == NULL)
18488 return;
18489 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18490 if (mode_str == NULL)
18491 return;
18492 if (STRLEN(mode_str) != 9)
18493 {
18494 EMSG2(_(e_invarg2), mode_str);
18495 return;
18496 }
18497
18498 mask = 1;
18499 for (i = 8; i >= 0; --i)
18500 {
18501 if (mode_str[i] != '-')
18502 mode |= mask;
18503 mask = mask << 1;
18504 }
18505 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18506}
18507
18508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 * "setline()" function
18510 */
18511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018512f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513{
18514 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018515 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018516 list_T *l = NULL;
18517 listitem_T *li = NULL;
18518 long added = 0;
18519 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018521 lnum = get_tv_lnum(&argvars[0]);
18522 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018524 l = argvars[1].vval.v_list;
18525 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018527 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018528 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018529
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018530 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018531 for (;;)
18532 {
18533 if (l != NULL)
18534 {
18535 /* list argument, get next string */
18536 if (li == NULL)
18537 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018538 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018539 li = li->li_next;
18540 }
18541
18542 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018543 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018544 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018545
18546 /* When coming here from Insert mode, sync undo, so that this can be
18547 * undone separately from what was previously inserted. */
18548 if (u_sync_once == 2)
18549 {
18550 u_sync_once = 1; /* notify that u_sync() was called */
18551 u_sync(TRUE);
18552 }
18553
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018554 if (lnum <= curbuf->b_ml.ml_line_count)
18555 {
18556 /* existing line, replace it */
18557 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18558 {
18559 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018560 if (lnum == curwin->w_cursor.lnum)
18561 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018562 rettv->vval.v_number = 0; /* OK */
18563 }
18564 }
18565 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18566 {
18567 /* lnum is one past the last line, append the line */
18568 ++added;
18569 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18570 rettv->vval.v_number = 0; /* OK */
18571 }
18572
18573 if (l == NULL) /* only one string argument */
18574 break;
18575 ++lnum;
18576 }
18577
18578 if (added > 0)
18579 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580}
18581
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018582static 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 +000018583
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018585 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018586 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018588set_qf_ll_list(
18589 win_T *wp UNUSED,
18590 typval_T *list_arg UNUSED,
18591 typval_T *action_arg UNUSED,
18592 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018593{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018594#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018595 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018596 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018597 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018598#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018599
Bram Moolenaar2641f772005-03-25 21:58:17 +000018600 rettv->vval.v_number = -1;
18601
18602#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018603 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018604 EMSG(_(e_listreq));
18605 else
18606 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018607 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018608
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018609 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018610 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018611 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018612 if (act == NULL)
18613 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018614 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018615 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018616 else
18617 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018618 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018619 else if (action_arg->v_type == VAR_UNKNOWN)
18620 action = ' ';
18621 else
18622 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018623
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018624 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018625 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018626 rettv->vval.v_number = 0;
18627 }
18628#endif
18629}
18630
18631/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018632 * "setloclist()" function
18633 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018635f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018636{
18637 win_T *win;
18638
18639 rettv->vval.v_number = -1;
18640
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018641 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018642 if (win != NULL)
18643 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18644}
18645
18646/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018647 * "setmatches()" function
18648 */
18649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018650f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018651{
18652#ifdef FEAT_SEARCH_EXTRA
18653 list_T *l;
18654 listitem_T *li;
18655 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018656 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018657
18658 rettv->vval.v_number = -1;
18659 if (argvars[0].v_type != VAR_LIST)
18660 {
18661 EMSG(_(e_listreq));
18662 return;
18663 }
18664 if ((l = argvars[0].vval.v_list) != NULL)
18665 {
18666
18667 /* To some extent make sure that we are dealing with a list from
18668 * "getmatches()". */
18669 li = l->lv_first;
18670 while (li != NULL)
18671 {
18672 if (li->li_tv.v_type != VAR_DICT
18673 || (d = li->li_tv.vval.v_dict) == NULL)
18674 {
18675 EMSG(_(e_invarg));
18676 return;
18677 }
18678 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018679 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18680 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018681 && dict_find(d, (char_u *)"priority", -1) != NULL
18682 && dict_find(d, (char_u *)"id", -1) != NULL))
18683 {
18684 EMSG(_(e_invarg));
18685 return;
18686 }
18687 li = li->li_next;
18688 }
18689
18690 clear_matches(curwin);
18691 li = l->lv_first;
18692 while (li != NULL)
18693 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018694 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018695 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018696 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018697 char_u *group;
18698 int priority;
18699 int id;
18700 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018701
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018702 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018703 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18704 {
18705 if (s == NULL)
18706 {
18707 s = list_alloc();
18708 if (s == NULL)
18709 return;
18710 }
18711
18712 /* match from matchaddpos() */
18713 for (i = 1; i < 9; i++)
18714 {
18715 sprintf((char *)buf, (char *)"pos%d", i);
18716 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18717 {
18718 if (di->di_tv.v_type != VAR_LIST)
18719 return;
18720
18721 list_append_tv(s, &di->di_tv);
18722 s->lv_refcount++;
18723 }
18724 else
18725 break;
18726 }
18727 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018728
18729 group = get_dict_string(d, (char_u *)"group", FALSE);
18730 priority = (int)get_dict_number(d, (char_u *)"priority");
18731 id = (int)get_dict_number(d, (char_u *)"id");
18732 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18733 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18734 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018735 if (i == 0)
18736 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018737 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018738 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018739 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018740 }
18741 else
18742 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018743 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018744 list_unref(s);
18745 s = NULL;
18746 }
18747
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018748 li = li->li_next;
18749 }
18750 rettv->vval.v_number = 0;
18751 }
18752#endif
18753}
18754
18755/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018756 * "setpos()" function
18757 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018759f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018760{
18761 pos_T pos;
18762 int fnum;
18763 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018764 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018765
Bram Moolenaar08250432008-02-13 11:42:46 +000018766 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018767 name = get_tv_string_chk(argvars);
18768 if (name != NULL)
18769 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018770 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018771 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018772 if (--pos.col < 0)
18773 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018774 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018775 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018776 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018777 if (fnum == curbuf->b_fnum)
18778 {
18779 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018780 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018781 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018782 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018783 curwin->w_set_curswant = FALSE;
18784 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018785 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018786 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018787 }
18788 else
18789 EMSG(_(e_invarg));
18790 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018791 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18792 {
18793 /* set mark */
18794 if (setmark_pos(name[1], &pos, fnum) == OK)
18795 rettv->vval.v_number = 0;
18796 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018797 else
18798 EMSG(_(e_invarg));
18799 }
18800 }
18801}
18802
18803/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018804 * "setqflist()" function
18805 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018807f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018808{
18809 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18810}
18811
18812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 * "setreg()" function
18814 */
18815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018816f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817{
18818 int regname;
18819 char_u *strregname;
18820 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018821 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 int append;
18823 char_u yank_type;
18824 long block_len;
18825
18826 block_len = -1;
18827 yank_type = MAUTO;
18828 append = FALSE;
18829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018830 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018831 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018833 if (strregname == NULL)
18834 return; /* type error; errmsg already given */
18835 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 if (regname == 0 || regname == '@')
18837 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018839 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018841 stropt = get_tv_string_chk(&argvars[2]);
18842 if (stropt == NULL)
18843 return; /* type error */
18844 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 switch (*stropt)
18846 {
18847 case 'a': case 'A': /* append */
18848 append = TRUE;
18849 break;
18850 case 'v': case 'c': /* character-wise selection */
18851 yank_type = MCHAR;
18852 break;
18853 case 'V': case 'l': /* line-wise selection */
18854 yank_type = MLINE;
18855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 case 'b': case Ctrl_V: /* block-wise selection */
18857 yank_type = MBLOCK;
18858 if (VIM_ISDIGIT(stropt[1]))
18859 {
18860 ++stropt;
18861 block_len = getdigits(&stropt) - 1;
18862 --stropt;
18863 }
18864 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 }
18866 }
18867
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018868 if (argvars[1].v_type == VAR_LIST)
18869 {
18870 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018871 char_u **allocval;
18872 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018873 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018874 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018875 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018876 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018877 int len;
18878
18879 /* If the list is NULL handle like an empty list. */
18880 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018881
Bram Moolenaar7d647822014-04-05 21:28:56 +020018882 /* First half: use for pointers to result lines; second half: use for
18883 * pointers to allocated copies. */
18884 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018885 if (lstval == NULL)
18886 return;
18887 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018888 allocval = lstval + len + 2;
18889 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018890
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018891 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018892 li = li->li_next)
18893 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018894 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018895 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018896 goto free_lstval;
18897 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018898 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018899 /* Need to make a copy, next get_tv_string_buf_chk() will
18900 * overwrite the string. */
18901 strval = vim_strsave(buf);
18902 if (strval == NULL)
18903 goto free_lstval;
18904 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018905 }
18906 *curval++ = strval;
18907 }
18908 *curval++ = NULL;
18909
18910 write_reg_contents_lst(regname, lstval, -1,
18911 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018912free_lstval:
18913 while (curallocval > allocval)
18914 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018915 vim_free(lstval);
18916 }
18917 else
18918 {
18919 strval = get_tv_string_chk(&argvars[1]);
18920 if (strval == NULL)
18921 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018922 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018925 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926}
18927
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018928/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018929 * "settabvar()" function
18930 */
18931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018932f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018933{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018934#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018935 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018936 tabpage_T *tp;
18937#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018938 char_u *varname, *tabvarname;
18939 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018940
18941 rettv->vval.v_number = 0;
18942
18943 if (check_restricted() || check_secure())
18944 return;
18945
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018946#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018947 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018948#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018949 varname = get_tv_string_chk(&argvars[1]);
18950 varp = &argvars[2];
18951
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018952 if (varname != NULL && varp != NULL
18953#ifdef FEAT_WINDOWS
18954 && tp != NULL
18955#endif
18956 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018957 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018958#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018959 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018960 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018961#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018962
18963 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18964 if (tabvarname != NULL)
18965 {
18966 STRCPY(tabvarname, "t:");
18967 STRCPY(tabvarname + 2, varname);
18968 set_var(tabvarname, varp, TRUE);
18969 vim_free(tabvarname);
18970 }
18971
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018972#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018973 /* Restore current tabpage */
18974 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018975 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018976#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018977 }
18978}
18979
18980/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018981 * "settabwinvar()" function
18982 */
18983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018984f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018985{
18986 setwinvar(argvars, rettv, 1);
18987}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988
18989/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018990 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018993f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018995 setwinvar(argvars, rettv, 0);
18996}
18997
18998/*
18999 * "setwinvar()" and "settabwinvar()" functions
19000 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020019001
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019003setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019004{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 win_T *win;
19006#ifdef FEAT_WINDOWS
19007 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019008 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020019009 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010#endif
19011 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019012 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019014 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015
19016 if (check_restricted() || check_secure())
19017 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019018
19019#ifdef FEAT_WINDOWS
19020 if (off == 1)
19021 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
19022 else
19023 tp = curtab;
19024#endif
19025 win = find_win_by_nr(&argvars[off], tp);
19026 varname = get_tv_string_chk(&argvars[off + 1]);
19027 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028
19029 if (win != NULL && varname != NULL && varp != NULL)
19030 {
19031#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019032 need_switch_win = !(tp == curtab && win == curwin);
19033 if (!need_switch_win
19034 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019037 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019039 long numval;
19040 char_u *strval;
19041 int error = FALSE;
19042
19043 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019044 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019045 strval = get_tv_string_buf_chk(varp, nbuf);
19046 if (!error && strval != NULL)
19047 set_option_value(varname, numval, strval, OPT_LOCAL);
19048 }
19049 else
19050 {
19051 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19052 if (winvarname != NULL)
19053 {
19054 STRCPY(winvarname, "w:");
19055 STRCPY(winvarname + 2, varname);
19056 set_var(winvarname, varp, TRUE);
19057 vim_free(winvarname);
19058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 }
19060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019062 if (need_switch_win)
19063 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064#endif
19065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066}
19067
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019068#ifdef FEAT_CRYPT
19069/*
19070 * "sha256({string})" function
19071 */
19072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019073f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019074{
19075 char_u *p;
19076
19077 p = get_tv_string(&argvars[0]);
19078 rettv->vval.v_string = vim_strsave(
19079 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19080 rettv->v_type = VAR_STRING;
19081}
19082#endif /* FEAT_CRYPT */
19083
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019085 * "shellescape({string})" function
19086 */
19087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019088f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019089{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019090 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019091 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019092 rettv->v_type = VAR_STRING;
19093}
19094
19095/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019096 * shiftwidth() function
19097 */
19098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019099f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019100{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019101 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019102}
19103
19104/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019105 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106 */
19107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019108f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019110 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111
Bram Moolenaar0d660222005-01-07 21:51:51 +000019112 p = get_tv_string(&argvars[0]);
19113 rettv->vval.v_string = vim_strsave(p);
19114 simplify_filename(rettv->vval.v_string); /* simplify in place */
19115 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116}
19117
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019118#ifdef FEAT_FLOAT
19119/*
19120 * "sin()" function
19121 */
19122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019123f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019124{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019125 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019126
19127 rettv->v_type = VAR_FLOAT;
19128 if (get_float_arg(argvars, &f) == OK)
19129 rettv->vval.v_float = sin(f);
19130 else
19131 rettv->vval.v_float = 0.0;
19132}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019133
19134/*
19135 * "sinh()" function
19136 */
19137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019138f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019139{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019140 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019141
19142 rettv->v_type = VAR_FLOAT;
19143 if (get_float_arg(argvars, &f) == OK)
19144 rettv->vval.v_float = sinh(f);
19145 else
19146 rettv->vval.v_float = 0.0;
19147}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019148#endif
19149
Bram Moolenaar0d660222005-01-07 21:51:51 +000019150static int
19151#ifdef __BORLANDC__
19152 _RTLENTRYF
19153#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019154 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019155static int
19156#ifdef __BORLANDC__
19157 _RTLENTRYF
19158#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019159 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019160
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019161/* struct used in the array that's given to qsort() */
19162typedef struct
19163{
19164 listitem_T *item;
19165 int idx;
19166} sortItem_T;
19167
Bram Moolenaar0b962472016-02-22 22:51:33 +010019168/* struct storing information about current sort */
19169typedef struct
19170{
19171 int item_compare_ic;
19172 int item_compare_numeric;
19173 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019174#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019175 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019176#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019177 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019178 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019179 dict_T *item_compare_selfdict;
19180 int item_compare_func_err;
19181 int item_compare_keep_zero;
19182} sortinfo_T;
19183static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019184static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019185#define ITEM_COMPARE_FAIL 999
19186
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019188 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019190 static int
19191#ifdef __BORLANDC__
19192_RTLENTRYF
19193#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019194item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019196 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019197 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019198 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019199 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019200 int res;
19201 char_u numbuf1[NUMBUFLEN];
19202 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019204 si1 = (sortItem_T *)s1;
19205 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019206 tv1 = &si1->item->li_tv;
19207 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019208
Bram Moolenaar0b962472016-02-22 22:51:33 +010019209 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019210 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019211 varnumber_T v1 = get_tv_number(tv1);
19212 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019213
19214 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19215 }
19216
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019217#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019218 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019219 {
19220 float_T v1 = get_tv_float(tv1);
19221 float_T v2 = get_tv_float(tv2);
19222
19223 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19224 }
19225#endif
19226
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019227 /* tv2string() puts quotes around a string and allocates memory. Don't do
19228 * that for string variables. Use a single quote when comparing with a
19229 * non-string to do what the docs promise. */
19230 if (tv1->v_type == VAR_STRING)
19231 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019232 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019233 p1 = (char_u *)"'";
19234 else
19235 p1 = tv1->vval.v_string;
19236 }
19237 else
19238 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19239 if (tv2->v_type == VAR_STRING)
19240 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019241 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019242 p2 = (char_u *)"'";
19243 else
19244 p2 = tv2->vval.v_string;
19245 }
19246 else
19247 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019248 if (p1 == NULL)
19249 p1 = (char_u *)"";
19250 if (p2 == NULL)
19251 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019252 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019253 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019254 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019255 res = STRICMP(p1, p2);
19256 else
19257 res = STRCMP(p1, p2);
19258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019260 {
19261 double n1, n2;
19262 n1 = strtod((char *)p1, (char **)&p1);
19263 n2 = strtod((char *)p2, (char **)&p2);
19264 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19265 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019266
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019267 /* When the result would be zero, compare the item indexes. Makes the
19268 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019269 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019270 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019271
Bram Moolenaar0d660222005-01-07 21:51:51 +000019272 vim_free(tofree1);
19273 vim_free(tofree2);
19274 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275}
19276
19277 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019278#ifdef __BORLANDC__
19279_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019281item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019282{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019283 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019284 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019285 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019286 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019287 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019288 char_u *func_name;
19289 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019291 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019292 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019293 return 0;
19294
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019295 si1 = (sortItem_T *)s1;
19296 si2 = (sortItem_T *)s2;
19297
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019298 if (partial == NULL)
19299 func_name = sortinfo->item_compare_func;
19300 else
19301 func_name = partial->pt_name;
19302
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019303 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019304 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019305 copy_tv(&si1->item->li_tv, &argv[0]);
19306 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019307
19308 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019309 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019310 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019311 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019312 clear_tv(&argv[0]);
19313 clear_tv(&argv[1]);
19314
19315 if (res == FAIL)
19316 res = ITEM_COMPARE_FAIL;
19317 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019318 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019319 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019320 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019321 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019322
19323 /* When the result would be zero, compare the pointers themselves. Makes
19324 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019325 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019326 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019327
Bram Moolenaar0d660222005-01-07 21:51:51 +000019328 return res;
19329}
19330
19331/*
19332 * "sort({list})" function
19333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019335do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336{
Bram Moolenaar33570922005-01-25 22:26:29 +000019337 list_T *l;
19338 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019339 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019340 sortinfo_T *old_sortinfo;
19341 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019342 long len;
19343 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344
Bram Moolenaar0b962472016-02-22 22:51:33 +010019345 /* Pointer to current info struct used in compare function. Save and
19346 * restore the current one for nested calls. */
19347 old_sortinfo = sortinfo;
19348 sortinfo = &info;
19349
Bram Moolenaar0d660222005-01-07 21:51:51 +000019350 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019351 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 else
19353 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019354 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019355 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019356 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19357 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019358 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019359 rettv->vval.v_list = l;
19360 rettv->v_type = VAR_LIST;
19361 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362
Bram Moolenaar0d660222005-01-07 21:51:51 +000019363 len = list_len(l);
19364 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019365 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366
Bram Moolenaar0b962472016-02-22 22:51:33 +010019367 info.item_compare_ic = FALSE;
19368 info.item_compare_numeric = FALSE;
19369 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019370#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019371 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019372#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019373 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019374 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019375 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019376 if (argvars[1].v_type != VAR_UNKNOWN)
19377 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019378 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019379 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019380 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019381 else if (argvars[1].v_type == VAR_PARTIAL)
19382 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019383 else
19384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019385 int error = FALSE;
19386
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019387 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019388 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019389 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019390 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019391 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019392 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019393 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019394 else if (i != 0)
19395 {
19396 EMSG(_(e_invarg));
19397 goto theend;
19398 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019399 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019400 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019401 if (*info.item_compare_func == NUL)
19402 {
19403 /* empty string means default sort */
19404 info.item_compare_func = NULL;
19405 }
19406 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019407 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019408 info.item_compare_func = NULL;
19409 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019410 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019411 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019412 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019413 info.item_compare_func = NULL;
19414 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019415 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019416#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019417 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019418 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019419 info.item_compare_func = NULL;
19420 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019421 }
19422#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019423 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019424 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019425 info.item_compare_func = NULL;
19426 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019427 }
19428 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019429 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019430
19431 if (argvars[2].v_type != VAR_UNKNOWN)
19432 {
19433 /* optional third argument: {dict} */
19434 if (argvars[2].v_type != VAR_DICT)
19435 {
19436 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019437 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019438 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019439 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019440 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442
Bram Moolenaar0d660222005-01-07 21:51:51 +000019443 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019444 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019445 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019446 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447
Bram Moolenaar327aa022014-03-25 18:24:23 +010019448 i = 0;
19449 if (sort)
19450 {
19451 /* sort(): ptrs will be the list to sort */
19452 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019453 {
19454 ptrs[i].item = li;
19455 ptrs[i].idx = i;
19456 ++i;
19457 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019458
Bram Moolenaar0b962472016-02-22 22:51:33 +010019459 info.item_compare_func_err = FALSE;
19460 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019461 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019462 if ((info.item_compare_func != NULL
19463 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019464 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019465 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019466 EMSG(_("E702: Sort compare function failed"));
19467 else
19468 {
19469 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019470 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019471 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019472 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019473 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019474
Bram Moolenaar0b962472016-02-22 22:51:33 +010019475 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019476 {
19477 /* Clear the List and append the items in sorted order. */
19478 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19479 l->lv_len = 0;
19480 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019481 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019482 }
19483 }
19484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019486 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019487 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019488
19489 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019490 info.item_compare_func_err = FALSE;
19491 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019492 item_compare_func_ptr = info.item_compare_func != NULL
19493 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019494 ? item_compare2 : item_compare;
19495
19496 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19497 li = li->li_next)
19498 {
19499 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19500 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019501 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019502 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019503 {
19504 EMSG(_("E882: Uniq compare function failed"));
19505 break;
19506 }
19507 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019508
Bram Moolenaar0b962472016-02-22 22:51:33 +010019509 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019510 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019511 while (--i >= 0)
19512 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019513 li = ptrs[i].item->li_next;
19514 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019515 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019516 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019517 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019518 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019519 list_fix_watch(l, li);
19520 listitem_free(li);
19521 l->lv_len--;
19522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019523 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019524 }
19525
19526 vim_free(ptrs);
19527 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019528theend:
19529 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019530}
19531
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019532/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019533 * "sort({list})" function
19534 */
19535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019536f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019537{
19538 do_sort_uniq(argvars, rettv, TRUE);
19539}
19540
19541/*
19542 * "uniq({list})" function
19543 */
19544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019545f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019546{
19547 do_sort_uniq(argvars, rettv, FALSE);
19548}
19549
19550/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019551 * "soundfold({word})" function
19552 */
19553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019554f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019555{
19556 char_u *s;
19557
19558 rettv->v_type = VAR_STRING;
19559 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019560#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019561 rettv->vval.v_string = eval_soundfold(s);
19562#else
19563 rettv->vval.v_string = vim_strsave(s);
19564#endif
19565}
19566
19567/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019568 * "spellbadword()" function
19569 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019571f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019572{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019573 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019574 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019575 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019576
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019577 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019578 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019579
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019580#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019581 if (argvars[0].v_type == VAR_UNKNOWN)
19582 {
19583 /* Find the start and length of the badly spelled word. */
19584 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19585 if (len != 0)
19586 word = ml_get_cursor();
19587 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019588 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019589 {
19590 char_u *str = get_tv_string_chk(&argvars[0]);
19591 int capcol = -1;
19592
19593 if (str != NULL)
19594 {
19595 /* Check the argument for spelling. */
19596 while (*str != NUL)
19597 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019598 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019599 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019600 {
19601 word = str;
19602 break;
19603 }
19604 str += len;
19605 }
19606 }
19607 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019608#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019609
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019610 list_append_string(rettv->vval.v_list, word, len);
19611 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019612 attr == HLF_SPB ? "bad" :
19613 attr == HLF_SPR ? "rare" :
19614 attr == HLF_SPL ? "local" :
19615 attr == HLF_SPC ? "caps" :
19616 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019617}
19618
19619/*
19620 * "spellsuggest()" function
19621 */
19622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019623f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019624{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019625#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019626 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019627 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019628 int maxcount;
19629 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019630 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019631 listitem_T *li;
19632 int need_capital = FALSE;
19633#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019634
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019635 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019636 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019637
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019638#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019639 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019640 {
19641 str = get_tv_string(&argvars[0]);
19642 if (argvars[1].v_type != VAR_UNKNOWN)
19643 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019644 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019645 if (maxcount <= 0)
19646 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019647 if (argvars[2].v_type != VAR_UNKNOWN)
19648 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019649 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019650 if (typeerr)
19651 return;
19652 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019653 }
19654 else
19655 maxcount = 25;
19656
Bram Moolenaar4770d092006-01-12 23:22:24 +000019657 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019658
19659 for (i = 0; i < ga.ga_len; ++i)
19660 {
19661 str = ((char_u **)ga.ga_data)[i];
19662
19663 li = listitem_alloc();
19664 if (li == NULL)
19665 vim_free(str);
19666 else
19667 {
19668 li->li_tv.v_type = VAR_STRING;
19669 li->li_tv.v_lock = 0;
19670 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019671 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019672 }
19673 }
19674 ga_clear(&ga);
19675 }
19676#endif
19677}
19678
Bram Moolenaar0d660222005-01-07 21:51:51 +000019679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019680f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019681{
19682 char_u *str;
19683 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019684 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019685 regmatch_T regmatch;
19686 char_u patbuf[NUMBUFLEN];
19687 char_u *save_cpo;
19688 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019689 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019690 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019691 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019692
19693 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19694 save_cpo = p_cpo;
19695 p_cpo = (char_u *)"";
19696
19697 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019698 if (argvars[1].v_type != VAR_UNKNOWN)
19699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019700 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19701 if (pat == NULL)
19702 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019703 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019704 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019705 }
19706 if (pat == NULL || *pat == NUL)
19707 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019708
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019709 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019711 if (typeerr)
19712 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713
Bram Moolenaar0d660222005-01-07 21:51:51 +000019714 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19715 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019717 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019718 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019719 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019720 if (*str == NUL)
19721 match = FALSE; /* empty item at the end */
19722 else
19723 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019724 if (match)
19725 end = regmatch.startp[0];
19726 else
19727 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019728 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19729 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019730 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019731 if (list_append_string(rettv->vval.v_list, str,
19732 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019733 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019734 }
19735 if (!match)
19736 break;
19737 /* Advance to just after the match. */
19738 if (regmatch.endp[0] > str)
19739 col = 0;
19740 else
19741 {
19742 /* Don't get stuck at the same match. */
19743#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019744 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019745#else
19746 col = 1;
19747#endif
19748 }
19749 str = regmatch.endp[0];
19750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751
Bram Moolenaar473de612013-06-08 18:19:48 +020019752 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754
Bram Moolenaar0d660222005-01-07 21:51:51 +000019755 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756}
19757
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019758#ifdef FEAT_FLOAT
19759/*
19760 * "sqrt()" function
19761 */
19762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019763f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019764{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019765 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019766
19767 rettv->v_type = VAR_FLOAT;
19768 if (get_float_arg(argvars, &f) == OK)
19769 rettv->vval.v_float = sqrt(f);
19770 else
19771 rettv->vval.v_float = 0.0;
19772}
19773
19774/*
19775 * "str2float()" function
19776 */
19777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019778f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019779{
19780 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19781
19782 if (*p == '+')
19783 p = skipwhite(p + 1);
19784 (void)string2float(p, &rettv->vval.v_float);
19785 rettv->v_type = VAR_FLOAT;
19786}
19787#endif
19788
Bram Moolenaar2c932302006-03-18 21:42:09 +000019789/*
19790 * "str2nr()" function
19791 */
19792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019793f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019794{
19795 int base = 10;
19796 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019797 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019798 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019799
19800 if (argvars[1].v_type != VAR_UNKNOWN)
19801 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019802 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019803 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019804 {
19805 EMSG(_(e_invarg));
19806 return;
19807 }
19808 }
19809
19810 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019811 if (*p == '+')
19812 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019813 switch (base)
19814 {
19815 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19816 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19817 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19818 default: what = 0;
19819 }
19820 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019821 rettv->vval.v_number = n;
19822}
19823
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824#ifdef HAVE_STRFTIME
19825/*
19826 * "strftime({format}[, {time}])" function
19827 */
19828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019829f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830{
19831 char_u result_buf[256];
19832 struct tm *curtime;
19833 time_t seconds;
19834 char_u *p;
19835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019839 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 seconds = time(NULL);
19841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 curtime = localtime(&seconds);
19844 /* MSVC returns NULL for an invalid value of seconds. */
19845 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019846 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 else
19848 {
19849# ifdef FEAT_MBYTE
19850 vimconv_T conv;
19851 char_u *enc;
19852
19853 conv.vc_type = CONV_NONE;
19854 enc = enc_locale();
19855 convert_setup(&conv, p_enc, enc);
19856 if (conv.vc_type != CONV_NONE)
19857 p = string_convert(&conv, p, NULL);
19858# endif
19859 if (p != NULL)
19860 (void)strftime((char *)result_buf, sizeof(result_buf),
19861 (char *)p, curtime);
19862 else
19863 result_buf[0] = NUL;
19864
19865# ifdef FEAT_MBYTE
19866 if (conv.vc_type != CONV_NONE)
19867 vim_free(p);
19868 convert_setup(&conv, enc, p_enc);
19869 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019870 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 else
19872# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019873 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874
19875# ifdef FEAT_MBYTE
19876 /* Release conversion descriptors */
19877 convert_setup(&conv, NULL, NULL);
19878 vim_free(enc);
19879# endif
19880 }
19881}
19882#endif
19883
19884/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019885 * "strgetchar()" function
19886 */
19887 static void
19888f_strgetchar(typval_T *argvars, typval_T *rettv)
19889{
19890 char_u *str;
19891 int len;
19892 int error = FALSE;
19893 int charidx;
19894
19895 rettv->vval.v_number = -1;
19896 str = get_tv_string_chk(&argvars[0]);
19897 if (str == NULL)
19898 return;
19899 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019900 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019901 if (error)
19902 return;
19903#ifdef FEAT_MBYTE
19904 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019905 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019906
19907 while (charidx >= 0 && byteidx < len)
19908 {
19909 if (charidx == 0)
19910 {
19911 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19912 break;
19913 }
19914 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019915 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019916 }
19917 }
19918#else
19919 if (charidx < len)
19920 rettv->vval.v_number = str[charidx];
19921#endif
19922}
19923
19924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 * "stridx()" function
19926 */
19927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019928f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929{
19930 char_u buf[NUMBUFLEN];
19931 char_u *needle;
19932 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019933 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019935 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019937 needle = get_tv_string_chk(&argvars[1]);
19938 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019939 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019940 if (needle == NULL || haystack == NULL)
19941 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942
Bram Moolenaar33570922005-01-25 22:26:29 +000019943 if (argvars[2].v_type != VAR_UNKNOWN)
19944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019945 int error = FALSE;
19946
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019947 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019948 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019949 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019950 if (start_idx >= 0)
19951 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019952 }
19953
19954 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19955 if (pos != NULL)
19956 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957}
19958
19959/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019960 * "string()" function
19961 */
19962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019963f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019964{
19965 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019966 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019968 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019969 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19970 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019971 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019972 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019973 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974}
19975
19976/*
19977 * "strlen()" function
19978 */
19979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019980f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982 rettv->vval.v_number = (varnumber_T)(STRLEN(
19983 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984}
19985
19986/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019987 * "strchars()" function
19988 */
19989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019990f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019991{
19992 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019993 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019994#ifdef FEAT_MBYTE
19995 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019996 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019997#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019998
19999 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020000 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020001 if (skipcc < 0 || skipcc > 1)
20002 EMSG(_(e_invarg));
20003 else
20004 {
20005#ifdef FEAT_MBYTE
20006 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
20007 while (*s != NUL)
20008 {
20009 func_mb_ptr2char_adv(&s);
20010 ++len;
20011 }
20012 rettv->vval.v_number = len;
20013#else
20014 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
20015#endif
20016 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020020017}
20018
20019/*
Bram Moolenaardc536092010-07-18 15:45:49 +020020020 * "strdisplaywidth()" function
20021 */
20022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020023f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020020024{
20025 char_u *s = get_tv_string(&argvars[0]);
20026 int col = 0;
20027
20028 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020029 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020020030
Bram Moolenaar8a09b982010-07-22 22:20:57 +020020031 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020020032}
20033
20034/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020035 * "strwidth()" function
20036 */
20037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020038f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020039{
20040 char_u *s = get_tv_string(&argvars[0]);
20041
20042 rettv->vval.v_number = (varnumber_T)(
20043#ifdef FEAT_MBYTE
20044 mb_string2cells(s, -1)
20045#else
20046 STRLEN(s)
20047#endif
20048 );
20049}
20050
20051/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020052 * "strcharpart()" function
20053 */
20054 static void
20055f_strcharpart(typval_T *argvars, typval_T *rettv)
20056{
20057#ifdef FEAT_MBYTE
20058 char_u *p;
20059 int nchar;
20060 int nbyte = 0;
20061 int charlen;
20062 int len = 0;
20063 int slen;
20064 int error = FALSE;
20065
20066 p = get_tv_string(&argvars[0]);
20067 slen = (int)STRLEN(p);
20068
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020069 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020070 if (!error)
20071 {
20072 if (nchar > 0)
20073 while (nchar > 0 && nbyte < slen)
20074 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020075 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020076 --nchar;
20077 }
20078 else
20079 nbyte = nchar;
20080 if (argvars[2].v_type != VAR_UNKNOWN)
20081 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020082 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020083 while (charlen > 0 && nbyte + len < slen)
20084 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020085 int off = nbyte + len;
20086
20087 if (off < 0)
20088 len += 1;
20089 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020090 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020091 --charlen;
20092 }
20093 }
20094 else
20095 len = slen - nbyte; /* default: all bytes that are available. */
20096 }
20097
20098 /*
20099 * Only return the overlap between the specified part and the actual
20100 * string.
20101 */
20102 if (nbyte < 0)
20103 {
20104 len += nbyte;
20105 nbyte = 0;
20106 }
20107 else if (nbyte > slen)
20108 nbyte = slen;
20109 if (len < 0)
20110 len = 0;
20111 else if (nbyte + len > slen)
20112 len = slen - nbyte;
20113
20114 rettv->v_type = VAR_STRING;
20115 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20116#else
20117 f_strpart(argvars, rettv);
20118#endif
20119}
20120
20121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 * "strpart()" function
20123 */
20124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020125f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126{
20127 char_u *p;
20128 int n;
20129 int len;
20130 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020131 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020133 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 slen = (int)STRLEN(p);
20135
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020136 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020137 if (error)
20138 len = 0;
20139 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020140 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 else
20142 len = slen - n; /* default len: all bytes that are available. */
20143
20144 /*
20145 * Only return the overlap between the specified part and the actual
20146 * string.
20147 */
20148 if (n < 0)
20149 {
20150 len += n;
20151 n = 0;
20152 }
20153 else if (n > slen)
20154 n = slen;
20155 if (len < 0)
20156 len = 0;
20157 else if (n + len > slen)
20158 len = slen - n;
20159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020160 rettv->v_type = VAR_STRING;
20161 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162}
20163
20164/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020165 * "strridx()" function
20166 */
20167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020168f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020169{
20170 char_u buf[NUMBUFLEN];
20171 char_u *needle;
20172 char_u *haystack;
20173 char_u *rest;
20174 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020175 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020177 needle = get_tv_string_chk(&argvars[1]);
20178 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020179
20180 rettv->vval.v_number = -1;
20181 if (needle == NULL || haystack == NULL)
20182 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020183
20184 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020185 if (argvars[2].v_type != VAR_UNKNOWN)
20186 {
20187 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020188 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020189 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020190 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020191 }
20192 else
20193 end_idx = haystack_len;
20194
Bram Moolenaar0d660222005-01-07 21:51:51 +000020195 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020196 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020197 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020198 lastmatch = haystack + end_idx;
20199 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020200 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020201 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020202 for (rest = haystack; *rest != '\0'; ++rest)
20203 {
20204 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020205 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020206 break;
20207 lastmatch = rest;
20208 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020209 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020210
20211 if (lastmatch == NULL)
20212 rettv->vval.v_number = -1;
20213 else
20214 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20215}
20216
20217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 * "strtrans()" function
20219 */
20220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020221f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020223 rettv->v_type = VAR_STRING;
20224 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225}
20226
20227/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020228 * "submatch()" function
20229 */
20230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020231f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020232{
Bram Moolenaar41571762014-04-02 19:00:58 +020020233 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020234 int no;
20235 int retList = 0;
20236
20237 no = (int)get_tv_number_chk(&argvars[0], &error);
20238 if (error)
20239 return;
20240 error = FALSE;
20241 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020242 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020243 if (error)
20244 return;
20245
20246 if (retList == 0)
20247 {
20248 rettv->v_type = VAR_STRING;
20249 rettv->vval.v_string = reg_submatch(no);
20250 }
20251 else
20252 {
20253 rettv->v_type = VAR_LIST;
20254 rettv->vval.v_list = reg_submatch_list(no);
20255 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020256}
20257
20258/*
20259 * "substitute()" function
20260 */
20261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020262f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020263{
20264 char_u patbuf[NUMBUFLEN];
20265 char_u subbuf[NUMBUFLEN];
20266 char_u flagsbuf[NUMBUFLEN];
20267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020268 char_u *str = get_tv_string_chk(&argvars[0]);
20269 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20270 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20271 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20272
Bram Moolenaar0d660222005-01-07 21:51:51 +000020273 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020274 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20275 rettv->vval.v_string = NULL;
20276 else
20277 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020278}
20279
20280/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020281 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020284f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285{
20286 int id = 0;
20287#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020288 linenr_T lnum;
20289 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020291 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020293 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020294 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20295 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020297 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020298 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020299 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300#endif
20301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020302 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303}
20304
20305/*
20306 * "synIDattr(id, what [, mode])" function
20307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020309f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310{
20311 char_u *p = NULL;
20312#ifdef FEAT_SYN_HL
20313 int id;
20314 char_u *what;
20315 char_u *mode;
20316 char_u modebuf[NUMBUFLEN];
20317 int modec;
20318
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020319 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020320 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020321 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020323 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020325 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 modec = 0; /* replace invalid with current */
20327 }
20328 else
20329 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020330#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020331 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 modec = 'g';
20333 else
20334#endif
20335 if (t_colors > 1)
20336 modec = 'c';
20337 else
20338 modec = 't';
20339 }
20340
20341
20342 switch (TOLOWER_ASC(what[0]))
20343 {
20344 case 'b':
20345 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20346 p = highlight_color(id, what, modec);
20347 else /* bold */
20348 p = highlight_has_attr(id, HL_BOLD, modec);
20349 break;
20350
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020351 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 p = highlight_color(id, what, modec);
20353 break;
20354
20355 case 'i':
20356 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20357 p = highlight_has_attr(id, HL_INVERSE, modec);
20358 else /* italic */
20359 p = highlight_has_attr(id, HL_ITALIC, modec);
20360 break;
20361
20362 case 'n': /* name */
20363 p = get_highlight_name(NULL, id - 1);
20364 break;
20365
20366 case 'r': /* reverse */
20367 p = highlight_has_attr(id, HL_INVERSE, modec);
20368 break;
20369
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020370 case 's':
20371 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20372 p = highlight_color(id, what, modec);
20373 else /* standout */
20374 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 break;
20376
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020377 case 'u':
20378 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20379 /* underline */
20380 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20381 else
20382 /* undercurl */
20383 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 break;
20385 }
20386
20387 if (p != NULL)
20388 p = vim_strsave(p);
20389#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020390 rettv->v_type = VAR_STRING;
20391 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392}
20393
20394/*
20395 * "synIDtrans(id)" function
20396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020398f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399{
20400 int id;
20401
20402#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020403 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404
20405 if (id > 0)
20406 id = syn_get_final_id(id);
20407 else
20408#endif
20409 id = 0;
20410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020411 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412}
20413
20414/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020415 * "synconcealed(lnum, col)" function
20416 */
20417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020418f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020419{
20420#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020421 linenr_T lnum;
20422 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020423 int syntax_flags = 0;
20424 int cchar;
20425 int matchid = 0;
20426 char_u str[NUMBUFLEN];
20427#endif
20428
20429 rettv->v_type = VAR_LIST;
20430 rettv->vval.v_list = NULL;
20431
20432#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20433 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020434 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020435
20436 vim_memset(str, NUL, sizeof(str));
20437
20438 if (rettv_list_alloc(rettv) != FAIL)
20439 {
20440 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20441 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20442 && curwin->w_p_cole > 0)
20443 {
20444 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20445 syntax_flags = get_syntax_info(&matchid);
20446
20447 /* get the conceal character */
20448 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20449 {
20450 cchar = syn_get_sub_char();
20451 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20452 cchar = lcs_conceal;
20453 if (cchar != NUL)
20454 {
20455# ifdef FEAT_MBYTE
20456 if (has_mbyte)
20457 (*mb_char2bytes)(cchar, str);
20458 else
20459# endif
20460 str[0] = cchar;
20461 }
20462 }
20463 }
20464
20465 list_append_number(rettv->vval.v_list,
20466 (syntax_flags & HL_CONCEAL) != 0);
20467 /* -1 to auto-determine strlen */
20468 list_append_string(rettv->vval.v_list, str, -1);
20469 list_append_number(rettv->vval.v_list, matchid);
20470 }
20471#endif
20472}
20473
20474/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020475 * "synstack(lnum, col)" function
20476 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020478f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020479{
20480#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020481 linenr_T lnum;
20482 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020483 int i;
20484 int id;
20485#endif
20486
20487 rettv->v_type = VAR_LIST;
20488 rettv->vval.v_list = NULL;
20489
20490#ifdef FEAT_SYN_HL
20491 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020492 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020493
20494 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020495 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020496 && rettv_list_alloc(rettv) != FAIL)
20497 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020498 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020499 for (i = 0; ; ++i)
20500 {
20501 id = syn_get_stack_item(i);
20502 if (id < 0)
20503 break;
20504 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20505 break;
20506 }
20507 }
20508#endif
20509}
20510
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020512get_cmd_output_as_rettv(
20513 typval_T *argvars,
20514 typval_T *rettv,
20515 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020517 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020519 char_u *infile = NULL;
20520 char_u buf[NUMBUFLEN];
20521 int err = FALSE;
20522 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020523 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020524 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020526 rettv->v_type = VAR_STRING;
20527 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020528 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020529 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020530
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020531 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020532 {
20533 /*
20534 * Write the string to a temp file, to be used for input of the shell
20535 * command.
20536 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020537 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020538 {
20539 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020540 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020541 }
20542
20543 fd = mch_fopen((char *)infile, WRITEBIN);
20544 if (fd == NULL)
20545 {
20546 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020547 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020548 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020549 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020550 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020551 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20552 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020553 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020554 else
20555 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020556 size_t len;
20557
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020558 p = get_tv_string_buf_chk(&argvars[1], buf);
20559 if (p == NULL)
20560 {
20561 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020562 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020563 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020564 len = STRLEN(p);
20565 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020566 err = TRUE;
20567 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020568 if (fclose(fd) != 0)
20569 err = TRUE;
20570 if (err)
20571 {
20572 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020573 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020574 }
20575 }
20576
Bram Moolenaar52a72462014-08-29 15:53:52 +020020577 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20578 * echoes typeahead, that messes up the display. */
20579 if (!msg_silent)
20580 flags += SHELL_COOKED;
20581
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020582 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020584 int len;
20585 listitem_T *li;
20586 char_u *s = NULL;
20587 char_u *start;
20588 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020589 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590
Bram Moolenaar52a72462014-08-29 15:53:52 +020020591 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020592 if (res == NULL)
20593 goto errret;
20594
20595 list = list_alloc();
20596 if (list == NULL)
20597 goto errret;
20598
20599 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020601 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020602 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020603 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020604 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020605
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020606 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020607 if (s == NULL)
20608 goto errret;
20609
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020610 for (p = s; start < end; ++p, ++start)
20611 *p = *start == NUL ? NL : *start;
20612 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020613
20614 li = listitem_alloc();
20615 if (li == NULL)
20616 {
20617 vim_free(s);
20618 goto errret;
20619 }
20620 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020621 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020622 li->li_tv.vval.v_string = s;
20623 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020625
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020626 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020627 rettv->v_type = VAR_LIST;
20628 rettv->vval.v_list = list;
20629 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020631 else
20632 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020633 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020634#ifdef USE_CR
20635 /* translate <CR> into <NL> */
20636 if (res != NULL)
20637 {
20638 char_u *s;
20639
20640 for (s = res; *s; ++s)
20641 {
20642 if (*s == CAR)
20643 *s = NL;
20644 }
20645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646#else
20647# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020648 /* translate <CR><NL> into <NL> */
20649 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020651 char_u *s, *d;
20652
20653 d = res;
20654 for (s = res; *s; ++s)
20655 {
20656 if (s[0] == CAR && s[1] == NL)
20657 ++s;
20658 *d++ = *s;
20659 }
20660 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662# endif
20663#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020664 rettv->vval.v_string = res;
20665 res = NULL;
20666 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020667
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020668errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020669 if (infile != NULL)
20670 {
20671 mch_remove(infile);
20672 vim_free(infile);
20673 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020674 if (res != NULL)
20675 vim_free(res);
20676 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020677 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020678}
20679
20680/*
20681 * "system()" function
20682 */
20683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020684f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020685{
20686 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20687}
20688
20689/*
20690 * "systemlist()" function
20691 */
20692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020693f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020694{
20695 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696}
20697
20698/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020699 * "tabpagebuflist()" function
20700 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020702f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020703{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020704#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020705 tabpage_T *tp;
20706 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020707
20708 if (argvars[0].v_type == VAR_UNKNOWN)
20709 wp = firstwin;
20710 else
20711 {
20712 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20713 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020714 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020715 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020716 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020717 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020718 for (; wp != NULL; wp = wp->w_next)
20719 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020720 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020721 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020722 }
20723#endif
20724}
20725
20726
20727/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020728 * "tabpagenr()" function
20729 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020731f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020732{
20733 int nr = 1;
20734#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020735 char_u *arg;
20736
20737 if (argvars[0].v_type != VAR_UNKNOWN)
20738 {
20739 arg = get_tv_string_chk(&argvars[0]);
20740 nr = 0;
20741 if (arg != NULL)
20742 {
20743 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020744 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020745 else
20746 EMSG2(_(e_invexpr2), arg);
20747 }
20748 }
20749 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020750 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020751#endif
20752 rettv->vval.v_number = nr;
20753}
20754
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020755
20756#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020757static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020758
20759/*
20760 * Common code for tabpagewinnr() and winnr().
20761 */
20762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020763get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020764{
20765 win_T *twin;
20766 int nr = 1;
20767 win_T *wp;
20768 char_u *arg;
20769
20770 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20771 if (argvar->v_type != VAR_UNKNOWN)
20772 {
20773 arg = get_tv_string_chk(argvar);
20774 if (arg == NULL)
20775 nr = 0; /* type error; errmsg already given */
20776 else if (STRCMP(arg, "$") == 0)
20777 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20778 else if (STRCMP(arg, "#") == 0)
20779 {
20780 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20781 if (twin == NULL)
20782 nr = 0;
20783 }
20784 else
20785 {
20786 EMSG2(_(e_invexpr2), arg);
20787 nr = 0;
20788 }
20789 }
20790
20791 if (nr > 0)
20792 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20793 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020794 {
20795 if (wp == NULL)
20796 {
20797 /* didn't find it in this tabpage */
20798 nr = 0;
20799 break;
20800 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020801 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020802 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020803 return nr;
20804}
20805#endif
20806
20807/*
20808 * "tabpagewinnr()" function
20809 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020811f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020812{
20813 int nr = 1;
20814#ifdef FEAT_WINDOWS
20815 tabpage_T *tp;
20816
20817 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20818 if (tp == NULL)
20819 nr = 0;
20820 else
20821 nr = get_winnr(tp, &argvars[1]);
20822#endif
20823 rettv->vval.v_number = nr;
20824}
20825
20826
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020827/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020828 * "tagfiles()" function
20829 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020831f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020832{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020833 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020834 tagname_T tn;
20835 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020836
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020837 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020838 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020839 fname = alloc(MAXPATHL);
20840 if (fname == NULL)
20841 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020842
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020843 for (first = TRUE; ; first = FALSE)
20844 if (get_tagfname(&tn, first, fname) == FAIL
20845 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020846 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020847 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020848 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020849}
20850
20851/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020852 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020853 */
20854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020855f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020856{
20857 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020858
20859 tag_pattern = get_tv_string(&argvars[0]);
20860
20861 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020862 if (*tag_pattern == NUL)
20863 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020864
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020865 if (rettv_list_alloc(rettv) == OK)
20866 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020867}
20868
20869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 * "tempname()" function
20871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020873f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874{
20875 static int x = 'A';
20876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020877 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020878 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879
20880 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20881 * names. Skip 'I' and 'O', they are used for shell redirection. */
20882 do
20883 {
20884 if (x == 'Z')
20885 x = '0';
20886 else if (x == '9')
20887 x = 'A';
20888 else
20889 {
20890#ifdef EBCDIC
20891 if (x == 'I')
20892 x = 'J';
20893 else if (x == 'R')
20894 x = 'S';
20895 else
20896#endif
20897 ++x;
20898 }
20899 } while (x == 'I' || x == 'O');
20900}
20901
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020902#ifdef FEAT_FLOAT
20903/*
20904 * "tan()" function
20905 */
20906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020907f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020908{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020909 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020910
20911 rettv->v_type = VAR_FLOAT;
20912 if (get_float_arg(argvars, &f) == OK)
20913 rettv->vval.v_float = tan(f);
20914 else
20915 rettv->vval.v_float = 0.0;
20916}
20917
20918/*
20919 * "tanh()" function
20920 */
20921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020922f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020923{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020924 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020925
20926 rettv->v_type = VAR_FLOAT;
20927 if (get_float_arg(argvars, &f) == OK)
20928 rettv->vval.v_float = tanh(f);
20929 else
20930 rettv->vval.v_float = 0.0;
20931}
20932#endif
20933
Bram Moolenaar574860b2016-05-24 17:33:34 +020020934/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020935 * "test_alloc_fail(id, countdown, repeat)" function
20936 */
20937 static void
20938f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20939{
20940 if (argvars[0].v_type != VAR_NUMBER
20941 || argvars[0].vval.v_number <= 0
20942 || argvars[1].v_type != VAR_NUMBER
20943 || argvars[1].vval.v_number < 0
20944 || argvars[2].v_type != VAR_NUMBER)
20945 EMSG(_(e_invarg));
20946 else
20947 {
20948 alloc_fail_id = argvars[0].vval.v_number;
20949 if (alloc_fail_id >= aid_last)
20950 EMSG(_(e_invarg));
20951 alloc_fail_countdown = argvars[1].vval.v_number;
20952 alloc_fail_repeat = argvars[2].vval.v_number;
20953 did_outofmem_msg = FALSE;
20954 }
20955}
20956
20957/*
20958 * "test_disable_char_avail({expr})" function
20959 */
20960 static void
20961f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20962{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020963 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020964}
20965
20966/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020967 * "test_garbagecollect_now()" function
20968 */
20969 static void
20970f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20971{
20972 /* This is dangerous, any Lists and Dicts used internally may be freed
20973 * while still in use. */
20974 garbage_collect(TRUE);
20975}
20976
20977#ifdef FEAT_JOB_CHANNEL
20978 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020979f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020980{
20981 rettv->v_type = VAR_CHANNEL;
20982 rettv->vval.v_channel = NULL;
20983}
20984#endif
20985
20986 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020987f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020988{
20989 rettv->v_type = VAR_DICT;
20990 rettv->vval.v_dict = NULL;
20991}
20992
20993#ifdef FEAT_JOB_CHANNEL
20994 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020995f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020996{
20997 rettv->v_type = VAR_JOB;
20998 rettv->vval.v_job = NULL;
20999}
21000#endif
21001
21002 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021003f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021004{
21005 rettv->v_type = VAR_LIST;
21006 rettv->vval.v_list = NULL;
21007}
21008
21009 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021010f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021011{
21012 rettv->v_type = VAR_PARTIAL;
21013 rettv->vval.v_partial = NULL;
21014}
21015
21016 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021017f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021018{
21019 rettv->v_type = VAR_STRING;
21020 rettv->vval.v_string = NULL;
21021}
21022
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021023 static void
21024f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
21025{
21026 time_for_testing = (time_t)get_tv_number(&argvars[0]);
21027}
21028
Bram Moolenaar975b5272016-03-15 23:10:59 +010021029#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
21030/*
21031 * Get a callback from "arg". It can be a Funcref or a function name.
21032 * When "arg" is zero return an empty string.
21033 * Return NULL for an invalid argument.
21034 */
21035 char_u *
21036get_callback(typval_T *arg, partial_T **pp)
21037{
21038 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
21039 {
21040 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010021041 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021042 return (*pp)->pt_name;
21043 }
21044 *pp = NULL;
21045 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
21046 return arg->vval.v_string;
21047 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
21048 return (char_u *)"";
21049 EMSG(_("E921: Invalid callback argument"));
21050 return NULL;
21051}
21052#endif
21053
21054#ifdef FEAT_TIMERS
21055/*
21056 * "timer_start(time, callback [, options])" function
21057 */
21058 static void
21059f_timer_start(typval_T *argvars, typval_T *rettv)
21060{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021061 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010021062 timer_T *timer;
21063 int repeat = 0;
21064 char_u *callback;
21065 dict_T *dict;
21066
Bram Moolenaar38499922016-04-22 20:46:52 +020021067 if (check_secure())
21068 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021069 if (argvars[2].v_type != VAR_UNKNOWN)
21070 {
21071 if (argvars[2].v_type != VAR_DICT
21072 || (dict = argvars[2].vval.v_dict) == NULL)
21073 {
21074 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21075 return;
21076 }
21077 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21078 repeat = get_dict_number(dict, (char_u *)"repeat");
21079 }
21080
21081 timer = create_timer(msec, repeat);
21082 callback = get_callback(&argvars[1], &timer->tr_partial);
21083 if (callback == NULL)
21084 {
21085 stop_timer(timer);
21086 rettv->vval.v_number = -1;
21087 }
21088 else
21089 {
21090 timer->tr_callback = vim_strsave(callback);
21091 rettv->vval.v_number = timer->tr_id;
21092 }
21093}
21094
21095/*
21096 * "timer_stop(timer)" function
21097 */
21098 static void
21099f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21100{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021101 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021102
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021103 if (argvars[0].v_type != VAR_NUMBER)
21104 {
21105 EMSG(_(e_number_exp));
21106 return;
21107 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021108 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021109 if (timer != NULL)
21110 stop_timer(timer);
21111}
21112#endif
21113
Bram Moolenaard52d9742005-08-21 22:20:28 +000021114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 * "tolower(string)" function
21116 */
21117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021118f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119{
21120 char_u *p;
21121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021122 p = vim_strsave(get_tv_string(&argvars[0]));
21123 rettv->v_type = VAR_STRING;
21124 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125
21126 if (p != NULL)
21127 while (*p != NUL)
21128 {
21129#ifdef FEAT_MBYTE
21130 int l;
21131
21132 if (enc_utf8)
21133 {
21134 int c, lc;
21135
21136 c = utf_ptr2char(p);
21137 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021138 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 /* TODO: reallocate string when byte count changes. */
21140 if (utf_char2len(lc) == l)
21141 utf_char2bytes(lc, p);
21142 p += l;
21143 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021144 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 p += l; /* skip multi-byte character */
21146 else
21147#endif
21148 {
21149 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21150 ++p;
21151 }
21152 }
21153}
21154
21155/*
21156 * "toupper(string)" function
21157 */
21158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021159f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021161 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021162 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163}
21164
21165/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021166 * "tr(string, fromstr, tostr)" function
21167 */
21168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021169f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021170{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021171 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021172 char_u *fromstr;
21173 char_u *tostr;
21174 char_u *p;
21175#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021176 int inlen;
21177 int fromlen;
21178 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021179 int idx;
21180 char_u *cpstr;
21181 int cplen;
21182 int first = TRUE;
21183#endif
21184 char_u buf[NUMBUFLEN];
21185 char_u buf2[NUMBUFLEN];
21186 garray_T ga;
21187
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021188 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021189 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21190 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021191
21192 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021193 rettv->v_type = VAR_STRING;
21194 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021195 if (fromstr == NULL || tostr == NULL)
21196 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021197 ga_init2(&ga, (int)sizeof(char), 80);
21198
21199#ifdef FEAT_MBYTE
21200 if (!has_mbyte)
21201#endif
21202 /* not multi-byte: fromstr and tostr must be the same length */
21203 if (STRLEN(fromstr) != STRLEN(tostr))
21204 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021205#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021206error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021207#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021208 EMSG2(_(e_invarg2), fromstr);
21209 ga_clear(&ga);
21210 return;
21211 }
21212
21213 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021214 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021215 {
21216#ifdef FEAT_MBYTE
21217 if (has_mbyte)
21218 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021219 inlen = (*mb_ptr2len)(in_str);
21220 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021221 cplen = inlen;
21222 idx = 0;
21223 for (p = fromstr; *p != NUL; p += fromlen)
21224 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021225 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021226 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021227 {
21228 for (p = tostr; *p != NUL; p += tolen)
21229 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021230 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021231 if (idx-- == 0)
21232 {
21233 cplen = tolen;
21234 cpstr = p;
21235 break;
21236 }
21237 }
21238 if (*p == NUL) /* tostr is shorter than fromstr */
21239 goto error;
21240 break;
21241 }
21242 ++idx;
21243 }
21244
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021245 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021246 {
21247 /* Check that fromstr and tostr have the same number of
21248 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021249 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021250 first = FALSE;
21251 for (p = tostr; *p != NUL; p += tolen)
21252 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021253 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021254 --idx;
21255 }
21256 if (idx != 0)
21257 goto error;
21258 }
21259
Bram Moolenaarcde88542015-08-11 19:14:00 +020021260 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021261 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021262 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021263
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021264 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021265 }
21266 else
21267#endif
21268 {
21269 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021270 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021271 if (p != NULL)
21272 ga_append(&ga, tostr[p - fromstr]);
21273 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021274 ga_append(&ga, *in_str);
21275 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021276 }
21277 }
21278
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021279 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021280 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021281 ga_append(&ga, NUL);
21282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021284}
21285
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021286#ifdef FEAT_FLOAT
21287/*
21288 * "trunc({float})" function
21289 */
21290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021291f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021292{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021293 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021294
21295 rettv->v_type = VAR_FLOAT;
21296 if (get_float_arg(argvars, &f) == OK)
21297 /* trunc() is not in C90, use floor() or ceil() instead. */
21298 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21299 else
21300 rettv->vval.v_float = 0.0;
21301}
21302#endif
21303
Bram Moolenaar8299df92004-07-10 09:47:34 +000021304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 * "type(expr)" function
21306 */
21307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021308f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021310 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021311
21312 switch (argvars[0].v_type)
21313 {
21314 case VAR_NUMBER: n = 0; break;
21315 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021316 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021317 case VAR_FUNC: n = 2; break;
21318 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021319 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021320 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021321 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021322 if (argvars[0].vval.v_number == VVAL_FALSE
21323 || argvars[0].vval.v_number == VVAL_TRUE)
21324 n = 6;
21325 else
21326 n = 7;
21327 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021328 case VAR_JOB: n = 8; break;
21329 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021330 case VAR_UNKNOWN:
21331 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21332 n = -1;
21333 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021334 }
21335 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336}
21337
21338/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021339 * "undofile(name)" function
21340 */
21341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021342f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021343{
21344 rettv->v_type = VAR_STRING;
21345#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021346 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021347 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021348
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021349 if (*fname == NUL)
21350 {
21351 /* If there is no file name there will be no undo file. */
21352 rettv->vval.v_string = NULL;
21353 }
21354 else
21355 {
21356 char_u *ffname = FullName_save(fname, FALSE);
21357
21358 if (ffname != NULL)
21359 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21360 vim_free(ffname);
21361 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021362 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021363#else
21364 rettv->vval.v_string = NULL;
21365#endif
21366}
21367
21368/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021369 * "undotree()" function
21370 */
21371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021372f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021373{
21374 if (rettv_dict_alloc(rettv) == OK)
21375 {
21376 dict_T *dict = rettv->vval.v_dict;
21377 list_T *list;
21378
Bram Moolenaar730cde92010-06-27 05:18:54 +020021379 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021380 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021381 dict_add_nr_str(dict, "save_last",
21382 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021383 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21384 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021385 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021386
21387 list = list_alloc();
21388 if (list != NULL)
21389 {
21390 u_eval_tree(curbuf->b_u_oldhead, list);
21391 dict_add_list(dict, "entries", list);
21392 }
21393 }
21394}
21395
21396/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021397 * "values(dict)" function
21398 */
21399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021400f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021401{
21402 dict_list(argvars, rettv, 1);
21403}
21404
21405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 * "virtcol(string)" function
21407 */
21408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021409f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410{
21411 colnr_T vcol = 0;
21412 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021413 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021415 fp = var2fpos(&argvars[0], FALSE, &fnum);
21416 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21417 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 {
21419 getvvcol(curwin, fp, NULL, NULL, &vcol);
21420 ++vcol;
21421 }
21422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021423 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424}
21425
21426/*
21427 * "visualmode()" function
21428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021430f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 char_u str[2];
21433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 str[0] = curbuf->b_visual_mode_eval;
21436 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021437 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438
21439 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021440 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442}
21443
21444/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021445 * "wildmenumode()" function
21446 */
21447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021448f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021449{
21450#ifdef FEAT_WILDMENU
21451 if (wild_menu_showing)
21452 rettv->vval.v_number = 1;
21453#endif
21454}
21455
21456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 * "winbufnr(nr)" function
21458 */
21459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021460f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461{
21462 win_T *wp;
21463
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021464 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021468 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469}
21470
21471/*
21472 * "wincol()" function
21473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021475f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476{
21477 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021478 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479}
21480
21481/*
21482 * "winheight(nr)" function
21483 */
21484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021485f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486{
21487 win_T *wp;
21488
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021489 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021491 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021493 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494}
21495
21496/*
21497 * "winline()" function
21498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021500f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501{
21502 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021503 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504}
21505
21506/*
21507 * "winnr()" function
21508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021510f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511{
21512 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021513
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021515 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021517 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518}
21519
21520/*
21521 * "winrestcmd()" function
21522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021524f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525{
21526#ifdef FEAT_WINDOWS
21527 win_T *wp;
21528 int winnr = 1;
21529 garray_T ga;
21530 char_u buf[50];
21531
21532 ga_init2(&ga, (int)sizeof(char), 70);
21533 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21534 {
21535 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21536 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21538 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 ++winnr;
21540 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021541 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021543 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021547 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548}
21549
21550/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021551 * "winrestview()" function
21552 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021554f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021555{
21556 dict_T *dict;
21557
21558 if (argvars[0].v_type != VAR_DICT
21559 || (dict = argvars[0].vval.v_dict) == NULL)
21560 EMSG(_(e_invarg));
21561 else
21562 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021563 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021564 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021565 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021566 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021567#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021568 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021569 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021570#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021571 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21572 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021573 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021574 curwin->w_set_curswant = FALSE;
21575 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021576
Bram Moolenaar82c25852014-05-28 16:47:16 +020021577 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021578 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021579#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021580 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021581 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021582#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021583 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021584 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021585 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021586 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021587
21588 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021589 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021590# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021591 win_new_width(curwin, W_WIDTH(curwin));
21592# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021593 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021594
Bram Moolenaarb851a962014-10-31 15:45:52 +010021595 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021596 curwin->w_topline = 1;
21597 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21598 curwin->w_topline = curbuf->b_ml.ml_line_count;
21599#ifdef FEAT_DIFF
21600 check_topfill(curwin, TRUE);
21601#endif
21602 }
21603}
21604
21605/*
21606 * "winsaveview()" function
21607 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021609f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021610{
21611 dict_T *dict;
21612
Bram Moolenaara800b422010-06-27 01:15:55 +020021613 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021614 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021615 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021616
21617 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21618 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21619#ifdef FEAT_VIRTUALEDIT
21620 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21621#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021622 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021623 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21624
21625 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21626#ifdef FEAT_DIFF
21627 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21628#endif
21629 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21630 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21631}
21632
21633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 * "winwidth(nr)" function
21635 */
21636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021637f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638{
21639 win_T *wp;
21640
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021641 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021643 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021645#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021646 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021648 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649#endif
21650}
21651
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021653 * "wordcount()" function
21654 */
21655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021656f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021657{
21658 if (rettv_dict_alloc(rettv) == FAIL)
21659 return;
21660 cursor_pos_info(rettv->vval.v_dict);
21661}
21662
21663/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021664 * Write list of strings to file
21665 */
21666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021667write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021668{
21669 listitem_T *li;
21670 int c;
21671 int ret = OK;
21672 char_u *s;
21673
21674 for (li = list->lv_first; li != NULL; li = li->li_next)
21675 {
21676 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21677 {
21678 if (*s == '\n')
21679 c = putc(NUL, fd);
21680 else
21681 c = putc(*s, fd);
21682 if (c == EOF)
21683 {
21684 ret = FAIL;
21685 break;
21686 }
21687 }
21688 if (!binary || li->li_next != NULL)
21689 if (putc('\n', fd) == EOF)
21690 {
21691 ret = FAIL;
21692 break;
21693 }
21694 if (ret == FAIL)
21695 {
21696 EMSG(_(e_write));
21697 break;
21698 }
21699 }
21700 return ret;
21701}
21702
21703/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021704 * "writefile()" function
21705 */
21706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021707f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021708{
21709 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021710 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021711 char_u *fname;
21712 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021713 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021714
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021715 if (check_restricted() || check_secure())
21716 return;
21717
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021718 if (argvars[0].v_type != VAR_LIST)
21719 {
21720 EMSG2(_(e_listarg), "writefile()");
21721 return;
21722 }
21723 if (argvars[0].vval.v_list == NULL)
21724 return;
21725
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021726 if (argvars[2].v_type != VAR_UNKNOWN)
21727 {
21728 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21729 binary = TRUE;
21730 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21731 append = TRUE;
21732 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021733
21734 /* Always open the file in binary mode, library functions have a mind of
21735 * their own about CR-LF conversion. */
21736 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021737 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21738 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021739 {
21740 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21741 ret = -1;
21742 }
21743 else
21744 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021745 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21746 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021747 fclose(fd);
21748 }
21749
21750 rettv->vval.v_number = ret;
21751}
21752
21753/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021754 * "xor(expr, expr)" function
21755 */
21756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021757f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021758{
21759 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21760 ^ get_tv_number_chk(&argvars[1], NULL);
21761}
21762
21763
21764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021766 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 */
21768 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021769var2fpos(
21770 typval_T *varp,
21771 int dollar_lnum, /* TRUE when $ is last line */
21772 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021774 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021776 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777
Bram Moolenaara5525202006-03-02 22:52:09 +000021778 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021779 if (varp->v_type == VAR_LIST)
21780 {
21781 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021782 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021783 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021784 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021785
21786 l = varp->vval.v_list;
21787 if (l == NULL)
21788 return NULL;
21789
21790 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021791 pos.lnum = list_find_nr(l, 0L, &error);
21792 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021793 return NULL; /* invalid line number */
21794
21795 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021796 pos.col = list_find_nr(l, 1L, &error);
21797 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021798 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021799 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021800
21801 /* We accept "$" for the column number: last column. */
21802 li = list_find(l, 1L);
21803 if (li != NULL && li->li_tv.v_type == VAR_STRING
21804 && li->li_tv.vval.v_string != NULL
21805 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21806 pos.col = len + 1;
21807
Bram Moolenaara5525202006-03-02 22:52:09 +000021808 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021809 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021810 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021811 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021812
Bram Moolenaara5525202006-03-02 22:52:09 +000021813#ifdef FEAT_VIRTUALEDIT
21814 /* Get the virtual offset. Defaults to zero. */
21815 pos.coladd = list_find_nr(l, 2L, &error);
21816 if (error)
21817 pos.coladd = 0;
21818#endif
21819
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021820 return &pos;
21821 }
21822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021823 name = get_tv_string_chk(varp);
21824 if (name == NULL)
21825 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021826 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021828 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21829 {
21830 if (VIsual_active)
21831 return &VIsual;
21832 return &curwin->w_cursor;
21833 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021834 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021836 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21838 return NULL;
21839 return pp;
21840 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021841
21842#ifdef FEAT_VIRTUALEDIT
21843 pos.coladd = 0;
21844#endif
21845
Bram Moolenaar477933c2007-07-17 14:32:23 +000021846 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021847 {
21848 pos.col = 0;
21849 if (name[1] == '0') /* "w0": first visible line */
21850 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021851 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021852 pos.lnum = curwin->w_topline;
21853 return &pos;
21854 }
21855 else if (name[1] == '$') /* "w$": last visible line */
21856 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021857 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021858 pos.lnum = curwin->w_botline - 1;
21859 return &pos;
21860 }
21861 }
21862 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021864 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 {
21866 pos.lnum = curbuf->b_ml.ml_line_count;
21867 pos.col = 0;
21868 }
21869 else
21870 {
21871 pos.lnum = curwin->w_cursor.lnum;
21872 pos.col = (colnr_T)STRLEN(ml_get_curline());
21873 }
21874 return &pos;
21875 }
21876 return NULL;
21877}
21878
21879/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021880 * Convert list in "arg" into a position and optional file number.
21881 * When "fnump" is NULL there is no file number, only 3 items.
21882 * Note that the column is passed on as-is, the caller may want to decrement
21883 * it to use 1 for the first column.
21884 * Return FAIL when conversion is not possible, doesn't check the position for
21885 * validity.
21886 */
21887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021888list2fpos(
21889 typval_T *arg,
21890 pos_T *posp,
21891 int *fnump,
21892 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021893{
21894 list_T *l = arg->vval.v_list;
21895 long i = 0;
21896 long n;
21897
Bram Moolenaar493c1782014-05-28 14:34:46 +020021898 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21899 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021900 if (arg->v_type != VAR_LIST
21901 || l == NULL
21902 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021903 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021904 return FAIL;
21905
21906 if (fnump != NULL)
21907 {
21908 n = list_find_nr(l, i++, NULL); /* fnum */
21909 if (n < 0)
21910 return FAIL;
21911 if (n == 0)
21912 n = curbuf->b_fnum; /* current buffer */
21913 *fnump = n;
21914 }
21915
21916 n = list_find_nr(l, i++, NULL); /* lnum */
21917 if (n < 0)
21918 return FAIL;
21919 posp->lnum = n;
21920
21921 n = list_find_nr(l, i++, NULL); /* col */
21922 if (n < 0)
21923 return FAIL;
21924 posp->col = n;
21925
21926#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021927 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021928 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021929 posp->coladd = 0;
21930 else
21931 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021932#endif
21933
Bram Moolenaar493c1782014-05-28 14:34:46 +020021934 if (curswantp != NULL)
21935 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21936
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021937 return OK;
21938}
21939
21940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 * Get the length of an environment variable name.
21942 * Advance "arg" to the first character after the name.
21943 * Return 0 for error.
21944 */
21945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021946get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947{
21948 char_u *p;
21949 int len;
21950
21951 for (p = *arg; vim_isIDc(*p); ++p)
21952 ;
21953 if (p == *arg) /* no name found */
21954 return 0;
21955
21956 len = (int)(p - *arg);
21957 *arg = p;
21958 return len;
21959}
21960
21961/*
21962 * Get the length of the name of a function or internal variable.
21963 * "arg" is advanced to the first non-white character after the name.
21964 * Return 0 if something is wrong.
21965 */
21966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021967get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968{
21969 char_u *p;
21970 int len;
21971
21972 /* Find the end of the name. */
21973 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021974 {
21975 if (*p == ':')
21976 {
21977 /* "s:" is start of "s:var", but "n:" is not and can be used in
21978 * slice "[n:]". Also "xx:" is not a namespace. */
21979 len = (int)(p - *arg);
21980 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21981 || len > 1)
21982 break;
21983 }
21984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 if (p == *arg) /* no name found */
21986 return 0;
21987
21988 len = (int)(p - *arg);
21989 *arg = skipwhite(p);
21990
21991 return len;
21992}
21993
21994/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021995 * Get the length of the name of a variable or function.
21996 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021998 * Return -1 if curly braces expansion failed.
21999 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 * If the name contains 'magic' {}'s, expand them and return the
22001 * expanded name in an allocated string via 'alias' - caller must free.
22002 */
22003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022004get_name_len(
22005 char_u **arg,
22006 char_u **alias,
22007 int evaluate,
22008 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009{
22010 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 char_u *p;
22012 char_u *expr_start;
22013 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014
22015 *alias = NULL; /* default to no alias */
22016
22017 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
22018 && (*arg)[2] == (int)KE_SNR)
22019 {
22020 /* hard coded <SNR>, already translated */
22021 *arg += 3;
22022 return get_id_len(arg) + 3;
22023 }
22024 len = eval_fname_script(*arg);
22025 if (len > 0)
22026 {
22027 /* literal "<SID>", "s:" or "<SNR>" */
22028 *arg += len;
22029 }
22030
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022032 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022034 p = find_name_end(*arg, &expr_start, &expr_end,
22035 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 if (expr_start != NULL)
22037 {
22038 char_u *temp_string;
22039
22040 if (!evaluate)
22041 {
22042 len += (int)(p - *arg);
22043 *arg = skipwhite(p);
22044 return len;
22045 }
22046
22047 /*
22048 * Include any <SID> etc in the expanded string:
22049 * Thus the -len here.
22050 */
22051 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
22052 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022053 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 *alias = temp_string;
22055 *arg = skipwhite(p);
22056 return (int)STRLEN(temp_string);
22057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058
22059 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022060 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 EMSG2(_(e_invexpr2), *arg);
22062
22063 return len;
22064}
22065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022066/*
22067 * Find the end of a variable or function name, taking care of magic braces.
22068 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
22069 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022070 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022071 * Return a pointer to just after the name. Equal to "arg" if there is no
22072 * valid name.
22073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022075find_name_end(
22076 char_u *arg,
22077 char_u **expr_start,
22078 char_u **expr_end,
22079 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022081 int mb_nest = 0;
22082 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022084 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022086 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022088 *expr_start = NULL;
22089 *expr_end = NULL;
22090 }
22091
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022092 /* Quick check for valid starting character. */
22093 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22094 return arg;
22095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022096 for (p = arg; *p != NUL
22097 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022098 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022099 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022100 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022101 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022102 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022103 if (*p == '\'')
22104 {
22105 /* skip over 'string' to avoid counting [ and ] inside it. */
22106 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22107 ;
22108 if (*p == NUL)
22109 break;
22110 }
22111 else if (*p == '"')
22112 {
22113 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22114 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22115 if (*p == '\\' && p[1] != NUL)
22116 ++p;
22117 if (*p == NUL)
22118 break;
22119 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022120 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22121 {
22122 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022123 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022124 len = (int)(p - arg);
22125 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022126 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022127 break;
22128 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022130 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022132 if (*p == '[')
22133 ++br_nest;
22134 else if (*p == ']')
22135 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022138 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022140 if (*p == '{')
22141 {
22142 mb_nest++;
22143 if (expr_start != NULL && *expr_start == NULL)
22144 *expr_start = p;
22145 }
22146 else if (*p == '}')
22147 {
22148 mb_nest--;
22149 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22150 *expr_end = p;
22151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 }
22154
22155 return p;
22156}
22157
22158/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022159 * Expands out the 'magic' {}'s in a variable/function name.
22160 * Note that this can call itself recursively, to deal with
22161 * constructs like foo{bar}{baz}{bam}
22162 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22163 * "in_start" ^
22164 * "expr_start" ^
22165 * "expr_end" ^
22166 * "in_end" ^
22167 *
22168 * Returns a new allocated string, which the caller must free.
22169 * Returns NULL for failure.
22170 */
22171 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022172make_expanded_name(
22173 char_u *in_start,
22174 char_u *expr_start,
22175 char_u *expr_end,
22176 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022177{
22178 char_u c1;
22179 char_u *retval = NULL;
22180 char_u *temp_result;
22181 char_u *nextcmd = NULL;
22182
22183 if (expr_end == NULL || in_end == NULL)
22184 return NULL;
22185 *expr_start = NUL;
22186 *expr_end = NUL;
22187 c1 = *in_end;
22188 *in_end = NUL;
22189
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022190 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022191 if (temp_result != NULL && nextcmd == NULL)
22192 {
22193 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22194 + (in_end - expr_end) + 1));
22195 if (retval != NULL)
22196 {
22197 STRCPY(retval, in_start);
22198 STRCAT(retval, temp_result);
22199 STRCAT(retval, expr_end + 1);
22200 }
22201 }
22202 vim_free(temp_result);
22203
22204 *in_end = c1; /* put char back for error messages */
22205 *expr_start = '{';
22206 *expr_end = '}';
22207
22208 if (retval != NULL)
22209 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022210 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022211 if (expr_start != NULL)
22212 {
22213 /* Further expansion! */
22214 temp_result = make_expanded_name(retval, expr_start,
22215 expr_end, temp_result);
22216 vim_free(retval);
22217 retval = temp_result;
22218 }
22219 }
22220
22221 return retval;
22222}
22223
22224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022226 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 */
22228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022229eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022231 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22232}
22233
22234/*
22235 * Return TRUE if character "c" can be used as the first character in a
22236 * variable or function name (excluding '{' and '}').
22237 */
22238 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022239eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022240{
22241 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242}
22243
22244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 * Set number v: variable to "val".
22246 */
22247 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022248set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022250 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251}
22252
22253/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022254 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022256 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022257get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022259 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260}
22261
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022262/*
22263 * Get string v: variable value. Uses a static buffer, can only be used once.
22264 */
22265 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022266get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022267{
22268 return get_tv_string(&vimvars[idx].vv_tv);
22269}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022270
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022272 * Get List v: variable value. Caller must take care of reference count when
22273 * needed.
22274 */
22275 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022276get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022277{
22278 return vimvars[idx].vv_list;
22279}
22280
22281/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022282 * Set v:char to character "c".
22283 */
22284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022285set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022286{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022287 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022288
22289#ifdef FEAT_MBYTE
22290 if (has_mbyte)
22291 buf[(*mb_char2bytes)(c, buf)] = NUL;
22292 else
22293#endif
22294 {
22295 buf[0] = c;
22296 buf[1] = NUL;
22297 }
22298 set_vim_var_string(VV_CHAR, buf, -1);
22299}
22300
22301/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022302 * Set v:count to "count" and v:count1 to "count1".
22303 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 */
22305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022306set_vcount(
22307 long count,
22308 long count1,
22309 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022311 if (set_prevcount)
22312 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022313 vimvars[VV_COUNT].vv_nr = count;
22314 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315}
22316
22317/*
22318 * Set string v: variable to a copy of "val".
22319 */
22320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022321set_vim_var_string(
22322 int idx,
22323 char_u *val,
22324 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325{
Bram Moolenaara542c682016-01-31 16:28:04 +010022326 clear_tv(&vimvars[idx].vv_di.di_tv);
22327 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022329 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022331 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022333 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334}
22335
22336/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022337 * Set List v: variable to "val".
22338 */
22339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022340set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022341{
Bram Moolenaara542c682016-01-31 16:28:04 +010022342 clear_tv(&vimvars[idx].vv_di.di_tv);
22343 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022344 vimvars[idx].vv_list = val;
22345 if (val != NULL)
22346 ++val->lv_refcount;
22347}
22348
22349/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022350 * Set Dictionary v: variable to "val".
22351 */
22352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022353set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022354{
22355 int todo;
22356 hashitem_T *hi;
22357
Bram Moolenaara542c682016-01-31 16:28:04 +010022358 clear_tv(&vimvars[idx].vv_di.di_tv);
22359 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022360 vimvars[idx].vv_dict = val;
22361 if (val != NULL)
22362 {
22363 ++val->dv_refcount;
22364
22365 /* Set readonly */
22366 todo = (int)val->dv_hashtab.ht_used;
22367 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22368 {
22369 if (HASHITEM_EMPTY(hi))
22370 continue;
22371 --todo;
22372 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22373 }
22374 }
22375}
22376
22377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 * Set v:register if needed.
22379 */
22380 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022381set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382{
22383 char_u regname;
22384
22385 if (c == 0 || c == ' ')
22386 regname = '"';
22387 else
22388 regname = c;
22389 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022390 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 set_vim_var_string(VV_REG, &regname, 1);
22392}
22393
22394/*
22395 * Get or set v:exception. If "oldval" == NULL, return the current value.
22396 * Otherwise, restore the value to "oldval" and return NULL.
22397 * Must always be called in pairs to save and restore v:exception! Does not
22398 * take care of memory allocations.
22399 */
22400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022401v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402{
22403 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022404 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405
Bram Moolenaare9a41262005-01-15 22:18:47 +000022406 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 return NULL;
22408}
22409
22410/*
22411 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22412 * Otherwise, restore the value to "oldval" and return NULL.
22413 * Must always be called in pairs to save and restore v:throwpoint! Does not
22414 * take care of memory allocations.
22415 */
22416 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022417v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418{
22419 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022420 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421
Bram Moolenaare9a41262005-01-15 22:18:47 +000022422 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 return NULL;
22424}
22425
22426#if defined(FEAT_AUTOCMD) || defined(PROTO)
22427/*
22428 * Set v:cmdarg.
22429 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22430 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22431 * Must always be called in pairs!
22432 */
22433 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022434set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435{
22436 char_u *oldval;
22437 char_u *newval;
22438 unsigned len;
22439
Bram Moolenaare9a41262005-01-15 22:18:47 +000022440 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022441 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022443 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022444 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022445 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 }
22447
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022448 if (eap->force_bin == FORCE_BIN)
22449 len = 6;
22450 else if (eap->force_bin == FORCE_NOBIN)
22451 len = 8;
22452 else
22453 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022454
22455 if (eap->read_edit)
22456 len += 7;
22457
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022458 if (eap->force_ff != 0)
22459 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22460# ifdef FEAT_MBYTE
22461 if (eap->force_enc != 0)
22462 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022463 if (eap->bad_char != 0)
22464 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022465# endif
22466
22467 newval = alloc(len + 1);
22468 if (newval == NULL)
22469 return NULL;
22470
22471 if (eap->force_bin == FORCE_BIN)
22472 sprintf((char *)newval, " ++bin");
22473 else if (eap->force_bin == FORCE_NOBIN)
22474 sprintf((char *)newval, " ++nobin");
22475 else
22476 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022477
22478 if (eap->read_edit)
22479 STRCAT(newval, " ++edit");
22480
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022481 if (eap->force_ff != 0)
22482 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22483 eap->cmd + eap->force_ff);
22484# ifdef FEAT_MBYTE
22485 if (eap->force_enc != 0)
22486 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22487 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022488 if (eap->bad_char == BAD_KEEP)
22489 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22490 else if (eap->bad_char == BAD_DROP)
22491 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22492 else if (eap->bad_char != 0)
22493 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022494# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022495 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022496 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497}
22498#endif
22499
22500/*
22501 * Get the value of internal variable "name".
22502 * Return OK or FAIL.
22503 */
22504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022505get_var_tv(
22506 char_u *name,
22507 int len, /* length of "name" */
22508 typval_T *rettv, /* NULL when only checking existence */
22509 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22510 int verbose, /* may give error message */
22511 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512{
22513 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022514 typval_T *tv = NULL;
22515 typval_T atv;
22516 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518
22519 /* truncate the name, so that we can use strcmp() */
22520 cc = name[len];
22521 name[len] = NUL;
22522
22523 /*
22524 * Check for "b:changedtick".
22525 */
22526 if (STRCMP(name, "b:changedtick") == 0)
22527 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022528 atv.v_type = VAR_NUMBER;
22529 atv.vval.v_number = curbuf->b_changedtick;
22530 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 }
22532
22533 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 * Check for user-defined variables.
22535 */
22536 else
22537 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022538 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022540 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022541 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022542 if (dip != NULL)
22543 *dip = v;
22544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 }
22546
Bram Moolenaare9a41262005-01-15 22:18:47 +000022547 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022549 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022550 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 ret = FAIL;
22552 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022553 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022554 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555
22556 name[len] = cc;
22557
22558 return ret;
22559}
22560
22561/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022562 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22563 * Also handle function call with Funcref variable: func(expr)
22564 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22565 */
22566 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022567handle_subscript(
22568 char_u **arg,
22569 typval_T *rettv,
22570 int evaluate, /* do more than finding the end */
22571 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022572{
22573 int ret = OK;
22574 dict_T *selfdict = NULL;
22575 char_u *s;
22576 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022577 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022578
22579 while (ret == OK
22580 && (**arg == '['
22581 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022582 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22583 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022584 && !vim_iswhite(*(*arg - 1)))
22585 {
22586 if (**arg == '(')
22587 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022588 partial_T *pt = NULL;
22589
Bram Moolenaard9fba312005-06-26 22:34:35 +000022590 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022591 if (evaluate)
22592 {
22593 functv = *rettv;
22594 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022595
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022596 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022597 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022598 {
22599 pt = functv.vval.v_partial;
22600 s = pt->pt_name;
22601 }
22602 else
22603 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022604 }
22605 else
22606 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022607 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022608 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022609 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022610
22611 /* Clear the funcref afterwards, so that deleting it while
22612 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022613 if (evaluate)
22614 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022615
22616 /* Stop the expression evaluation when immediately aborting on
22617 * error, or when an interrupt occurred or an exception was thrown
22618 * but not caught. */
22619 if (aborting())
22620 {
22621 if (ret == OK)
22622 clear_tv(rettv);
22623 ret = FAIL;
22624 }
22625 dict_unref(selfdict);
22626 selfdict = NULL;
22627 }
22628 else /* **arg == '[' || **arg == '.' */
22629 {
22630 dict_unref(selfdict);
22631 if (rettv->v_type == VAR_DICT)
22632 {
22633 selfdict = rettv->vval.v_dict;
22634 if (selfdict != NULL)
22635 ++selfdict->dv_refcount;
22636 }
22637 else
22638 selfdict = NULL;
22639 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22640 {
22641 clear_tv(rettv);
22642 ret = FAIL;
22643 }
22644 }
22645 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022646
Bram Moolenaar1d429612016-05-24 15:44:17 +020022647 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22648 * Don't do this when "Func" is already a partial that was bound
22649 * explicitly (pt_auto is FALSE). */
22650 if (selfdict != NULL
22651 && (rettv->v_type == VAR_FUNC
22652 || (rettv->v_type == VAR_PARTIAL
22653 && (rettv->vval.v_partial->pt_auto
22654 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022655 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022656 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22657 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022658 char_u *tofree = NULL;
22659 ufunc_T *fp;
22660 char_u fname_buf[FLEN_FIXED + 1];
22661 int error;
22662
22663 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022664 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022665 fp = find_func(fname);
22666 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022667
Bram Moolenaar65639032016-03-16 21:40:30 +010022668 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022669 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022670 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22671
22672 if (pt != NULL)
22673 {
22674 pt->pt_refcount = 1;
22675 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022676 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022677 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022678 if (rettv->v_type == VAR_FUNC)
22679 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022680 /* Just a function: Take over the function name and use
22681 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022682 pt->pt_name = rettv->vval.v_string;
22683 }
22684 else
22685 {
22686 partial_T *ret_pt = rettv->vval.v_partial;
22687 int i;
22688
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022689 /* Partial: copy the function name, use selfdict and copy
22690 * args. Can't take over name or args, the partial might
22691 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022692 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022693 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022694 if (ret_pt->pt_argc > 0)
22695 {
22696 pt->pt_argv = (typval_T *)alloc(
22697 sizeof(typval_T) * ret_pt->pt_argc);
22698 if (pt->pt_argv == NULL)
22699 /* out of memory: drop the arguments */
22700 pt->pt_argc = 0;
22701 else
22702 {
22703 pt->pt_argc = ret_pt->pt_argc;
22704 for (i = 0; i < pt->pt_argc; i++)
22705 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22706 }
22707 }
22708 partial_unref(ret_pt);
22709 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022710 rettv->v_type = VAR_PARTIAL;
22711 rettv->vval.v_partial = pt;
22712 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022713 }
22714 }
22715
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022716 dict_unref(selfdict);
22717 return ret;
22718}
22719
22720/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022721 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022722 * value).
22723 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022724 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022725alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022726{
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022728}
22729
22730/*
22731 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 * The string "s" must have been allocated, it is consumed.
22733 * Return NULL for out of memory, the variable otherwise.
22734 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022735 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022736alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737{
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022740 rettv = alloc_tv();
22741 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022743 rettv->v_type = VAR_STRING;
22744 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 }
22746 else
22747 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022748 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749}
22750
22751/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022752 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022754 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022755free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756{
22757 if (varp != NULL)
22758 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022759 switch (varp->v_type)
22760 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022761 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022762 func_unref(varp->vval.v_string);
22763 /*FALLTHROUGH*/
22764 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022765 vim_free(varp->vval.v_string);
22766 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022767 case VAR_PARTIAL:
22768 partial_unref(varp->vval.v_partial);
22769 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022770 case VAR_LIST:
22771 list_unref(varp->vval.v_list);
22772 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022773 case VAR_DICT:
22774 dict_unref(varp->vval.v_dict);
22775 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022776 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022777#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022778 job_unref(varp->vval.v_job);
22779 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022780#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022781 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022782#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022783 channel_unref(varp->vval.v_channel);
22784 break;
22785#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022786 case VAR_NUMBER:
22787 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022788 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022789 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022790 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 vim_free(varp);
22793 }
22794}
22795
22796/*
22797 * Free the memory for a variable value and set the value to NULL or 0.
22798 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022800clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801{
22802 if (varp != NULL)
22803 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022804 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022806 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022807 func_unref(varp->vval.v_string);
22808 /*FALLTHROUGH*/
22809 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022810 vim_free(varp->vval.v_string);
22811 varp->vval.v_string = NULL;
22812 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022813 case VAR_PARTIAL:
22814 partial_unref(varp->vval.v_partial);
22815 varp->vval.v_partial = NULL;
22816 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022817 case VAR_LIST:
22818 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022819 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022820 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022821 case VAR_DICT:
22822 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022823 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022824 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022825 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022826 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022827 varp->vval.v_number = 0;
22828 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022829 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022830#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022831 varp->vval.v_float = 0.0;
22832 break;
22833#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022834 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022835#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022836 job_unref(varp->vval.v_job);
22837 varp->vval.v_job = NULL;
22838#endif
22839 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022840 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022841#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022842 channel_unref(varp->vval.v_channel);
22843 varp->vval.v_channel = NULL;
22844#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022845 case VAR_UNKNOWN:
22846 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022848 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 }
22850}
22851
22852/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022853 * Set the value of a variable to NULL without freeing items.
22854 */
22855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022856init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022857{
22858 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022859 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022860}
22861
22862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863 * Get the number value of a variable.
22864 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022865 * For incompatible types, return 0.
22866 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22867 * caller of incompatible types: it sets *denote to TRUE if "denote"
22868 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022870 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022871get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022873 int error = FALSE;
22874
22875 return get_tv_number_chk(varp, &error); /* return 0L on error */
22876}
22877
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022878 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022879get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022880{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022881 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022883 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022885 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022886 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022887 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022888#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022889 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022890 break;
22891#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022892 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022893 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022894 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022895 break;
22896 case VAR_STRING:
22897 if (varp->vval.v_string != NULL)
22898 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022899 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022900 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022901 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022902 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022903 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022904 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022905 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022906 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022907 case VAR_SPECIAL:
22908 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22909 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022910 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022911#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022912 EMSG(_("E910: Using a Job as a Number"));
22913 break;
22914#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022915 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022916#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022917 EMSG(_("E913: Using a Channel as a Number"));
22918 break;
22919#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022920 case VAR_UNKNOWN:
22921 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022922 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022924 if (denote == NULL) /* useful for values that must be unsigned */
22925 n = -1;
22926 else
22927 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022928 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929}
22930
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022931#ifdef FEAT_FLOAT
22932 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022933get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022934{
22935 switch (varp->v_type)
22936 {
22937 case VAR_NUMBER:
22938 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022939 case VAR_FLOAT:
22940 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022941 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022942 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022943 EMSG(_("E891: Using a Funcref as a Float"));
22944 break;
22945 case VAR_STRING:
22946 EMSG(_("E892: Using a String as a Float"));
22947 break;
22948 case VAR_LIST:
22949 EMSG(_("E893: Using a List as a Float"));
22950 break;
22951 case VAR_DICT:
22952 EMSG(_("E894: Using a Dictionary as a Float"));
22953 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022954 case VAR_SPECIAL:
22955 EMSG(_("E907: Using a special value as a Float"));
22956 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022957 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022958# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022959 EMSG(_("E911: Using a Job as a Float"));
22960 break;
22961# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022962 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022963# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022964 EMSG(_("E914: Using a Channel as a Float"));
22965 break;
22966# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022967 case VAR_UNKNOWN:
22968 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022969 break;
22970 }
22971 return 0;
22972}
22973#endif
22974
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022976 * Get the lnum from the first argument.
22977 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022978 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 */
22980 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022981get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982{
Bram Moolenaar33570922005-01-25 22:26:29 +000022983 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 linenr_T lnum;
22985
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022986 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 if (lnum == 0) /* no valid number, try using line() */
22988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022989 rettv.v_type = VAR_NUMBER;
22990 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022991 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022992 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 }
22994 return lnum;
22995}
22996
22997/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022998 * Get the lnum from the first argument.
22999 * Also accepts "$", then "buf" is used.
23000 * Returns 0 on error.
23001 */
23002 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023003get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000023004{
23005 if (argvars[0].v_type == VAR_STRING
23006 && argvars[0].vval.v_string != NULL
23007 && argvars[0].vval.v_string[0] == '$'
23008 && buf != NULL)
23009 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023010 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000023011}
23012
23013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 * Get the string value of a variable.
23015 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000023016 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23017 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 * If the String variable has never been set, return an empty string.
23019 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023020 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
23021 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023023 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023024get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025{
23026 static char_u mybuf[NUMBUFLEN];
23027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023028 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029}
23030
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023031 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023032get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023034 char_u *res = get_tv_string_buf_chk(varp, buf);
23035
23036 return res != NULL ? res : (char_u *)"";
23037}
23038
Bram Moolenaar7d647822014-04-05 21:28:56 +020023039/*
23040 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23041 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000023042 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023043get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023044{
23045 static char_u mybuf[NUMBUFLEN];
23046
23047 return get_tv_string_buf_chk(varp, mybuf);
23048}
23049
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023050 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023051get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023052{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023053 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023055 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023056 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
23057 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023058 return buf;
23059 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023060 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023061 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023062 break;
23063 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023064 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000023065 break;
23066 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023067 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023068 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023069 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023070#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020023071 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023072 break;
23073#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023074 case VAR_STRING:
23075 if (varp->vval.v_string != NULL)
23076 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023077 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023078 case VAR_SPECIAL:
23079 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23080 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023081 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023082#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023083 {
23084 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023085 char *status;
23086
23087 if (job == NULL)
23088 return (char_u *)"no process";
23089 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023090 : job->jv_status == JOB_ENDED ? "dead"
23091 : "run";
23092# ifdef UNIX
23093 vim_snprintf((char *)buf, NUMBUFLEN,
23094 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023095# elif defined(WIN32)
23096 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023097 "process %ld %s",
23098 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023099 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023100# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023101 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023102 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23103# endif
23104 return buf;
23105 }
23106#endif
23107 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023108 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023109#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023110 {
23111 channel_T *channel = varp->vval.v_channel;
23112 char *status = channel_status(channel);
23113
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023114 if (channel == NULL)
23115 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23116 else
23117 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023118 "channel %d %s", channel->ch_id, status);
23119 return buf;
23120 }
23121#endif
23122 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023123 case VAR_UNKNOWN:
23124 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023125 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023127 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128}
23129
23130/*
23131 * Find variable "name" in the list of variables.
23132 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023133 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023134 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023135 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023137 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023138find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023141 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142
Bram Moolenaara7043832005-01-21 11:56:39 +000023143 ht = find_var_ht(name, &varname);
23144 if (htp != NULL)
23145 *htp = ht;
23146 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023148 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149}
23150
23151/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023152 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023153 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023155 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023156find_var_in_ht(
23157 hashtab_T *ht,
23158 int htname,
23159 char_u *varname,
23160 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023161{
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 hashitem_T *hi;
23163
23164 if (*varname == NUL)
23165 {
23166 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023167 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023169 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 case 'g': return &globvars_var;
23171 case 'v': return &vimvars_var;
23172 case 'b': return &curbuf->b_bufvar;
23173 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023174#ifdef FEAT_WINDOWS
23175 case 't': return &curtab->tp_winvar;
23176#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023177 case 'l': return current_funccal == NULL
23178 ? NULL : &current_funccal->l_vars_var;
23179 case 'a': return current_funccal == NULL
23180 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023181 }
23182 return NULL;
23183 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023184
23185 hi = hash_find(ht, varname);
23186 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023187 {
23188 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023189 * worked find the variable again. Don't auto-load a script if it was
23190 * loaded already, otherwise it would be loaded every time when
23191 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023192 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023193 {
23194 /* Note: script_autoload() may make "hi" invalid. It must either
23195 * be obtained again or not used. */
23196 if (!script_autoload(varname, FALSE) || aborting())
23197 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023198 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023199 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023200 if (HASHITEM_EMPTY(hi))
23201 return NULL;
23202 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023203 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023204}
23205
23206/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023208 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023209 * Set "varname" to the start of name without ':'.
23210 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023211 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023212find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023214 hashitem_T *hi;
23215
Bram Moolenaar73627d02015-08-11 15:46:09 +020023216 if (name[0] == NUL)
23217 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 if (name[1] != ':')
23219 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023220 /* The name must not start with a colon or #. */
23221 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222 return NULL;
23223 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023224
23225 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023226 hi = hash_find(&compat_hashtab, name);
23227 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023228 return &compat_hashtab;
23229
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023231 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023232 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 }
23234 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023235 if (*name == 'g') /* global variable */
23236 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023237 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23238 */
23239 if (vim_strchr(name + 2, ':') != NULL
23240 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023241 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023243 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023245 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023246#ifdef FEAT_WINDOWS
23247 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023248 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023249#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 if (*name == 'v') /* v: variable */
23251 return &vimvarht;
23252 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023253 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023254 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023255 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 if (*name == 's' /* script variable */
23257 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23258 return &SCRIPT_VARS(current_SID);
23259 return NULL;
23260}
23261
23262/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023263 * Get function call environment based on bactrace debug level
23264 */
23265 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023266get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023267{
23268 int i;
23269 funccall_T *funccal;
23270 funccall_T *temp_funccal;
23271
23272 funccal = current_funccal;
23273 if (debug_backtrace_level > 0)
23274 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023275 for (i = 0; i < debug_backtrace_level; i++)
23276 {
23277 temp_funccal = funccal->caller;
23278 if (temp_funccal)
23279 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023280 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023281 /* backtrace level overflow. reset to max */
23282 debug_backtrace_level = i;
23283 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023284 }
23285 return funccal;
23286}
23287
23288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023290 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 * Returns NULL when it doesn't exist.
23292 */
23293 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023294get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295{
Bram Moolenaar33570922005-01-25 22:26:29 +000023296 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023298 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 if (v == NULL)
23300 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023301 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302}
23303
23304/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023305 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 * sourcing this script and when executing functions defined in the script.
23307 */
23308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023309new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310{
Bram Moolenaara7043832005-01-21 11:56:39 +000023311 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023312 hashtab_T *ht;
23313 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023314
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23316 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023317 /* Re-allocating ga_data means that an ht_array pointing to
23318 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023319 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023320 for (i = 1; i <= ga_scripts.ga_len; ++i)
23321 {
23322 ht = &SCRIPT_VARS(i);
23323 if (ht->ht_mask == HT_INIT_SIZE - 1)
23324 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023325 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023326 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023327 }
23328
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329 while (ga_scripts.ga_len < id)
23330 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023331 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023332 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023333 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335 }
23336 }
23337}
23338
23339/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023340 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23341 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342 */
23343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023344init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345{
Bram Moolenaar33570922005-01-25 22:26:29 +000023346 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023347 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023348 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023349 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023350 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023351 dict_var->di_tv.vval.v_dict = dict;
23352 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023353 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023354 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23355 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356}
23357
23358/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023359 * Unreference a dictionary initialized by init_var_dict().
23360 */
23361 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023362unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023363{
23364 /* Now the dict needs to be freed if no one else is using it, go back to
23365 * normal reference counting. */
23366 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23367 dict_unref(dict);
23368}
23369
23370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023372 * Frees all allocated variables and the value they contain.
23373 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 */
23375 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023376vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023377{
23378 vars_clear_ext(ht, TRUE);
23379}
23380
23381/*
23382 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23383 */
23384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023385vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386{
Bram Moolenaara7043832005-01-21 11:56:39 +000023387 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023388 hashitem_T *hi;
23389 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390
Bram Moolenaar33570922005-01-25 22:26:29 +000023391 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023392 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023393 for (hi = ht->ht_array; todo > 0; ++hi)
23394 {
23395 if (!HASHITEM_EMPTY(hi))
23396 {
23397 --todo;
23398
Bram Moolenaar33570922005-01-25 22:26:29 +000023399 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023400 * ht_array might change then. hash_clear() takes care of it
23401 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023402 v = HI2DI(hi);
23403 if (free_val)
23404 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023405 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023406 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023407 }
23408 }
23409 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023410 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411}
23412
Bram Moolenaara7043832005-01-21 11:56:39 +000023413/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023414 * Delete a variable from hashtab "ht" at item "hi".
23415 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023418delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419{
Bram Moolenaar33570922005-01-25 22:26:29 +000023420 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023421
23422 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023423 clear_tv(&di->di_tv);
23424 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425}
23426
23427/*
23428 * List the value of one internal variable.
23429 */
23430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023431list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023433 char_u *tofree;
23434 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023435 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023436
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023437 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023438 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023439 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023440 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441}
23442
Bram Moolenaar071d4272004-06-13 20:20:40 +000023443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023444list_one_var_a(
23445 char_u *prefix,
23446 char_u *name,
23447 int type,
23448 char_u *string,
23449 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450{
Bram Moolenaar31859182007-08-14 20:41:13 +000023451 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23452 msg_start();
23453 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 if (name != NULL) /* "a:" vars don't have a name stored */
23455 msg_puts(name);
23456 msg_putchar(' ');
23457 msg_advance(22);
23458 if (type == VAR_NUMBER)
23459 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023460 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023461 msg_putchar('*');
23462 else if (type == VAR_LIST)
23463 {
23464 msg_putchar('[');
23465 if (*string == '[')
23466 ++string;
23467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023468 else if (type == VAR_DICT)
23469 {
23470 msg_putchar('{');
23471 if (*string == '{')
23472 ++string;
23473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 else
23475 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023476
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023478
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023479 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023480 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023481 if (*first)
23482 {
23483 msg_clr_eos();
23484 *first = FALSE;
23485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486}
23487
23488/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023489 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 * If the variable already exists, the value is updated.
23491 * Otherwise the variable is created.
23492 */
23493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023494set_var(
23495 char_u *name,
23496 typval_T *tv,
23497 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498{
Bram Moolenaar33570922005-01-25 22:26:29 +000023499 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023501 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023502
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023503 ht = find_var_ht(name, &varname);
23504 if (ht == NULL || *varname == NUL)
23505 {
23506 EMSG2(_(e_illvar), name);
23507 return;
23508 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023509 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023510
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023511 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23512 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023513 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023514
Bram Moolenaar33570922005-01-25 22:26:29 +000023515 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023517 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023518 if (var_check_ro(v->di_flags, name, FALSE)
23519 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023520 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023521
23522 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023523 * Handle setting internal v: variables separately where needed to
23524 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023525 */
23526 if (ht == &vimvarht)
23527 {
23528 if (v->di_tv.v_type == VAR_STRING)
23529 {
23530 vim_free(v->di_tv.vval.v_string);
23531 if (copy || tv->v_type != VAR_STRING)
23532 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23533 else
23534 {
23535 /* Take over the string to avoid an extra alloc/free. */
23536 v->di_tv.vval.v_string = tv->vval.v_string;
23537 tv->vval.v_string = NULL;
23538 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023539 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023540 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023541 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023542 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023543 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023544 if (STRCMP(varname, "searchforward") == 0)
23545 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023546#ifdef FEAT_SEARCH_EXTRA
23547 else if (STRCMP(varname, "hlsearch") == 0)
23548 {
23549 no_hlsearch = !v->di_tv.vval.v_number;
23550 redraw_all_later(SOME_VALID);
23551 }
23552#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023553 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023554 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023555 else if (v->di_tv.v_type != tv->v_type)
23556 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023557 }
23558
23559 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023560 }
23561 else /* add a new variable */
23562 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023563 /* Can't add "v:" variable. */
23564 if (ht == &vimvarht)
23565 {
23566 EMSG2(_(e_illvar), name);
23567 return;
23568 }
23569
Bram Moolenaar92124a32005-06-17 22:03:40 +000023570 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023571 if (!valid_varname(varname))
23572 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023573
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023574 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23575 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023576 if (v == NULL)
23577 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023578 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023579 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023581 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 return;
23583 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023584 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023586
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023587 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023588 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023589 else
23590 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023591 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023592 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023593 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595}
23596
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023597/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023598 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023599 * Also give an error message.
23600 */
23601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023602var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023603{
23604 if (flags & DI_FLAGS_RO)
23605 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023606 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023607 return TRUE;
23608 }
23609 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23610 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023611 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023612 return TRUE;
23613 }
23614 return FALSE;
23615}
23616
23617/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023618 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23619 * Also give an error message.
23620 */
23621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023622var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023623{
23624 if (flags & DI_FLAGS_FIX)
23625 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023626 EMSG2(_("E795: Cannot delete variable %s"),
23627 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023628 return TRUE;
23629 }
23630 return FALSE;
23631}
23632
23633/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023634 * Check if a funcref is assigned to a valid variable name.
23635 * Return TRUE and give an error if not.
23636 */
23637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023638var_check_func_name(
23639 char_u *name, /* points to start of variable name */
23640 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023641{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023642 /* Allow for w: b: s: and t:. */
23643 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023644 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23645 ? name[2] : name[0]))
23646 {
23647 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23648 name);
23649 return TRUE;
23650 }
23651 /* Don't allow hiding a function. When "v" is not NULL we might be
23652 * assigning another function to the same var, the type is checked
23653 * below. */
23654 if (new_var && function_exists(name))
23655 {
23656 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23657 name);
23658 return TRUE;
23659 }
23660 return FALSE;
23661}
23662
23663/*
23664 * Check if a variable name is valid.
23665 * Return FALSE and give an error if not.
23666 */
23667 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023668valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023669{
23670 char_u *p;
23671
23672 for (p = varname; *p != NUL; ++p)
23673 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23674 && *p != AUTOLOAD_CHAR)
23675 {
23676 EMSG2(_(e_illvar), varname);
23677 return FALSE;
23678 }
23679 return TRUE;
23680}
23681
23682/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023683 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023684 * Also give an error message, using "name" or _("name") when use_gettext is
23685 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023686 */
23687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023688tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023689{
23690 if (lock & VAR_LOCKED)
23691 {
23692 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023693 name == NULL ? (char_u *)_("Unknown")
23694 : use_gettext ? (char_u *)_(name)
23695 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023696 return TRUE;
23697 }
23698 if (lock & VAR_FIXED)
23699 {
23700 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023701 name == NULL ? (char_u *)_("Unknown")
23702 : use_gettext ? (char_u *)_(name)
23703 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023704 return TRUE;
23705 }
23706 return FALSE;
23707}
23708
23709/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023710 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023711 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023712 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023713 * It is OK for "from" and "to" to point to the same item. This is used to
23714 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023715 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023717copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023719 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023720 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023721 switch (from->v_type)
23722 {
23723 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023724 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023725 to->vval.v_number = from->vval.v_number;
23726 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023727 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023728#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023729 to->vval.v_float = from->vval.v_float;
23730 break;
23731#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023732 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023733#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023734 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023735 if (to->vval.v_job != NULL)
23736 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023737 break;
23738#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023739 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023740#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023741 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023742 if (to->vval.v_channel != NULL)
23743 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023744 break;
23745#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023746 case VAR_STRING:
23747 case VAR_FUNC:
23748 if (from->vval.v_string == NULL)
23749 to->vval.v_string = NULL;
23750 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023751 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023752 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023753 if (from->v_type == VAR_FUNC)
23754 func_ref(to->vval.v_string);
23755 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023756 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023757 case VAR_PARTIAL:
23758 if (from->vval.v_partial == NULL)
23759 to->vval.v_partial = NULL;
23760 else
23761 {
23762 to->vval.v_partial = from->vval.v_partial;
23763 ++to->vval.v_partial->pt_refcount;
23764 }
23765 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023766 case VAR_LIST:
23767 if (from->vval.v_list == NULL)
23768 to->vval.v_list = NULL;
23769 else
23770 {
23771 to->vval.v_list = from->vval.v_list;
23772 ++to->vval.v_list->lv_refcount;
23773 }
23774 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023775 case VAR_DICT:
23776 if (from->vval.v_dict == NULL)
23777 to->vval.v_dict = NULL;
23778 else
23779 {
23780 to->vval.v_dict = from->vval.v_dict;
23781 ++to->vval.v_dict->dv_refcount;
23782 }
23783 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023784 case VAR_UNKNOWN:
23785 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023786 break;
23787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788}
23789
23790/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023791 * Make a copy of an item.
23792 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023793 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23794 * reference to an already copied list/dict can be used.
23795 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023796 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023797 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023798item_copy(
23799 typval_T *from,
23800 typval_T *to,
23801 int deep,
23802 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023803{
23804 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023805 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023806
Bram Moolenaar33570922005-01-25 22:26:29 +000023807 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023808 {
23809 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023810 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023811 }
23812 ++recurse;
23813
23814 switch (from->v_type)
23815 {
23816 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023817 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023818 case VAR_STRING:
23819 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023820 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023821 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023822 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023823 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023824 copy_tv(from, to);
23825 break;
23826 case VAR_LIST:
23827 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023828 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023829 if (from->vval.v_list == NULL)
23830 to->vval.v_list = NULL;
23831 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23832 {
23833 /* use the copy made earlier */
23834 to->vval.v_list = from->vval.v_list->lv_copylist;
23835 ++to->vval.v_list->lv_refcount;
23836 }
23837 else
23838 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23839 if (to->vval.v_list == NULL)
23840 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023841 break;
23842 case VAR_DICT:
23843 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023844 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023845 if (from->vval.v_dict == NULL)
23846 to->vval.v_dict = NULL;
23847 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23848 {
23849 /* use the copy made earlier */
23850 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23851 ++to->vval.v_dict->dv_refcount;
23852 }
23853 else
23854 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23855 if (to->vval.v_dict == NULL)
23856 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023857 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023858 case VAR_UNKNOWN:
23859 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023860 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023861 }
23862 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023863 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023864}
23865
23866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867 * ":echo expr1 ..." print each argument separated with a space, add a
23868 * newline at the end.
23869 * ":echon expr1 ..." print each argument plain.
23870 */
23871 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023872ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873{
23874 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023875 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023876 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 char_u *p;
23878 int needclr = TRUE;
23879 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023880 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881
23882 if (eap->skip)
23883 ++emsg_skip;
23884 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23885 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023886 /* If eval1() causes an error message the text from the command may
23887 * still need to be cleared. E.g., "echo 22,44". */
23888 need_clr_eos = needclr;
23889
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023891 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 {
23893 /*
23894 * Report the invalid expression unless the expression evaluation
23895 * has been cancelled due to an aborting error, an interrupt, or an
23896 * exception.
23897 */
23898 if (!aborting())
23899 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023900 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 break;
23902 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023903 need_clr_eos = FALSE;
23904
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 if (!eap->skip)
23906 {
23907 if (atstart)
23908 {
23909 atstart = FALSE;
23910 /* Call msg_start() after eval1(), evaluating the expression
23911 * may cause a message to appear. */
23912 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023913 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023914 /* Mark the saved text as finishing the line, so that what
23915 * follows is displayed on a new line when scrolling back
23916 * at the more prompt. */
23917 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 }
23921 else if (eap->cmdidx == CMD_echo)
23922 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023923 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023924 if (p != NULL)
23925 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023927 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023929 if (*p != TAB && needclr)
23930 {
23931 /* remove any text still there from the command */
23932 msg_clr_eos();
23933 needclr = FALSE;
23934 }
23935 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936 }
23937 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023938 {
23939#ifdef FEAT_MBYTE
23940 if (has_mbyte)
23941 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023942 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023943
23944 (void)msg_outtrans_len_attr(p, i, echo_attr);
23945 p += i - 1;
23946 }
23947 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023949 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023952 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023954 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 arg = skipwhite(arg);
23956 }
23957 eap->nextcmd = check_nextcmd(arg);
23958
23959 if (eap->skip)
23960 --emsg_skip;
23961 else
23962 {
23963 /* remove text that may still be there from the command */
23964 if (needclr)
23965 msg_clr_eos();
23966 if (eap->cmdidx == CMD_echo)
23967 msg_end();
23968 }
23969}
23970
23971/*
23972 * ":echohl {name}".
23973 */
23974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023975ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976{
23977 int id;
23978
23979 id = syn_name2id(eap->arg);
23980 if (id == 0)
23981 echo_attr = 0;
23982 else
23983 echo_attr = syn_id2attr(id);
23984}
23985
23986/*
23987 * ":execute expr1 ..." execute the result of an expression.
23988 * ":echomsg expr1 ..." Print a message
23989 * ":echoerr expr1 ..." Print an error
23990 * Each gets spaces around each argument and a newline at the end for
23991 * echo commands
23992 */
23993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023994ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995{
23996 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023997 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 int ret = OK;
23999 char_u *p;
24000 garray_T ga;
24001 int len;
24002 int save_did_emsg;
24003
24004 ga_init2(&ga, 1, 80);
24005
24006 if (eap->skip)
24007 ++emsg_skip;
24008 while (*arg != NUL && *arg != '|' && *arg != '\n')
24009 {
24010 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024011 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 {
24013 /*
24014 * Report the invalid expression unless the expression evaluation
24015 * has been cancelled due to an aborting error, an interrupt, or an
24016 * exception.
24017 */
24018 if (!aborting())
24019 EMSG2(_(e_invexpr2), p);
24020 ret = FAIL;
24021 break;
24022 }
24023
24024 if (!eap->skip)
24025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024026 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 len = (int)STRLEN(p);
24028 if (ga_grow(&ga, len + 2) == FAIL)
24029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024030 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 ret = FAIL;
24032 break;
24033 }
24034 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 ga.ga_len += len;
24038 }
24039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024040 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 arg = skipwhite(arg);
24042 }
24043
24044 if (ret != FAIL && ga.ga_data != NULL)
24045 {
24046 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000024047 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000024049 out_flush();
24050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051 else if (eap->cmdidx == CMD_echoerr)
24052 {
24053 /* We don't want to abort following commands, restore did_emsg. */
24054 save_did_emsg = did_emsg;
24055 EMSG((char_u *)ga.ga_data);
24056 if (!force_abort)
24057 did_emsg = save_did_emsg;
24058 }
24059 else if (eap->cmdidx == CMD_execute)
24060 do_cmdline((char_u *)ga.ga_data,
24061 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
24062 }
24063
24064 ga_clear(&ga);
24065
24066 if (eap->skip)
24067 --emsg_skip;
24068
24069 eap->nextcmd = check_nextcmd(arg);
24070}
24071
24072/*
24073 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24074 * "arg" points to the "&" or '+' when called, to "option" when returning.
24075 * Returns NULL when no option name found. Otherwise pointer to the char
24076 * after the option name.
24077 */
24078 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024079find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080{
24081 char_u *p = *arg;
24082
24083 ++p;
24084 if (*p == 'g' && p[1] == ':')
24085 {
24086 *opt_flags = OPT_GLOBAL;
24087 p += 2;
24088 }
24089 else if (*p == 'l' && p[1] == ':')
24090 {
24091 *opt_flags = OPT_LOCAL;
24092 p += 2;
24093 }
24094 else
24095 *opt_flags = 0;
24096
24097 if (!ASCII_ISALPHA(*p))
24098 return NULL;
24099 *arg = p;
24100
24101 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24102 p += 4; /* termcap option */
24103 else
24104 while (ASCII_ISALPHA(*p))
24105 ++p;
24106 return p;
24107}
24108
24109/*
24110 * ":function"
24111 */
24112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024113ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024114{
24115 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024116 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 int j;
24118 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024120 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 char_u *name = NULL;
24122 char_u *p;
24123 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024124 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 garray_T newargs;
24126 garray_T newlines;
24127 int varargs = FALSE;
24128 int mustend = FALSE;
24129 int flags = 0;
24130 ufunc_T *fp;
24131 int indent;
24132 int nesting;
24133 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024134 dictitem_T *v;
24135 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024136 static int func_nr = 0; /* number for nameless function */
24137 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024138 hashtab_T *ht;
24139 int todo;
24140 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024141 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142
24143 /*
24144 * ":function" without argument: list functions.
24145 */
24146 if (ends_excmd(*eap->arg))
24147 {
24148 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024149 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024150 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024151 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024152 {
24153 if (!HASHITEM_EMPTY(hi))
24154 {
24155 --todo;
24156 fp = HI2UF(hi);
24157 if (!isdigit(*fp->uf_name))
24158 list_func_head(fp, FALSE);
24159 }
24160 }
24161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 eap->nextcmd = check_nextcmd(eap->arg);
24163 return;
24164 }
24165
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024166 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024167 * ":function /pat": list functions matching pattern.
24168 */
24169 if (*eap->arg == '/')
24170 {
24171 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24172 if (!eap->skip)
24173 {
24174 regmatch_T regmatch;
24175
24176 c = *p;
24177 *p = NUL;
24178 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24179 *p = c;
24180 if (regmatch.regprog != NULL)
24181 {
24182 regmatch.rm_ic = p_ic;
24183
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024184 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024185 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24186 {
24187 if (!HASHITEM_EMPTY(hi))
24188 {
24189 --todo;
24190 fp = HI2UF(hi);
24191 if (!isdigit(*fp->uf_name)
24192 && vim_regexec(&regmatch, fp->uf_name, 0))
24193 list_func_head(fp, FALSE);
24194 }
24195 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024196 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024197 }
24198 }
24199 if (*p == '/')
24200 ++p;
24201 eap->nextcmd = check_nextcmd(p);
24202 return;
24203 }
24204
24205 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024206 * Get the function name. There are these situations:
24207 * func normal function name
24208 * "name" == func, "fudi.fd_dict" == NULL
24209 * dict.func new dictionary entry
24210 * "name" == NULL, "fudi.fd_dict" set,
24211 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24212 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024213 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024214 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24215 * dict.func existing dict entry that's not a Funcref
24216 * "name" == NULL, "fudi.fd_dict" set,
24217 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024218 * s:func script-local function name
24219 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024222 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024223 paren = (vim_strchr(p, '(') != NULL);
24224 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 {
24226 /*
24227 * Return on an invalid expression in braces, unless the expression
24228 * evaluation has been cancelled due to an aborting error, an
24229 * interrupt, or an exception.
24230 */
24231 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024232 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024233 if (!eap->skip && fudi.fd_newkey != NULL)
24234 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024235 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 else
24239 eap->skip = TRUE;
24240 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024241
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 /* An error in a function call during evaluation of an expression in magic
24243 * braces should not cause the function not to be defined. */
24244 saved_did_emsg = did_emsg;
24245 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246
24247 /*
24248 * ":function func" with only function name: list function.
24249 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024250 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 {
24252 if (!ends_excmd(*skipwhite(p)))
24253 {
24254 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024255 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024256 }
24257 eap->nextcmd = check_nextcmd(p);
24258 if (eap->nextcmd != NULL)
24259 *p = NUL;
24260 if (!eap->skip && !got_int)
24261 {
24262 fp = find_func(name);
24263 if (fp != NULL)
24264 {
24265 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024266 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024268 if (FUNCLINE(fp, j) == NULL)
24269 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270 msg_putchar('\n');
24271 msg_outnum((long)(j + 1));
24272 if (j < 9)
24273 msg_putchar(' ');
24274 if (j < 99)
24275 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024276 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 out_flush(); /* show a line at a time */
24278 ui_breakcheck();
24279 }
24280 if (!got_int)
24281 {
24282 msg_putchar('\n');
24283 msg_puts((char_u *)" endfunction");
24284 }
24285 }
24286 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024287 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024289 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290 }
24291
24292 /*
24293 * ":function name(arg1, arg2)" Define function.
24294 */
24295 p = skipwhite(p);
24296 if (*p != '(')
24297 {
24298 if (!eap->skip)
24299 {
24300 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024301 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 }
24303 /* attempt to continue by skipping some text */
24304 if (vim_strchr(p, '(') != NULL)
24305 p = vim_strchr(p, '(');
24306 }
24307 p = skipwhite(p + 1);
24308
24309 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24310 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24311
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024312 if (!eap->skip)
24313 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024314 /* Check the name of the function. Unless it's a dictionary function
24315 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024316 if (name != NULL)
24317 arg = name;
24318 else
24319 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024320 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024321 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24322 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024323 {
24324 if (*arg == K_SPECIAL)
24325 j = 3;
24326 else
24327 j = 0;
24328 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24329 : eval_isnamec(arg[j])))
24330 ++j;
24331 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024332 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024333 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024334 /* Disallow using the g: dict. */
24335 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24336 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024337 }
24338
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 /*
24340 * Isolate the arguments: "arg1, arg2, ...)"
24341 */
24342 while (*p != ')')
24343 {
24344 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24345 {
24346 varargs = TRUE;
24347 p += 3;
24348 mustend = TRUE;
24349 }
24350 else
24351 {
24352 arg = p;
24353 while (ASCII_ISALNUM(*p) || *p == '_')
24354 ++p;
24355 if (arg == p || isdigit(*arg)
24356 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24357 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24358 {
24359 if (!eap->skip)
24360 EMSG2(_("E125: Illegal argument: %s"), arg);
24361 break;
24362 }
24363 if (ga_grow(&newargs, 1) == FAIL)
24364 goto erret;
24365 c = *p;
24366 *p = NUL;
24367 arg = vim_strsave(arg);
24368 if (arg == NULL)
24369 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024370
24371 /* Check for duplicate argument name. */
24372 for (i = 0; i < newargs.ga_len; ++i)
24373 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24374 {
24375 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024376 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024377 goto erret;
24378 }
24379
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24381 *p = c;
24382 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 if (*p == ',')
24384 ++p;
24385 else
24386 mustend = TRUE;
24387 }
24388 p = skipwhite(p);
24389 if (mustend && *p != ')')
24390 {
24391 if (!eap->skip)
24392 EMSG2(_(e_invarg2), eap->arg);
24393 break;
24394 }
24395 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024396 if (*p != ')')
24397 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398 ++p; /* skip the ')' */
24399
Bram Moolenaare9a41262005-01-15 22:18:47 +000024400 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 for (;;)
24402 {
24403 p = skipwhite(p);
24404 if (STRNCMP(p, "range", 5) == 0)
24405 {
24406 flags |= FC_RANGE;
24407 p += 5;
24408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024409 else if (STRNCMP(p, "dict", 4) == 0)
24410 {
24411 flags |= FC_DICT;
24412 p += 4;
24413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 else if (STRNCMP(p, "abort", 5) == 0)
24415 {
24416 flags |= FC_ABORT;
24417 p += 5;
24418 }
24419 else
24420 break;
24421 }
24422
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024423 /* When there is a line break use what follows for the function body.
24424 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24425 if (*p == '\n')
24426 line_arg = p + 1;
24427 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428 EMSG(_(e_trailing));
24429
24430 /*
24431 * Read the body of the function, until ":endfunction" is found.
24432 */
24433 if (KeyTyped)
24434 {
24435 /* Check if the function already exists, don't let the user type the
24436 * whole function before telling him it doesn't work! For a script we
24437 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024438 if (!eap->skip && !eap->forceit)
24439 {
24440 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24441 EMSG(_(e_funcdict));
24442 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024443 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024446 if (!eap->skip && did_emsg)
24447 goto erret;
24448
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449 msg_putchar('\n'); /* don't overwrite the function name */
24450 cmdline_row = msg_row;
24451 }
24452
24453 indent = 2;
24454 nesting = 0;
24455 for (;;)
24456 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024457 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024458 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024459 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024460 saved_wait_return = FALSE;
24461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024463 sourcing_lnum_off = sourcing_lnum;
24464
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024465 if (line_arg != NULL)
24466 {
24467 /* Use eap->arg, split up in parts by line breaks. */
24468 theline = line_arg;
24469 p = vim_strchr(theline, '\n');
24470 if (p == NULL)
24471 line_arg += STRLEN(line_arg);
24472 else
24473 {
24474 *p = NUL;
24475 line_arg = p + 1;
24476 }
24477 }
24478 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479 theline = getcmdline(':', 0L, indent);
24480 else
24481 theline = eap->getline(':', eap->cookie, indent);
24482 if (KeyTyped)
24483 lines_left = Rows - 1;
24484 if (theline == NULL)
24485 {
24486 EMSG(_("E126: Missing :endfunction"));
24487 goto erret;
24488 }
24489
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024490 /* Detect line continuation: sourcing_lnum increased more than one. */
24491 if (sourcing_lnum > sourcing_lnum_off + 1)
24492 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24493 else
24494 sourcing_lnum_off = 0;
24495
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496 if (skip_until != NULL)
24497 {
24498 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24499 * don't check for ":endfunc". */
24500 if (STRCMP(theline, skip_until) == 0)
24501 {
24502 vim_free(skip_until);
24503 skip_until = NULL;
24504 }
24505 }
24506 else
24507 {
24508 /* skip ':' and blanks*/
24509 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24510 ;
24511
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024512 /* Check for "endfunction". */
24513 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024515 if (line_arg == NULL)
24516 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024517 break;
24518 }
24519
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024520 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521 * at "end". */
24522 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24523 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024524 else if (STRNCMP(p, "if", 2) == 0
24525 || STRNCMP(p, "wh", 2) == 0
24526 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 || STRNCMP(p, "try", 3) == 0)
24528 indent += 2;
24529
24530 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024531 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024532 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024533 if (*p == '!')
24534 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024536 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024537 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024539 ++nesting;
24540 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 }
24542 }
24543
24544 /* Check for ":append" or ":insert". */
24545 p = skip_range(p, NULL);
24546 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24547 || (p[0] == 'i'
24548 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24549 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24550 skip_until = vim_strsave((char_u *)".");
24551
24552 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24553 arg = skipwhite(skiptowhite(p));
24554 if (arg[0] == '<' && arg[1] =='<'
24555 && ((p[0] == 'p' && p[1] == 'y'
24556 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24557 || (p[0] == 'p' && p[1] == 'e'
24558 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24559 || (p[0] == 't' && p[1] == 'c'
24560 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024561 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24562 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24564 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024565 || (p[0] == 'm' && p[1] == 'z'
24566 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 ))
24568 {
24569 /* ":python <<" continues until a dot, like ":append" */
24570 p = skipwhite(arg + 2);
24571 if (*p == NUL)
24572 skip_until = vim_strsave((char_u *)".");
24573 else
24574 skip_until = vim_strsave(p);
24575 }
24576 }
24577
24578 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024579 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024580 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024581 if (line_arg == NULL)
24582 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024584 }
24585
24586 /* Copy the line to newly allocated memory. get_one_sourceline()
24587 * allocates 250 bytes per line, this saves 80% on average. The cost
24588 * is an extra alloc/free. */
24589 p = vim_strsave(theline);
24590 if (p != NULL)
24591 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024592 if (line_arg == NULL)
24593 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024594 theline = p;
24595 }
24596
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024597 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24598
24599 /* Add NULL lines for continuation lines, so that the line count is
24600 * equal to the index in the growarray. */
24601 while (sourcing_lnum_off-- > 0)
24602 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024603
24604 /* Check for end of eap->arg. */
24605 if (line_arg != NULL && *line_arg == NUL)
24606 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 }
24608
24609 /* Don't define the function when skipping commands or when an error was
24610 * detected. */
24611 if (eap->skip || did_emsg)
24612 goto erret;
24613
24614 /*
24615 * If there are no errors, add the function
24616 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024617 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024618 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024619 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024620 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024621 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024622 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024623 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024624 goto erret;
24625 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024626
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024627 fp = find_func(name);
24628 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024630 if (!eap->forceit)
24631 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024632 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024633 goto erret;
24634 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024635 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024636 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024637 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024638 name);
24639 goto erret;
24640 }
24641 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024642 ga_clear_strings(&(fp->uf_args));
24643 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024644 vim_free(name);
24645 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 }
24648 else
24649 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024650 char numbuf[20];
24651
24652 fp = NULL;
24653 if (fudi.fd_newkey == NULL && !eap->forceit)
24654 {
24655 EMSG(_(e_funcdict));
24656 goto erret;
24657 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024658 if (fudi.fd_di == NULL)
24659 {
24660 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024661 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024662 goto erret;
24663 }
24664 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024665 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024666 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024667
24668 /* Give the function a sequential number. Can only be used with a
24669 * Funcref! */
24670 vim_free(name);
24671 sprintf(numbuf, "%d", ++func_nr);
24672 name = vim_strsave((char_u *)numbuf);
24673 if (name == NULL)
24674 goto erret;
24675 }
24676
24677 if (fp == NULL)
24678 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024679 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024680 {
24681 int slen, plen;
24682 char_u *scriptname;
24683
24684 /* Check that the autoload name matches the script name. */
24685 j = FAIL;
24686 if (sourcing_name != NULL)
24687 {
24688 scriptname = autoload_name(name);
24689 if (scriptname != NULL)
24690 {
24691 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024692 plen = (int)STRLEN(p);
24693 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024694 if (slen > plen && fnamecmp(p,
24695 sourcing_name + slen - plen) == 0)
24696 j = OK;
24697 vim_free(scriptname);
24698 }
24699 }
24700 if (j == FAIL)
24701 {
24702 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24703 goto erret;
24704 }
24705 }
24706
24707 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708 if (fp == NULL)
24709 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024710
24711 if (fudi.fd_dict != NULL)
24712 {
24713 if (fudi.fd_di == NULL)
24714 {
24715 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024716 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024717 if (fudi.fd_di == NULL)
24718 {
24719 vim_free(fp);
24720 goto erret;
24721 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024722 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24723 {
24724 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024725 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024726 goto erret;
24727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024728 }
24729 else
24730 /* overwrite existing dict entry */
24731 clear_tv(&fudi.fd_di->di_tv);
24732 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024733 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024734 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024735 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024736
24737 /* behave like "dict" was used */
24738 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024739 }
24740
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024742 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024743 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24744 {
24745 vim_free(fp);
24746 goto erret;
24747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024748 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024749 fp->uf_args = newargs;
24750 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024751#ifdef FEAT_PROFILE
24752 fp->uf_tml_count = NULL;
24753 fp->uf_tml_total = NULL;
24754 fp->uf_tml_self = NULL;
24755 fp->uf_profiling = FALSE;
24756 if (prof_def_func())
24757 func_do_profile(fp);
24758#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024759 fp->uf_varargs = varargs;
24760 fp->uf_flags = flags;
24761 fp->uf_calls = 0;
24762 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024763 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764
24765erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766 ga_clear_strings(&newargs);
24767 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024768ret_free:
24769 vim_free(skip_until);
24770 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024773 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774}
24775
24776/*
24777 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024778 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024780 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024781 * TFN_INT: internal function name OK
24782 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024783 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784 * Advances "pp" to just after the function name (if no error).
24785 */
24786 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024787trans_function_name(
24788 char_u **pp,
24789 int skip, /* only find the end, don't evaluate */
24790 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024791 funcdict_T *fdp, /* return: info about dictionary used */
24792 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024794 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795 char_u *start;
24796 char_u *end;
24797 int lead;
24798 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024799 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024800 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024801
24802 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024803 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024805
24806 /* Check for hard coded <SNR>: already translated function ID (from a user
24807 * command). */
24808 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24809 && (*pp)[2] == (int)KE_SNR)
24810 {
24811 *pp += 3;
24812 len = get_id_len(pp) + 3;
24813 return vim_strnsave(start, len);
24814 }
24815
24816 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24817 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024819 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024821
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024822 /* Note that TFN_ flags use the same values as GLV_ flags. */
24823 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024824 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024825 if (end == start)
24826 {
24827 if (!skip)
24828 EMSG(_("E129: Function name required"));
24829 goto theend;
24830 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024831 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024832 {
24833 /*
24834 * Report an invalid expression in braces, unless the expression
24835 * evaluation has been cancelled due to an aborting error, an
24836 * interrupt, or an exception.
24837 */
24838 if (!aborting())
24839 {
24840 if (end != NULL)
24841 EMSG2(_(e_invarg2), start);
24842 }
24843 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024844 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024845 goto theend;
24846 }
24847
24848 if (lv.ll_tv != NULL)
24849 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024850 if (fdp != NULL)
24851 {
24852 fdp->fd_dict = lv.ll_dict;
24853 fdp->fd_newkey = lv.ll_newkey;
24854 lv.ll_newkey = NULL;
24855 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024856 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024857 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24858 {
24859 name = vim_strsave(lv.ll_tv->vval.v_string);
24860 *pp = end;
24861 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024862 else if (lv.ll_tv->v_type == VAR_PARTIAL
24863 && lv.ll_tv->vval.v_partial != NULL)
24864 {
24865 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24866 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024867 if (partial != NULL)
24868 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024869 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024870 else
24871 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024872 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24873 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024874 EMSG(_(e_funcref));
24875 else
24876 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024877 name = NULL;
24878 }
24879 goto theend;
24880 }
24881
24882 if (lv.ll_name == NULL)
24883 {
24884 /* Error found, but continue after the function name. */
24885 *pp = end;
24886 goto theend;
24887 }
24888
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024889 /* Check if the name is a Funcref. If so, use the value. */
24890 if (lv.ll_exp_name != NULL)
24891 {
24892 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024893 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024894 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024895 if (name == lv.ll_exp_name)
24896 name = NULL;
24897 }
24898 else
24899 {
24900 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024901 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024902 if (name == *pp)
24903 name = NULL;
24904 }
24905 if (name != NULL)
24906 {
24907 name = vim_strsave(name);
24908 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024909 if (STRNCMP(name, "<SNR>", 5) == 0)
24910 {
24911 /* Change "<SNR>" to the byte sequence. */
24912 name[0] = K_SPECIAL;
24913 name[1] = KS_EXTRA;
24914 name[2] = (int)KE_SNR;
24915 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24916 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024917 goto theend;
24918 }
24919
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024920 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024921 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024922 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024923 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24924 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24925 {
24926 /* When there was "s:" already or the name expanded to get a
24927 * leading "s:" then remove it. */
24928 lv.ll_name += 2;
24929 len -= 2;
24930 lead = 2;
24931 }
24932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024933 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024934 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024935 /* skip over "s:" and "g:" */
24936 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024937 lv.ll_name += 2;
24938 len = (int)(end - lv.ll_name);
24939 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024940
24941 /*
24942 * Copy the function name to allocated memory.
24943 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24944 * Accept <SNR>123_name() outside a script.
24945 */
24946 if (skip)
24947 lead = 0; /* do nothing */
24948 else if (lead > 0)
24949 {
24950 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024951 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24952 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024953 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024954 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024955 if (current_SID <= 0)
24956 {
24957 EMSG(_(e_usingsid));
24958 goto theend;
24959 }
24960 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24961 lead += (int)STRLEN(sid_buf);
24962 }
24963 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024964 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024965 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024966 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024967 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024968 goto theend;
24969 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024970 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024971 {
24972 char_u *cp = vim_strchr(lv.ll_name, ':');
24973
24974 if (cp != NULL && cp < end)
24975 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024976 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024977 goto theend;
24978 }
24979 }
24980
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024981 name = alloc((unsigned)(len + lead + 1));
24982 if (name != NULL)
24983 {
24984 if (lead > 0)
24985 {
24986 name[0] = K_SPECIAL;
24987 name[1] = KS_EXTRA;
24988 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024989 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024990 STRCPY(name + 3, sid_buf);
24991 }
24992 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024993 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024994 }
24995 *pp = end;
24996
24997theend:
24998 clear_lval(&lv);
24999 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000}
25001
25002/*
25003 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
25004 * Return 2 if "p" starts with "s:".
25005 * Return 0 otherwise.
25006 */
25007 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025008eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010025010 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
25011 * the standard library function. */
25012 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
25013 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025014 return 5;
25015 if (p[0] == 's' && p[1] == ':')
25016 return 2;
25017 return 0;
25018}
25019
25020/*
25021 * Return TRUE if "p" starts with "<SID>" or "s:".
25022 * Only works if eval_fname_script() returned non-zero for "p"!
25023 */
25024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025025eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026{
25027 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
25028}
25029
25030/*
25031 * List the head of the function: "name(arg1, arg2)".
25032 */
25033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025034list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035{
25036 int j;
25037
25038 msg_start();
25039 if (indent)
25040 MSG_PUTS(" ");
25041 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025042 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 {
25044 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025045 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046 }
25047 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025048 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025050 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051 {
25052 if (j)
25053 MSG_PUTS(", ");
25054 msg_puts(FUNCARG(fp, j));
25055 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025056 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025057 {
25058 if (j)
25059 MSG_PUTS(", ");
25060 MSG_PUTS("...");
25061 }
25062 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020025063 if (fp->uf_flags & FC_ABORT)
25064 MSG_PUTS(" abort");
25065 if (fp->uf_flags & FC_RANGE)
25066 MSG_PUTS(" range");
25067 if (fp->uf_flags & FC_DICT)
25068 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025069 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000025070 if (p_verbose > 0)
25071 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072}
25073
25074/*
25075 * Find a function by name, return pointer to it in ufuncs.
25076 * Return NULL for unknown function.
25077 */
25078 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025079find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025081 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025083 hi = hash_find(&func_hashtab, name);
25084 if (!HASHITEM_EMPTY(hi))
25085 return HI2UF(hi);
25086 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087}
25088
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025089#if defined(EXITFREE) || defined(PROTO)
25090 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025091free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025092{
25093 hashitem_T *hi;
25094
25095 /* Need to start all over every time, because func_free() may change the
25096 * hash table. */
25097 while (func_hashtab.ht_used > 0)
25098 for (hi = func_hashtab.ht_array; ; ++hi)
25099 if (!HASHITEM_EMPTY(hi))
25100 {
25101 func_free(HI2UF(hi));
25102 break;
25103 }
25104}
25105#endif
25106
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025108translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025109{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025110 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025111 return find_internal_func(name) >= 0;
25112 return find_func(name) != NULL;
25113}
25114
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025115/*
25116 * Return TRUE if a function "name" exists.
25117 */
25118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025119function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025120{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025121 char_u *nm = name;
25122 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025123 int n = FALSE;
25124
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025125 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025126 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025127 nm = skipwhite(nm);
25128
25129 /* Only accept "funcname", "funcname ", "funcname (..." and
25130 * "funcname(...", not "funcname!...". */
25131 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025132 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025133 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025134 return n;
25135}
25136
Bram Moolenaara1544c02013-05-30 12:35:52 +020025137 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025138get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025139{
25140 char_u *nm = name;
25141 char_u *p;
25142
Bram Moolenaar65639032016-03-16 21:40:30 +010025143 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025144
25145 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025146 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025147 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025148
Bram Moolenaara1544c02013-05-30 12:35:52 +020025149 vim_free(p);
25150 return NULL;
25151}
25152
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025153/*
25154 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025155 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25156 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025157 */
25158 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025159builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025160{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025161 char_u *p;
25162
25163 if (!ASCII_ISLOWER(name[0]))
25164 return FALSE;
25165 p = vim_strchr(name, AUTOLOAD_CHAR);
25166 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025167}
25168
Bram Moolenaar05159a02005-02-26 23:04:13 +000025169#if defined(FEAT_PROFILE) || defined(PROTO)
25170/*
25171 * Start profiling function "fp".
25172 */
25173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025174func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025175{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025176 int len = fp->uf_lines.ga_len;
25177
25178 if (len == 0)
25179 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025180 fp->uf_tm_count = 0;
25181 profile_zero(&fp->uf_tm_self);
25182 profile_zero(&fp->uf_tm_total);
25183 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025184 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025185 if (fp->uf_tml_total == NULL)
25186 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025187 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025188 if (fp->uf_tml_self == NULL)
25189 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025190 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025191 fp->uf_tml_idx = -1;
25192 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25193 || fp->uf_tml_self == NULL)
25194 return; /* out of memory */
25195
25196 fp->uf_profiling = TRUE;
25197}
25198
25199/*
25200 * Dump the profiling results for all functions in file "fd".
25201 */
25202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025203func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025204{
25205 hashitem_T *hi;
25206 int todo;
25207 ufunc_T *fp;
25208 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025209 ufunc_T **sorttab;
25210 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025211
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025212 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025213 if (todo == 0)
25214 return; /* nothing to dump */
25215
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025216 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025217
Bram Moolenaar05159a02005-02-26 23:04:13 +000025218 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25219 {
25220 if (!HASHITEM_EMPTY(hi))
25221 {
25222 --todo;
25223 fp = HI2UF(hi);
25224 if (fp->uf_profiling)
25225 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025226 if (sorttab != NULL)
25227 sorttab[st_len++] = fp;
25228
Bram Moolenaar05159a02005-02-26 23:04:13 +000025229 if (fp->uf_name[0] == K_SPECIAL)
25230 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25231 else
25232 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25233 if (fp->uf_tm_count == 1)
25234 fprintf(fd, "Called 1 time\n");
25235 else
25236 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25237 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25238 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25239 fprintf(fd, "\n");
25240 fprintf(fd, "count total (s) self (s)\n");
25241
25242 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25243 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025244 if (FUNCLINE(fp, i) == NULL)
25245 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025246 prof_func_line(fd, fp->uf_tml_count[i],
25247 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025248 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25249 }
25250 fprintf(fd, "\n");
25251 }
25252 }
25253 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025254
25255 if (sorttab != NULL && st_len > 0)
25256 {
25257 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25258 prof_total_cmp);
25259 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25260 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25261 prof_self_cmp);
25262 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25263 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025264
25265 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025266}
Bram Moolenaar73830342005-02-28 22:48:19 +000025267
25268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025269prof_sort_list(
25270 FILE *fd,
25271 ufunc_T **sorttab,
25272 int st_len,
25273 char *title,
25274 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025275{
25276 int i;
25277 ufunc_T *fp;
25278
25279 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25280 fprintf(fd, "count total (s) self (s) function\n");
25281 for (i = 0; i < 20 && i < st_len; ++i)
25282 {
25283 fp = sorttab[i];
25284 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25285 prefer_self);
25286 if (fp->uf_name[0] == K_SPECIAL)
25287 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25288 else
25289 fprintf(fd, " %s()\n", fp->uf_name);
25290 }
25291 fprintf(fd, "\n");
25292}
25293
25294/*
25295 * Print the count and times for one function or function line.
25296 */
25297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025298prof_func_line(
25299 FILE *fd,
25300 int count,
25301 proftime_T *total,
25302 proftime_T *self,
25303 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025304{
25305 if (count > 0)
25306 {
25307 fprintf(fd, "%5d ", count);
25308 if (prefer_self && profile_equal(total, self))
25309 fprintf(fd, " ");
25310 else
25311 fprintf(fd, "%s ", profile_msg(total));
25312 if (!prefer_self && profile_equal(total, self))
25313 fprintf(fd, " ");
25314 else
25315 fprintf(fd, "%s ", profile_msg(self));
25316 }
25317 else
25318 fprintf(fd, " ");
25319}
25320
25321/*
25322 * Compare function for total time sorting.
25323 */
25324 static int
25325#ifdef __BORLANDC__
25326_RTLENTRYF
25327#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025328prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025329{
25330 ufunc_T *p1, *p2;
25331
25332 p1 = *(ufunc_T **)s1;
25333 p2 = *(ufunc_T **)s2;
25334 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25335}
25336
25337/*
25338 * Compare function for self time sorting.
25339 */
25340 static int
25341#ifdef __BORLANDC__
25342_RTLENTRYF
25343#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025344prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025345{
25346 ufunc_T *p1, *p2;
25347
25348 p1 = *(ufunc_T **)s1;
25349 p2 = *(ufunc_T **)s2;
25350 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25351}
25352
Bram Moolenaar05159a02005-02-26 23:04:13 +000025353#endif
25354
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025355/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025356 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025357 * Return TRUE if a package was loaded.
25358 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025359 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025360script_autoload(
25361 char_u *name,
25362 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025363{
25364 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025365 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025366 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025367 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025368
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025369 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025370 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025371 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025372 return FALSE;
25373
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025374 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025375
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025376 /* Find the name in the list of previously loaded package names. Skip
25377 * "autoload/", it's always the same. */
25378 for (i = 0; i < ga_loaded.ga_len; ++i)
25379 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25380 break;
25381 if (!reload && i < ga_loaded.ga_len)
25382 ret = FALSE; /* was loaded already */
25383 else
25384 {
25385 /* Remember the name if it wasn't loaded already. */
25386 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25387 {
25388 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25389 tofree = NULL;
25390 }
25391
25392 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025393 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025394 ret = TRUE;
25395 }
25396
25397 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025398 return ret;
25399}
25400
25401/*
25402 * Return the autoload script name for a function or variable name.
25403 * Returns NULL when out of memory.
25404 */
25405 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025406autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025407{
25408 char_u *p;
25409 char_u *scriptname;
25410
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025411 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025412 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25413 if (scriptname == NULL)
25414 return FALSE;
25415 STRCPY(scriptname, "autoload/");
25416 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025417 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025418 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025419 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025420 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025421 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025422}
25423
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25425
25426/*
25427 * Function given to ExpandGeneric() to obtain the list of user defined
25428 * function names.
25429 */
25430 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025431get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025432{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025433 static long_u done;
25434 static hashitem_T *hi;
25435 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025436
25437 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025438 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025439 done = 0;
25440 hi = func_hashtab.ht_array;
25441 }
25442 if (done < func_hashtab.ht_used)
25443 {
25444 if (done++ > 0)
25445 ++hi;
25446 while (HASHITEM_EMPTY(hi))
25447 ++hi;
25448 fp = HI2UF(hi);
25449
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025450 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025451 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025452
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025453 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25454 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455
25456 cat_func_name(IObuff, fp);
25457 if (xp->xp_context != EXPAND_USER_FUNC)
25458 {
25459 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025460 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 STRCAT(IObuff, ")");
25462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 return IObuff;
25464 }
25465 return NULL;
25466}
25467
25468#endif /* FEAT_CMDL_COMPL */
25469
25470/*
25471 * Copy the function name of "fp" to buffer "buf".
25472 * "buf" must be able to hold the function name plus three bytes.
25473 * Takes care of script-local function names.
25474 */
25475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025476cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025478 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025479 {
25480 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025481 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 }
25483 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025484 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485}
25486
25487/*
25488 * ":delfunction {name}"
25489 */
25490 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025491ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025493 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 char_u *p;
25495 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025496 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497
25498 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025499 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025500 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025502 {
25503 if (fudi.fd_dict != NULL && !eap->skip)
25504 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025507 if (!ends_excmd(*skipwhite(p)))
25508 {
25509 vim_free(name);
25510 EMSG(_(e_trailing));
25511 return;
25512 }
25513 eap->nextcmd = check_nextcmd(p);
25514 if (eap->nextcmd != NULL)
25515 *p = NUL;
25516
25517 if (!eap->skip)
25518 fp = find_func(name);
25519 vim_free(name);
25520
25521 if (!eap->skip)
25522 {
25523 if (fp == NULL)
25524 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025525 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526 return;
25527 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025528 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529 {
25530 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25531 return;
25532 }
25533
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025534 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025536 /* Delete the dict item that refers to the function, it will
25537 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025538 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025540 else
25541 func_free(fp);
25542 }
25543}
25544
25545/*
25546 * Free a function and remove it from the list of functions.
25547 */
25548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025549func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025550{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025551 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025552
25553 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025554 ga_clear_strings(&(fp->uf_args));
25555 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025556#ifdef FEAT_PROFILE
25557 vim_free(fp->uf_tml_count);
25558 vim_free(fp->uf_tml_total);
25559 vim_free(fp->uf_tml_self);
25560#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025561
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025562 /* remove the function from the function hashtable */
25563 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25564 if (HASHITEM_EMPTY(hi))
25565 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025566 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025567 hash_remove(&func_hashtab, hi);
25568
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025569 vim_free(fp);
25570}
25571
25572/*
25573 * Unreference a Function: decrement the reference count and free it when it
25574 * becomes zero. Only for numbered functions.
25575 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025577func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025578{
25579 ufunc_T *fp;
25580
25581 if (name != NULL && isdigit(*name))
25582 {
25583 fp = find_func(name);
25584 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025585 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025586#ifdef EXITFREE
25587 if (!entered_free_all_mem)
25588#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025589 EMSG2(_(e_intern2), "func_unref()");
25590 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025591 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025592 {
25593 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025594 * when "uf_calls" becomes zero. */
25595 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025596 func_free(fp);
25597 }
25598 }
25599}
25600
25601/*
25602 * Count a reference to a Function.
25603 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025605func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025606{
25607 ufunc_T *fp;
25608
25609 if (name != NULL && isdigit(*name))
25610 {
25611 fp = find_func(name);
25612 if (fp == NULL)
25613 EMSG2(_(e_intern2), "func_ref()");
25614 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025615 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616 }
25617}
25618
25619/*
25620 * Call a user function.
25621 */
25622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025623call_user_func(
25624 ufunc_T *fp, /* pointer to function */
25625 int argcount, /* nr of args */
25626 typval_T *argvars, /* arguments */
25627 typval_T *rettv, /* return value */
25628 linenr_T firstline, /* first line of range */
25629 linenr_T lastline, /* last line of range */
25630 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025631{
Bram Moolenaar33570922005-01-25 22:26:29 +000025632 char_u *save_sourcing_name;
25633 linenr_T save_sourcing_lnum;
25634 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025635 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025636 int save_did_emsg;
25637 static int depth = 0;
25638 dictitem_T *v;
25639 int fixvar_idx = 0; /* index in fixvar[] */
25640 int i;
25641 int ai;
25642 char_u numbuf[NUMBUFLEN];
25643 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025644 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025645#ifdef FEAT_PROFILE
25646 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025647 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025649
25650 /* If depth of calling is getting too high, don't execute the function */
25651 if (depth >= p_mfd)
25652 {
25653 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025654 rettv->v_type = VAR_NUMBER;
25655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025656 return;
25657 }
25658 ++depth;
25659
25660 line_breakcheck(); /* check for CTRL-C hit */
25661
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025662 fc = (funccall_T *)alloc(sizeof(funccall_T));
25663 fc->caller = current_funccal;
25664 current_funccal = fc;
25665 fc->func = fp;
25666 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025667 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025668 fc->linenr = 0;
25669 fc->returned = FALSE;
25670 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025672 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25673 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674
Bram Moolenaar33570922005-01-25 22:26:29 +000025675 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025676 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025677 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25678 * each argument variable and saves a lot of time.
25679 */
25680 /*
25681 * Init l: variables.
25682 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025683 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025684 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025685 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025686 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25687 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025688 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025689 name = v->di_key;
25690 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025691 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025692 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025693 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025694 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025695 v->di_tv.vval.v_dict = selfdict;
25696 ++selfdict->dv_refcount;
25697 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025698
Bram Moolenaar33570922005-01-25 22:26:29 +000025699 /*
25700 * Init a: variables.
25701 * Set a:0 to "argcount".
25702 * Set a:000 to a list with room for the "..." arguments.
25703 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025704 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025705 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025706 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025707 /* Use "name" to avoid a warning from some compiler that checks the
25708 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025709 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025710 name = v->di_key;
25711 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025712 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025713 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025714 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025715 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025716 v->di_tv.vval.v_list = &fc->l_varlist;
25717 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25718 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25719 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025720
25721 /*
25722 * Set a:firstline to "firstline" and a:lastline to "lastline".
25723 * Set a:name to named arguments.
25724 * Set a:N to the "..." arguments.
25725 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025726 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025727 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025728 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025729 (varnumber_T)lastline);
25730 for (i = 0; i < argcount; ++i)
25731 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025732 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025733 if (ai < 0)
25734 /* named argument a:name */
25735 name = FUNCARG(fp, i);
25736 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025737 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025738 /* "..." argument a:1, a:2, etc. */
25739 sprintf((char *)numbuf, "%d", ai + 1);
25740 name = numbuf;
25741 }
25742 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25743 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025744 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025745 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25746 }
25747 else
25748 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025749 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25750 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025751 if (v == NULL)
25752 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025753 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025754 }
25755 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025756 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025757
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025758 /* Note: the values are copied directly to avoid alloc/free.
25759 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025760 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025761 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025762
25763 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25764 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025765 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25766 fc->l_listitems[ai].li_tv = argvars[i];
25767 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025768 }
25769 }
25770
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771 /* Don't redraw while executing the function. */
25772 ++RedrawingDisabled;
25773 save_sourcing_name = sourcing_name;
25774 save_sourcing_lnum = sourcing_lnum;
25775 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025776 /* need space for function name + ("function " + 3) or "[number]" */
25777 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25778 + STRLEN(fp->uf_name) + 20;
25779 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 if (sourcing_name != NULL)
25781 {
25782 if (save_sourcing_name != NULL
25783 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025784 sprintf((char *)sourcing_name, "%s[%d]..",
25785 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 else
25787 STRCPY(sourcing_name, "function ");
25788 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25789
25790 if (p_verbose >= 12)
25791 {
25792 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025793 verbose_enter_scroll();
25794
Bram Moolenaar555b2802005-05-19 21:08:39 +000025795 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796 if (p_verbose >= 14)
25797 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025798 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025799 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025800 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025801 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802
25803 msg_puts((char_u *)"(");
25804 for (i = 0; i < argcount; ++i)
25805 {
25806 if (i > 0)
25807 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025808 if (argvars[i].v_type == VAR_NUMBER)
25809 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810 else
25811 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025812 /* Do not want errors such as E724 here. */
25813 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025814 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025815 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025816 if (s != NULL)
25817 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025818 if (vim_strsize(s) > MSG_BUF_CLEN)
25819 {
25820 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25821 s = buf;
25822 }
25823 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025824 vim_free(tofree);
25825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826 }
25827 }
25828 msg_puts((char_u *)")");
25829 }
25830 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025831
25832 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025833 --no_wait_return;
25834 }
25835 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025836#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025837 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025838 {
25839 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25840 func_do_profile(fp);
25841 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025842 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025843 {
25844 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025845 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025846 profile_zero(&fp->uf_tm_children);
25847 }
25848 script_prof_save(&wait_start);
25849 }
25850#endif
25851
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025853 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854 save_did_emsg = did_emsg;
25855 did_emsg = FALSE;
25856
25857 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025858 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025859 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25860
25861 --RedrawingDisabled;
25862
25863 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025864 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025866 clear_tv(rettv);
25867 rettv->v_type = VAR_NUMBER;
25868 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025869 }
25870
Bram Moolenaar05159a02005-02-26 23:04:13 +000025871#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025872 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025873 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025874 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025875 profile_end(&call_start);
25876 profile_sub_wait(&wait_start, &call_start);
25877 profile_add(&fp->uf_tm_total, &call_start);
25878 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025879 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025880 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025881 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25882 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025883 }
25884 }
25885#endif
25886
Bram Moolenaar071d4272004-06-13 20:20:40 +000025887 /* when being verbose, mention the return value */
25888 if (p_verbose >= 12)
25889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025890 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025891 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025894 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025895 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025896 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025897 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025899 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025900 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025901 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025902 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025903 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025904
Bram Moolenaar555b2802005-05-19 21:08:39 +000025905 /* The value may be very long. Skip the middle part, so that we
25906 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025907 * truncate it at the end. Don't want errors such as E724 here. */
25908 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025909 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025910 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025911 if (s != NULL)
25912 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025913 if (vim_strsize(s) > MSG_BUF_CLEN)
25914 {
25915 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25916 s = buf;
25917 }
25918 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025919 vim_free(tofree);
25920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921 }
25922 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025923
25924 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025925 --no_wait_return;
25926 }
25927
25928 vim_free(sourcing_name);
25929 sourcing_name = save_sourcing_name;
25930 sourcing_lnum = save_sourcing_lnum;
25931 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025932#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025933 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025934 script_prof_restore(&wait_start);
25935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936
25937 if (p_verbose >= 12 && sourcing_name != NULL)
25938 {
25939 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025940 verbose_enter_scroll();
25941
Bram Moolenaar555b2802005-05-19 21:08:39 +000025942 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025943 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025944
25945 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946 --no_wait_return;
25947 }
25948
25949 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025950 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025951 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025952
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025953 /* If the a:000 list and the l: and a: dicts are not referenced we can
25954 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025955 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25956 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25957 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25958 {
25959 free_funccal(fc, FALSE);
25960 }
25961 else
25962 {
25963 hashitem_T *hi;
25964 listitem_T *li;
25965 int todo;
25966
25967 /* "fc" is still in use. This can happen when returning "a:000" or
25968 * assigning "l:" to a global variable.
25969 * Link "fc" in the list for garbage collection later. */
25970 fc->caller = previous_funccal;
25971 previous_funccal = fc;
25972
25973 /* Make a copy of the a: variables, since we didn't do that above. */
25974 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25975 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25976 {
25977 if (!HASHITEM_EMPTY(hi))
25978 {
25979 --todo;
25980 v = HI2DI(hi);
25981 copy_tv(&v->di_tv, &v->di_tv);
25982 }
25983 }
25984
25985 /* Make a copy of the a:000 items, since we didn't do that above. */
25986 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25987 copy_tv(&li->li_tv, &li->li_tv);
25988 }
25989}
25990
25991/*
25992 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025993 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025994 */
25995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025996can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025997{
25998 return (fc->l_varlist.lv_copyID != copyID
25999 && fc->l_vars.dv_copyID != copyID
26000 && fc->l_avars.dv_copyID != copyID);
26001}
26002
26003/*
26004 * Free "fc" and what it contains.
26005 */
26006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026007free_funccal(
26008 funccall_T *fc,
26009 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026010{
26011 listitem_T *li;
26012
26013 /* The a: variables typevals may not have been allocated, only free the
26014 * allocated variables. */
26015 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
26016
26017 /* free all l: variables */
26018 vars_clear(&fc->l_vars.dv_hashtab);
26019
26020 /* Free the a:000 variables if they were allocated. */
26021 if (free_val)
26022 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26023 clear_tv(&li->li_tv);
26024
26025 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026}
26027
26028/*
Bram Moolenaar33570922005-01-25 22:26:29 +000026029 * Add a number variable "name" to dict "dp" with value "nr".
26030 */
26031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026032add_nr_var(
26033 dict_T *dp,
26034 dictitem_T *v,
26035 char *name,
26036 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000026037{
26038 STRCPY(v->di_key, name);
26039 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
26040 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
26041 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000026042 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000026043 v->di_tv.vval.v_number = nr;
26044}
26045
26046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000026047 * ":return [expr]"
26048 */
26049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026050ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051{
26052 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000026053 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026054 int returning = FALSE;
26055
26056 if (current_funccal == NULL)
26057 {
26058 EMSG(_("E133: :return not inside a function"));
26059 return;
26060 }
26061
26062 if (eap->skip)
26063 ++emsg_skip;
26064
26065 eap->nextcmd = NULL;
26066 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026067 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068 {
26069 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026070 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026072 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026073 }
26074 /* It's safer to return also on error. */
26075 else if (!eap->skip)
26076 {
26077 /*
26078 * Return unless the expression evaluation has been cancelled due to an
26079 * aborting error, an interrupt, or an exception.
26080 */
26081 if (!aborting())
26082 returning = do_return(eap, FALSE, TRUE, NULL);
26083 }
26084
26085 /* When skipping or the return gets pending, advance to the next command
26086 * in this line (!returning). Otherwise, ignore the rest of the line.
26087 * Following lines will be ignored by get_func_line(). */
26088 if (returning)
26089 eap->nextcmd = NULL;
26090 else if (eap->nextcmd == NULL) /* no argument */
26091 eap->nextcmd = check_nextcmd(arg);
26092
26093 if (eap->skip)
26094 --emsg_skip;
26095}
26096
26097/*
26098 * Return from a function. Possibly makes the return pending. Also called
26099 * for a pending return at the ":endtry" or after returning from an extra
26100 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026101 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026102 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 * FALSE when the return gets pending.
26104 */
26105 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026106do_return(
26107 exarg_T *eap,
26108 int reanimate,
26109 int is_cmd,
26110 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111{
26112 int idx;
26113 struct condstack *cstack = eap->cstack;
26114
26115 if (reanimate)
26116 /* Undo the return. */
26117 current_funccal->returned = FALSE;
26118
26119 /*
26120 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26121 * not in its finally clause (which then is to be executed next) is found.
26122 * In this case, make the ":return" pending for execution at the ":endtry".
26123 * Otherwise, return normally.
26124 */
26125 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26126 if (idx >= 0)
26127 {
26128 cstack->cs_pending[idx] = CSTP_RETURN;
26129
26130 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026131 /* A pending return again gets pending. "rettv" points to an
26132 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026133 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026134 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135 else
26136 {
26137 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026138 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026139 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026140 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026142 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026143 {
26144 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026145 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026146 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147 else
26148 EMSG(_(e_outofmem));
26149 }
26150 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026151 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026152
26153 if (reanimate)
26154 {
26155 /* The pending return value could be overwritten by a ":return"
26156 * without argument in a finally clause; reset the default
26157 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026158 current_funccal->rettv->v_type = VAR_NUMBER;
26159 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 }
26161 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026162 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026163 }
26164 else
26165 {
26166 current_funccal->returned = TRUE;
26167
26168 /* If the return is carried out now, store the return value. For
26169 * a return immediately after reanimation, the value is already
26170 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026171 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026173 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026174 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026175 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026176 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026177 }
26178 }
26179
26180 return idx < 0;
26181}
26182
26183/*
26184 * Free the variable with a pending return value.
26185 */
26186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026187discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026188{
Bram Moolenaar33570922005-01-25 22:26:29 +000026189 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190}
26191
26192/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026193 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026194 * is an allocated string. Used by report_pending() for verbose messages.
26195 */
26196 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026197get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026199 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026200 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026201 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026202
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026203 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026204 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026205 if (s == NULL)
26206 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026207
26208 STRCPY(IObuff, ":return ");
26209 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26210 if (STRLEN(s) + 8 >= IOSIZE)
26211 STRCPY(IObuff + IOSIZE - 4, "...");
26212 vim_free(tofree);
26213 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026214}
26215
26216/*
26217 * Get next function line.
26218 * Called by do_cmdline() to get the next line.
26219 * Returns allocated string, or NULL for end of function.
26220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026221 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026222get_func_line(
26223 int c UNUSED,
26224 void *cookie,
26225 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026226{
Bram Moolenaar33570922005-01-25 22:26:29 +000026227 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026228 ufunc_T *fp = fcp->func;
26229 char_u *retval;
26230 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026231
26232 /* If breakpoints have been added/deleted need to check for it. */
26233 if (fcp->dbg_tick != debug_tick)
26234 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026235 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026236 sourcing_lnum);
26237 fcp->dbg_tick = debug_tick;
26238 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026239#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026240 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026241 func_line_end(cookie);
26242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026243
Bram Moolenaar05159a02005-02-26 23:04:13 +000026244 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026245 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26246 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026247 retval = NULL;
26248 else
26249 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026250 /* Skip NULL lines (continuation lines). */
26251 while (fcp->linenr < gap->ga_len
26252 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26253 ++fcp->linenr;
26254 if (fcp->linenr >= gap->ga_len)
26255 retval = NULL;
26256 else
26257 {
26258 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26259 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026260#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026261 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026262 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026263#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026265 }
26266
26267 /* Did we encounter a breakpoint? */
26268 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26269 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026270 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026271 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026272 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026273 sourcing_lnum);
26274 fcp->dbg_tick = debug_tick;
26275 }
26276
26277 return retval;
26278}
26279
Bram Moolenaar05159a02005-02-26 23:04:13 +000026280#if defined(FEAT_PROFILE) || defined(PROTO)
26281/*
26282 * Called when starting to read a function line.
26283 * "sourcing_lnum" must be correct!
26284 * When skipping lines it may not actually be executed, but we won't find out
26285 * until later and we need to store the time now.
26286 */
26287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026288func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026289{
26290 funccall_T *fcp = (funccall_T *)cookie;
26291 ufunc_T *fp = fcp->func;
26292
26293 if (fp->uf_profiling && sourcing_lnum >= 1
26294 && sourcing_lnum <= fp->uf_lines.ga_len)
26295 {
26296 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026297 /* Skip continuation lines. */
26298 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26299 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026300 fp->uf_tml_execed = FALSE;
26301 profile_start(&fp->uf_tml_start);
26302 profile_zero(&fp->uf_tml_children);
26303 profile_get_wait(&fp->uf_tml_wait);
26304 }
26305}
26306
26307/*
26308 * Called when actually executing a function line.
26309 */
26310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026311func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026312{
26313 funccall_T *fcp = (funccall_T *)cookie;
26314 ufunc_T *fp = fcp->func;
26315
26316 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26317 fp->uf_tml_execed = TRUE;
26318}
26319
26320/*
26321 * Called when done with a function line.
26322 */
26323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026324func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026325{
26326 funccall_T *fcp = (funccall_T *)cookie;
26327 ufunc_T *fp = fcp->func;
26328
26329 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26330 {
26331 if (fp->uf_tml_execed)
26332 {
26333 ++fp->uf_tml_count[fp->uf_tml_idx];
26334 profile_end(&fp->uf_tml_start);
26335 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026336 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026337 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26338 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026339 }
26340 fp->uf_tml_idx = -1;
26341 }
26342}
26343#endif
26344
Bram Moolenaar071d4272004-06-13 20:20:40 +000026345/*
26346 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026347 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026348 */
26349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026350func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026351{
Bram Moolenaar33570922005-01-25 22:26:29 +000026352 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353
26354 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26355 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026356 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026357 || fcp->returned);
26358}
26359
26360/*
26361 * return TRUE if cookie indicates a function which "abort"s on errors.
26362 */
26363 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026364func_has_abort(
26365 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026366{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026367 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026368}
26369
26370#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26371typedef enum
26372{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026373 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26374 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26375 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026376} var_flavour_T;
26377
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026378static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026379
26380 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026381var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382{
26383 char_u *p = varname;
26384
26385 if (ASCII_ISUPPER(*p))
26386 {
26387 while (*(++p))
26388 if (ASCII_ISLOWER(*p))
26389 return VAR_FLAVOUR_SESSION;
26390 return VAR_FLAVOUR_VIMINFO;
26391 }
26392 else
26393 return VAR_FLAVOUR_DEFAULT;
26394}
26395#endif
26396
26397#if defined(FEAT_VIMINFO) || defined(PROTO)
26398/*
26399 * Restore global vars that start with a capital from the viminfo file
26400 */
26401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026402read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026403{
26404 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026405 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026406 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026407 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026408
26409 if (!writing && (find_viminfo_parameter('!') != NULL))
26410 {
26411 tab = vim_strchr(virp->vir_line + 1, '\t');
26412 if (tab != NULL)
26413 {
26414 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026415 switch (*tab)
26416 {
26417 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026418#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026419 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026420#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026421 case 'D': type = VAR_DICT; break;
26422 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026423 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026425
26426 tab = vim_strchr(tab, '\t');
26427 if (tab != NULL)
26428 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026429 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026430 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026431 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026432 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026433#ifdef FEAT_FLOAT
26434 else if (type == VAR_FLOAT)
26435 (void)string2float(tab + 1, &tv.vval.v_float);
26436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026437 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026438 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026439 if (type == VAR_DICT || type == VAR_LIST)
26440 {
26441 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26442
26443 if (etv == NULL)
26444 /* Failed to parse back the dict or list, use it as a
26445 * string. */
26446 tv.v_type = VAR_STRING;
26447 else
26448 {
26449 vim_free(tv.vval.v_string);
26450 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026451 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026452 }
26453 }
26454
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026455 /* when in a function use global variables */
26456 save_funccal = current_funccal;
26457 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026458 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026459 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026460
26461 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026462 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026463 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26464 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026465 }
26466 }
26467 }
26468
26469 return viminfo_readline(virp);
26470}
26471
26472/*
26473 * Write global vars that start with a capital to the viminfo file
26474 */
26475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026476write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026477{
Bram Moolenaar33570922005-01-25 22:26:29 +000026478 hashitem_T *hi;
26479 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026480 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026481 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026482 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026483 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026484 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026485
26486 if (find_viminfo_parameter('!') == NULL)
26487 return;
26488
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026489 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026490
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026491 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026492 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026493 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026494 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026495 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026496 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026497 this_var = HI2DI(hi);
26498 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026499 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026500 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026501 {
26502 case VAR_STRING: s = "STR"; break;
26503 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026504 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026505 case VAR_DICT: s = "DIC"; break;
26506 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026507 case VAR_SPECIAL: s = "XPL"; break;
26508
26509 case VAR_UNKNOWN:
26510 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026511 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026512 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026513 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026514 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026515 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026516 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026517 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026518 if (p != NULL)
26519 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026520 vim_free(tofree);
26521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026522 }
26523 }
26524}
26525#endif
26526
26527#if defined(FEAT_SESSION) || defined(PROTO)
26528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026529store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026530{
Bram Moolenaar33570922005-01-25 22:26:29 +000026531 hashitem_T *hi;
26532 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026533 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026534 char_u *p, *t;
26535
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026536 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026537 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026538 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026539 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026540 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026541 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026542 this_var = HI2DI(hi);
26543 if ((this_var->di_tv.v_type == VAR_NUMBER
26544 || this_var->di_tv.v_type == VAR_STRING)
26545 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026546 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026547 /* Escape special characters with a backslash. Turn a LF and
26548 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026549 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026550 (char_u *)"\\\"\n\r");
26551 if (p == NULL) /* out of memory */
26552 break;
26553 for (t = p; *t != NUL; ++t)
26554 if (*t == '\n')
26555 *t = 'n';
26556 else if (*t == '\r')
26557 *t = 'r';
26558 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026559 this_var->di_key,
26560 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26561 : ' ',
26562 p,
26563 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26564 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026565 || put_eol(fd) == FAIL)
26566 {
26567 vim_free(p);
26568 return FAIL;
26569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026570 vim_free(p);
26571 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026572#ifdef FEAT_FLOAT
26573 else if (this_var->di_tv.v_type == VAR_FLOAT
26574 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26575 {
26576 float_T f = this_var->di_tv.vval.v_float;
26577 int sign = ' ';
26578
26579 if (f < 0)
26580 {
26581 f = -f;
26582 sign = '-';
26583 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026584 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026585 this_var->di_key, sign, f) < 0)
26586 || put_eol(fd) == FAIL)
26587 return FAIL;
26588 }
26589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026590 }
26591 }
26592 return OK;
26593}
26594#endif
26595
Bram Moolenaar661b1822005-07-28 22:36:45 +000026596/*
26597 * Display script name where an item was last set.
26598 * Should only be invoked when 'verbose' is non-zero.
26599 */
26600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026601last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026602{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026603 char_u *p;
26604
Bram Moolenaar661b1822005-07-28 22:36:45 +000026605 if (scriptID != 0)
26606 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026607 p = home_replace_save(NULL, get_scriptname(scriptID));
26608 if (p != NULL)
26609 {
26610 verbose_enter();
26611 MSG_PUTS(_("\n\tLast set from "));
26612 MSG_PUTS(p);
26613 vim_free(p);
26614 verbose_leave();
26615 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026616 }
26617}
26618
Bram Moolenaard812df62008-11-09 12:46:09 +000026619/*
26620 * List v:oldfiles in a nice way.
26621 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026623ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026624{
26625 list_T *l = vimvars[VV_OLDFILES].vv_list;
26626 listitem_T *li;
26627 int nr = 0;
26628
26629 if (l == NULL)
26630 msg((char_u *)_("No old files"));
26631 else
26632 {
26633 msg_start();
26634 msg_scroll = TRUE;
26635 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26636 {
26637 msg_outnum((long)++nr);
26638 MSG_PUTS(": ");
26639 msg_outtrans(get_tv_string(&li->li_tv));
26640 msg_putchar('\n');
26641 out_flush(); /* output one line at a time */
26642 ui_breakcheck();
26643 }
26644 /* Assume "got_int" was set to truncate the listing. */
26645 got_int = FALSE;
26646
26647#ifdef FEAT_BROWSE_CMD
26648 if (cmdmod.browse)
26649 {
26650 quit_more = FALSE;
26651 nr = prompt_for_number(FALSE);
26652 msg_starthere();
26653 if (nr > 0)
26654 {
26655 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26656 (long)nr);
26657
26658 if (p != NULL)
26659 {
26660 p = expand_env_save(p);
26661 eap->arg = p;
26662 eap->cmdidx = CMD_edit;
26663 cmdmod.browse = FALSE;
26664 do_exedit(eap, NULL);
26665 vim_free(p);
26666 }
26667 }
26668 }
26669#endif
26670 }
26671}
26672
Bram Moolenaar53744302015-07-17 17:38:22 +020026673/* reset v:option_new, v:option_old and v:option_type */
26674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026675reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026676{
26677 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26678 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26679 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26680}
26681
26682
Bram Moolenaar071d4272004-06-13 20:20:40 +000026683#endif /* FEAT_EVAL */
26684
Bram Moolenaar071d4272004-06-13 20:20:40 +000026685
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026686#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026687
26688#ifdef WIN3264
26689/*
26690 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26691 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026692static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26693static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26694static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026695
26696/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026697 * Get the short path (8.3) for the filename in "fnamep".
26698 * Only works for a valid file name.
26699 * When the path gets longer "fnamep" is changed and the allocated buffer
26700 * is put in "bufp".
26701 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26702 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026703 */
26704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026705get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026706{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026707 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026708 char_u *newbuf;
26709
26710 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026711 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026712 if (l > len - 1)
26713 {
26714 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026715 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026716 newbuf = vim_strnsave(*fnamep, l);
26717 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026718 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026719
26720 vim_free(*bufp);
26721 *fnamep = *bufp = newbuf;
26722
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026723 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026724 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026725 }
26726
26727 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026728 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026729}
26730
26731/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026732 * Get the short path (8.3) for the filename in "fname". The converted
26733 * path is returned in "bufp".
26734 *
26735 * Some of the directories specified in "fname" may not exist. This function
26736 * will shorten the existing directories at the beginning of the path and then
26737 * append the remaining non-existing path.
26738 *
26739 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026740 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026741 * bufp - Pointer to an allocated buffer for the filename.
26742 * fnamelen - Length of the filename pointed to by fname
26743 *
26744 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026745 */
26746 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026747shortpath_for_invalid_fname(
26748 char_u **fname,
26749 char_u **bufp,
26750 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026751{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026752 char_u *short_fname, *save_fname, *pbuf_unused;
26753 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026754 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026755 int old_len, len;
26756 int new_len, sfx_len;
26757 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026758
26759 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026760 old_len = *fnamelen;
26761 save_fname = vim_strnsave(*fname, old_len);
26762 pbuf_unused = NULL;
26763 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026764
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026765 endp = save_fname + old_len - 1; /* Find the end of the copy */
26766 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026767
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026768 /*
26769 * Try shortening the supplied path till it succeeds by removing one
26770 * directory at a time from the tail of the path.
26771 */
26772 len = 0;
26773 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026774 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026775 /* go back one path-separator */
26776 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26777 --endp;
26778 if (endp <= save_fname)
26779 break; /* processed the complete path */
26780
26781 /*
26782 * Replace the path separator with a NUL and try to shorten the
26783 * resulting path.
26784 */
26785 ch = *endp;
26786 *endp = 0;
26787 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026788 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026789 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26790 {
26791 retval = FAIL;
26792 goto theend;
26793 }
26794 *endp = ch; /* preserve the string */
26795
26796 if (len > 0)
26797 break; /* successfully shortened the path */
26798
26799 /* failed to shorten the path. Skip the path separator */
26800 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026801 }
26802
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026803 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026804 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026805 /*
26806 * Succeeded in shortening the path. Now concatenate the shortened
26807 * path with the remaining path at the tail.
26808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026809
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026810 /* Compute the length of the new path. */
26811 sfx_len = (int)(save_endp - endp) + 1;
26812 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026813
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026814 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026815 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026816 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026817 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026818 /* There is not enough space in the currently allocated string,
26819 * copy it to a buffer big enough. */
26820 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026821 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026822 {
26823 retval = FAIL;
26824 goto theend;
26825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026826 }
26827 else
26828 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026829 /* Transfer short_fname to the main buffer (it's big enough),
26830 * unless get_short_pathname() did its work in-place. */
26831 *fname = *bufp = save_fname;
26832 if (short_fname != save_fname)
26833 vim_strncpy(save_fname, short_fname, len);
26834 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026835 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026836
26837 /* concat the not-shortened part of the path */
26838 vim_strncpy(*fname + len, endp, sfx_len);
26839 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026840 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026841
26842theend:
26843 vim_free(pbuf_unused);
26844 vim_free(save_fname);
26845
26846 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026847}
26848
26849/*
26850 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026851 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026852 */
26853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026854shortpath_for_partial(
26855 char_u **fnamep,
26856 char_u **bufp,
26857 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026858{
26859 int sepcount, len, tflen;
26860 char_u *p;
26861 char_u *pbuf, *tfname;
26862 int hasTilde;
26863
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026864 /* Count up the path separators from the RHS.. so we know which part
26865 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026866 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026867 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026868 if (vim_ispathsep(*p))
26869 ++sepcount;
26870
26871 /* Need full path first (use expand_env() to remove a "~/") */
26872 hasTilde = (**fnamep == '~');
26873 if (hasTilde)
26874 pbuf = tfname = expand_env_save(*fnamep);
26875 else
26876 pbuf = tfname = FullName_save(*fnamep, FALSE);
26877
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026878 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026879
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026880 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26881 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026882
26883 if (len == 0)
26884 {
26885 /* Don't have a valid filename, so shorten the rest of the
26886 * path if we can. This CAN give us invalid 8.3 filenames, but
26887 * there's not a lot of point in guessing what it might be.
26888 */
26889 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026890 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26891 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026892 }
26893
26894 /* Count the paths backward to find the beginning of the desired string. */
26895 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026896 {
26897#ifdef FEAT_MBYTE
26898 if (has_mbyte)
26899 p -= mb_head_off(tfname, p);
26900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026901 if (vim_ispathsep(*p))
26902 {
26903 if (sepcount == 0 || (hasTilde && sepcount == 1))
26904 break;
26905 else
26906 sepcount --;
26907 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026909 if (hasTilde)
26910 {
26911 --p;
26912 if (p >= tfname)
26913 *p = '~';
26914 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026916 }
26917 else
26918 ++p;
26919
26920 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26921 vim_free(*bufp);
26922 *fnamelen = (int)STRLEN(p);
26923 *bufp = pbuf;
26924 *fnamep = p;
26925
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026926 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026927}
26928#endif /* WIN3264 */
26929
26930/*
26931 * Adjust a filename, according to a string of modifiers.
26932 * *fnamep must be NUL terminated when called. When returning, the length is
26933 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026934 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026935 * When there is an error, *fnamep is set to NULL.
26936 */
26937 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026938modify_fname(
26939 char_u *src, /* string with modifiers */
26940 int *usedlen, /* characters after src that are used */
26941 char_u **fnamep, /* file name so far */
26942 char_u **bufp, /* buffer for allocated file name or NULL */
26943 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026944{
26945 int valid = 0;
26946 char_u *tail;
26947 char_u *s, *p, *pbuf;
26948 char_u dirname[MAXPATHL];
26949 int c;
26950 int has_fullname = 0;
26951#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026952 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026953 int has_shortname = 0;
26954#endif
26955
26956repeat:
26957 /* ":p" - full path/file_name */
26958 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26959 {
26960 has_fullname = 1;
26961
26962 valid |= VALID_PATH;
26963 *usedlen += 2;
26964
26965 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26966 if ((*fnamep)[0] == '~'
26967#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26968 && ((*fnamep)[1] == '/'
26969# ifdef BACKSLASH_IN_FILENAME
26970 || (*fnamep)[1] == '\\'
26971# endif
26972 || (*fnamep)[1] == NUL)
26973
26974#endif
26975 )
26976 {
26977 *fnamep = expand_env_save(*fnamep);
26978 vim_free(*bufp); /* free any allocated file name */
26979 *bufp = *fnamep;
26980 if (*fnamep == NULL)
26981 return -1;
26982 }
26983
26984 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026985 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026986 {
26987 if (vim_ispathsep(*p)
26988 && p[1] == '.'
26989 && (p[2] == NUL
26990 || vim_ispathsep(p[2])
26991 || (p[2] == '.'
26992 && (p[3] == NUL || vim_ispathsep(p[3])))))
26993 break;
26994 }
26995
26996 /* FullName_save() is slow, don't use it when not needed. */
26997 if (*p != NUL || !vim_isAbsName(*fnamep))
26998 {
26999 *fnamep = FullName_save(*fnamep, *p != NUL);
27000 vim_free(*bufp); /* free any allocated file name */
27001 *bufp = *fnamep;
27002 if (*fnamep == NULL)
27003 return -1;
27004 }
27005
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027006#ifdef WIN3264
27007# if _WIN32_WINNT >= 0x0500
27008 if (vim_strchr(*fnamep, '~') != NULL)
27009 {
27010 /* Expand 8.3 filename to full path. Needed to make sure the same
27011 * file does not have two different names.
27012 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
27013 p = alloc(_MAX_PATH + 1);
27014 if (p != NULL)
27015 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010027016 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027017 {
27018 vim_free(*bufp);
27019 *bufp = *fnamep = p;
27020 }
27021 else
27022 vim_free(p);
27023 }
27024 }
27025# endif
27026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027027 /* Append a path separator to a directory. */
27028 if (mch_isdir(*fnamep))
27029 {
27030 /* Make room for one or two extra characters. */
27031 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
27032 vim_free(*bufp); /* free any allocated file name */
27033 *bufp = *fnamep;
27034 if (*fnamep == NULL)
27035 return -1;
27036 add_pathsep(*fnamep);
27037 }
27038 }
27039
27040 /* ":." - path relative to the current directory */
27041 /* ":~" - path relative to the home directory */
27042 /* ":8" - shortname path - postponed till after */
27043 while (src[*usedlen] == ':'
27044 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
27045 {
27046 *usedlen += 2;
27047 if (c == '8')
27048 {
27049#ifdef WIN3264
27050 has_shortname = 1; /* Postpone this. */
27051#endif
27052 continue;
27053 }
27054 pbuf = NULL;
27055 /* Need full path first (use expand_env() to remove a "~/") */
27056 if (!has_fullname)
27057 {
27058 if (c == '.' && **fnamep == '~')
27059 p = pbuf = expand_env_save(*fnamep);
27060 else
27061 p = pbuf = FullName_save(*fnamep, FALSE);
27062 }
27063 else
27064 p = *fnamep;
27065
27066 has_fullname = 0;
27067
27068 if (p != NULL)
27069 {
27070 if (c == '.')
27071 {
27072 mch_dirname(dirname, MAXPATHL);
27073 s = shorten_fname(p, dirname);
27074 if (s != NULL)
27075 {
27076 *fnamep = s;
27077 if (pbuf != NULL)
27078 {
27079 vim_free(*bufp); /* free any allocated file name */
27080 *bufp = pbuf;
27081 pbuf = NULL;
27082 }
27083 }
27084 }
27085 else
27086 {
27087 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27088 /* Only replace it when it starts with '~' */
27089 if (*dirname == '~')
27090 {
27091 s = vim_strsave(dirname);
27092 if (s != NULL)
27093 {
27094 *fnamep = s;
27095 vim_free(*bufp);
27096 *bufp = s;
27097 }
27098 }
27099 }
27100 vim_free(pbuf);
27101 }
27102 }
27103
27104 tail = gettail(*fnamep);
27105 *fnamelen = (int)STRLEN(*fnamep);
27106
27107 /* ":h" - head, remove "/file_name", can be repeated */
27108 /* Don't remove the first "/" or "c:\" */
27109 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27110 {
27111 valid |= VALID_HEAD;
27112 *usedlen += 2;
27113 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027114 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027115 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027116 *fnamelen = (int)(tail - *fnamep);
27117#ifdef VMS
27118 if (*fnamelen > 0)
27119 *fnamelen += 1; /* the path separator is part of the path */
27120#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027121 if (*fnamelen == 0)
27122 {
27123 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27124 p = vim_strsave((char_u *)".");
27125 if (p == NULL)
27126 return -1;
27127 vim_free(*bufp);
27128 *bufp = *fnamep = tail = p;
27129 *fnamelen = 1;
27130 }
27131 else
27132 {
27133 while (tail > s && !after_pathsep(s, tail))
27134 mb_ptr_back(*fnamep, tail);
27135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027136 }
27137
27138 /* ":8" - shortname */
27139 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27140 {
27141 *usedlen += 2;
27142#ifdef WIN3264
27143 has_shortname = 1;
27144#endif
27145 }
27146
27147#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027148 /*
27149 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027150 */
27151 if (has_shortname)
27152 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027153 /* Copy the string if it is shortened by :h and when it wasn't copied
27154 * yet, because we are going to change it in place. Avoids changing
27155 * the buffer name for "%:8". */
27156 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027157 {
27158 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027159 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027160 return -1;
27161 vim_free(*bufp);
27162 *bufp = *fnamep = p;
27163 }
27164
27165 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027166 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027167 if (!has_fullname && !vim_isAbsName(*fnamep))
27168 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027169 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027170 return -1;
27171 }
27172 else
27173 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027174 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027175
Bram Moolenaardc935552011-08-17 15:23:23 +020027176 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027177 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027178 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027179 return -1;
27180
27181 if (l == 0)
27182 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027183 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027184 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027185 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027186 return -1;
27187 }
27188 *fnamelen = l;
27189 }
27190 }
27191#endif /* WIN3264 */
27192
27193 /* ":t" - tail, just the basename */
27194 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27195 {
27196 *usedlen += 2;
27197 *fnamelen -= (int)(tail - *fnamep);
27198 *fnamep = tail;
27199 }
27200
27201 /* ":e" - extension, can be repeated */
27202 /* ":r" - root, without extension, can be repeated */
27203 while (src[*usedlen] == ':'
27204 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27205 {
27206 /* find a '.' in the tail:
27207 * - for second :e: before the current fname
27208 * - otherwise: The last '.'
27209 */
27210 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27211 s = *fnamep - 2;
27212 else
27213 s = *fnamep + *fnamelen - 1;
27214 for ( ; s > tail; --s)
27215 if (s[0] == '.')
27216 break;
27217 if (src[*usedlen + 1] == 'e') /* :e */
27218 {
27219 if (s > tail)
27220 {
27221 *fnamelen += (int)(*fnamep - (s + 1));
27222 *fnamep = s + 1;
27223#ifdef VMS
27224 /* cut version from the extension */
27225 s = *fnamep + *fnamelen - 1;
27226 for ( ; s > *fnamep; --s)
27227 if (s[0] == ';')
27228 break;
27229 if (s > *fnamep)
27230 *fnamelen = s - *fnamep;
27231#endif
27232 }
27233 else if (*fnamep <= tail)
27234 *fnamelen = 0;
27235 }
27236 else /* :r */
27237 {
27238 if (s > tail) /* remove one extension */
27239 *fnamelen = (int)(s - *fnamep);
27240 }
27241 *usedlen += 2;
27242 }
27243
27244 /* ":s?pat?foo?" - substitute */
27245 /* ":gs?pat?foo?" - global substitute */
27246 if (src[*usedlen] == ':'
27247 && (src[*usedlen + 1] == 's'
27248 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27249 {
27250 char_u *str;
27251 char_u *pat;
27252 char_u *sub;
27253 int sep;
27254 char_u *flags;
27255 int didit = FALSE;
27256
27257 flags = (char_u *)"";
27258 s = src + *usedlen + 2;
27259 if (src[*usedlen + 1] == 'g')
27260 {
27261 flags = (char_u *)"g";
27262 ++s;
27263 }
27264
27265 sep = *s++;
27266 if (sep)
27267 {
27268 /* find end of pattern */
27269 p = vim_strchr(s, sep);
27270 if (p != NULL)
27271 {
27272 pat = vim_strnsave(s, (int)(p - s));
27273 if (pat != NULL)
27274 {
27275 s = p + 1;
27276 /* find end of substitution */
27277 p = vim_strchr(s, sep);
27278 if (p != NULL)
27279 {
27280 sub = vim_strnsave(s, (int)(p - s));
27281 str = vim_strnsave(*fnamep, *fnamelen);
27282 if (sub != NULL && str != NULL)
27283 {
27284 *usedlen = (int)(p + 1 - src);
27285 s = do_string_sub(str, pat, sub, flags);
27286 if (s != NULL)
27287 {
27288 *fnamep = s;
27289 *fnamelen = (int)STRLEN(s);
27290 vim_free(*bufp);
27291 *bufp = s;
27292 didit = TRUE;
27293 }
27294 }
27295 vim_free(sub);
27296 vim_free(str);
27297 }
27298 vim_free(pat);
27299 }
27300 }
27301 /* after using ":s", repeat all the modifiers */
27302 if (didit)
27303 goto repeat;
27304 }
27305 }
27306
Bram Moolenaar26df0922014-02-23 23:39:13 +010027307 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27308 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027309 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027310 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027311 if (c != NUL)
27312 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027313 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027314 if (c != NUL)
27315 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027316 if (p == NULL)
27317 return -1;
27318 vim_free(*bufp);
27319 *bufp = *fnamep = p;
27320 *fnamelen = (int)STRLEN(p);
27321 *usedlen += 2;
27322 }
27323
Bram Moolenaar071d4272004-06-13 20:20:40 +000027324 return valid;
27325}
27326
27327/*
27328 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27329 * "flags" can be "g" to do a global substitute.
27330 * Returns an allocated string, NULL for error.
27331 */
27332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027333do_string_sub(
27334 char_u *str,
27335 char_u *pat,
27336 char_u *sub,
27337 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027338{
27339 int sublen;
27340 regmatch_T regmatch;
27341 int i;
27342 int do_all;
27343 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027344 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027345 garray_T ga;
27346 char_u *ret;
27347 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027348 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027349
27350 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27351 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027352 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027353
27354 ga_init2(&ga, 1, 200);
27355
27356 do_all = (flags[0] == 'g');
27357
27358 regmatch.rm_ic = p_ic;
27359 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27360 if (regmatch.regprog != NULL)
27361 {
27362 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027363 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027364 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27365 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027366 /* Skip empty match except for first match. */
27367 if (regmatch.startp[0] == regmatch.endp[0])
27368 {
27369 if (zero_width == regmatch.startp[0])
27370 {
27371 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027372 i = MB_PTR2LEN(tail);
27373 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27374 (size_t)i);
27375 ga.ga_len += i;
27376 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027377 continue;
27378 }
27379 zero_width = regmatch.startp[0];
27380 }
27381
Bram Moolenaar071d4272004-06-13 20:20:40 +000027382 /*
27383 * Get some space for a temporary buffer to do the substitution
27384 * into. It will contain:
27385 * - The text up to where the match is.
27386 * - The substituted text.
27387 * - The text after the match.
27388 */
27389 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027390 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027391 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27392 {
27393 ga_clear(&ga);
27394 break;
27395 }
27396
27397 /* copy the text up to where the match is */
27398 i = (int)(regmatch.startp[0] - tail);
27399 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27400 /* add the substituted text */
27401 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27402 + ga.ga_len + i, TRUE, TRUE, FALSE);
27403 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027404 tail = regmatch.endp[0];
27405 if (*tail == NUL)
27406 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027407 if (!do_all)
27408 break;
27409 }
27410
27411 if (ga.ga_data != NULL)
27412 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27413
Bram Moolenaar473de612013-06-08 18:19:48 +020027414 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027415 }
27416
27417 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27418 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027419 if (p_cpo == empty_option)
27420 p_cpo = save_cpo;
27421 else
27422 /* Darn, evaluating {sub} expression changed the value. */
27423 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027424
27425 return ret;
27426}
27427
27428#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */