blob: 3ab8ea4956b0c19f3fb6376e961da2f68543b414 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200352 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000353 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
355 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000356 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000357 {VV_NAME("swapname", VAR_STRING), VV_RO},
358 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000359 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200360 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000361 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200362 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000363 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
364 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000365 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000368 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200369 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200370 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200371 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200372 {VV_NAME("option_new", VAR_STRING), VV_RO},
373 {VV_NAME("option_old", VAR_STRING), VV_RO},
374 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100375 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100376 {VV_NAME("false", VAR_SPECIAL), VV_RO},
377 {VV_NAME("true", VAR_SPECIAL), VV_RO},
378 {VV_NAME("null", VAR_SPECIAL), VV_RO},
379 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100380 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200381 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000382};
383
384/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_type vv_di.di_tv.v_type
386#define vv_nr vv_di.di_tv.vval.v_number
387#define vv_float vv_di.di_tv.vval.v_float
388#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000389#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200390#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000391#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000392
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200393static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000394#define vimvarht vimvardict.dv_hashtab
395
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100396static void prepare_vimvar(int idx, typval_T *save_tv);
397static void restore_vimvar(int idx, typval_T *save_tv);
398static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
399static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
400static char_u *skip_var_one(char_u *arg);
401static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
402static void list_glob_vars(int *first);
403static void list_buf_vars(int *first);
404static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000407#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100408static void list_vim_vars(int *first);
409static void list_script_vars(int *first);
410static void list_func_vars(int *first);
411static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
412static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
413static int check_changedtick(char_u *arg);
414static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
415static void clear_lval(lval_T *lp);
416static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
417static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
418static void list_fix_watch(list_T *l, listitem_T *item);
419static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
420static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
421static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
422static void item_lock(typval_T *tv, int deep, int lock);
423static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000424
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100425static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
426static int eval1(char_u **arg, typval_T *rettv, int evaluate);
427static int eval2(char_u **arg, typval_T *rettv, int evaluate);
428static int eval3(char_u **arg, typval_T *rettv, int evaluate);
429static int eval4(char_u **arg, typval_T *rettv, int evaluate);
430static int eval5(char_u **arg, typval_T *rettv, int evaluate);
431static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
432static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000433
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100434static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
435static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
438static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200439static void list_free_contents(list_T *l);
440static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100441static long list_len(list_T *l);
442static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
443static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
444static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
445static long list_find_nr(list_T *l, long idx, int *errorp);
446static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100447static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
448static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
449static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200450static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
451static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
452static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100453static int free_unref_items(int copyID);
454static dictitem_T *dictitem_copy(dictitem_T *org);
455static void dictitem_remove(dict_T *dict, dictitem_T *item);
456static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
457static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200460static char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int dict_val);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static char_u *string_quote(char_u *str, int function);
463static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
464static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100465static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
466static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100467static void emsg_funcname(char *ermsg, char_u *name);
468static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200470static void dict_free_contents(dict_T *d);
471static void dict_free_dict(dict_T *d);
472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_abs(typval_T *argvars, typval_T *rettv);
475static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100477static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_and(typval_T *argvars, typval_T *rettv);
479static void f_append(typval_T *argvars, typval_T *rettv);
480static void f_argc(typval_T *argvars, typval_T *rettv);
481static void f_argidx(typval_T *argvars, typval_T *rettv);
482static void f_arglistid(typval_T *argvars, typval_T *rettv);
483static void f_argv(typval_T *argvars, typval_T *rettv);
484static void f_assert_equal(typval_T *argvars, typval_T *rettv);
485static void f_assert_exception(typval_T *argvars, typval_T *rettv);
486static void f_assert_fails(typval_T *argvars, typval_T *rettv);
487static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200488static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200489static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
490static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_asin(typval_T *argvars, typval_T *rettv);
494static void f_atan(typval_T *argvars, typval_T *rettv);
495static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_browse(typval_T *argvars, typval_T *rettv);
498static void f_browsedir(typval_T *argvars, typval_T *rettv);
499static void f_bufexists(typval_T *argvars, typval_T *rettv);
500static void f_buflisted(typval_T *argvars, typval_T *rettv);
501static void f_bufloaded(typval_T *argvars, typval_T *rettv);
502static void f_bufname(typval_T *argvars, typval_T *rettv);
503static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200504static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100505static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
506static void f_byte2line(typval_T *argvars, typval_T *rettv);
507static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
508static void f_byteidx(typval_T *argvars, typval_T *rettv);
509static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
510static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100516static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
517static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100518static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100519static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100520static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100521static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
523static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100524static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100525static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
527static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100528static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100529static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_changenr(typval_T *argvars, typval_T *rettv);
532static void f_char2nr(typval_T *argvars, typval_T *rettv);
533static void f_cindent(typval_T *argvars, typval_T *rettv);
534static void f_clearmatches(typval_T *argvars, typval_T *rettv);
535static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_complete(typval_T *argvars, typval_T *rettv);
538static void f_complete_add(typval_T *argvars, typval_T *rettv);
539static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_confirm(typval_T *argvars, typval_T *rettv);
542static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_cos(typval_T *argvars, typval_T *rettv);
545static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_count(typval_T *argvars, typval_T *rettv);
548static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
549static void f_cursor(typval_T *argsvars, typval_T *rettv);
550static void f_deepcopy(typval_T *argvars, typval_T *rettv);
551static void f_delete(typval_T *argvars, typval_T *rettv);
552static void f_did_filetype(typval_T *argvars, typval_T *rettv);
553static void f_diff_filler(typval_T *argvars, typval_T *rettv);
554static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
555static void f_empty(typval_T *argvars, typval_T *rettv);
556static void f_escape(typval_T *argvars, typval_T *rettv);
557static void f_eval(typval_T *argvars, typval_T *rettv);
558static void f_eventhandler(typval_T *argvars, typval_T *rettv);
559static void f_executable(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79815f12016-07-09 17:07:29 +0200560static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_exepath(typval_T *argvars, typval_T *rettv);
562static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200563#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200565#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100566static void f_expand(typval_T *argvars, typval_T *rettv);
567static void f_extend(typval_T *argvars, typval_T *rettv);
568static void f_feedkeys(typval_T *argvars, typval_T *rettv);
569static void f_filereadable(typval_T *argvars, typval_T *rettv);
570static void f_filewritable(typval_T *argvars, typval_T *rettv);
571static void f_filter(typval_T *argvars, typval_T *rettv);
572static void f_finddir(typval_T *argvars, typval_T *rettv);
573static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000574#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100575static void f_float2nr(typval_T *argvars, typval_T *rettv);
576static void f_floor(typval_T *argvars, typval_T *rettv);
577static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000578#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100579static void f_fnameescape(typval_T *argvars, typval_T *rettv);
580static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
581static void f_foldclosed(typval_T *argvars, typval_T *rettv);
582static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
583static void f_foldlevel(typval_T *argvars, typval_T *rettv);
584static void f_foldtext(typval_T *argvars, typval_T *rettv);
585static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
586static void f_foreground(typval_T *argvars, typval_T *rettv);
587static void f_function(typval_T *argvars, typval_T *rettv);
588static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
589static void f_get(typval_T *argvars, typval_T *rettv);
590static void f_getbufline(typval_T *argvars, typval_T *rettv);
591static void f_getbufvar(typval_T *argvars, typval_T *rettv);
592static void f_getchar(typval_T *argvars, typval_T *rettv);
593static void f_getcharmod(typval_T *argvars, typval_T *rettv);
594static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
595static void f_getcmdline(typval_T *argvars, typval_T *rettv);
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 Moolenaar071d4272004-06-13 20:20:40 +00008567 {"eventhandler", 0, 0, f_eventhandler},
8568 {"executable", 1, 1, f_executable},
Bram Moolenaar79815f12016-07-09 17:07:29 +02008569 {"execute", 1, 2, f_execute},
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 Moolenaar1e5e1232016-07-07 17:33:02 +020011348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 * "eventhandler()" function
11350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011352f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355}
11356
11357/*
11358 * "executable()" function
11359 */
11360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011361f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011363 char_u *name = get_tv_string(&argvars[0]);
11364
11365 /* Check in $PATH and also check directly if there is a directory name. */
11366 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11367 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011368}
11369
Bram Moolenaar79815f12016-07-09 17:07:29 +020011370static garray_T redir_execute_ga;
11371
11372/*
11373 * Append "value[value_len]" to the execute() output.
11374 */
11375 void
11376execute_redir_str(char_u *value, int value_len)
11377{
11378 int len;
11379
11380 if (value_len == -1)
11381 len = (int)STRLEN(value); /* Append the entire string */
11382 else
11383 len = value_len; /* Append only "value_len" characters */
11384 if (ga_grow(&redir_execute_ga, len) == OK)
11385 {
11386 mch_memmove((char *)redir_execute_ga.ga_data
11387 + redir_execute_ga.ga_len, value, len);
11388 redir_execute_ga.ga_len += len;
11389 }
11390}
11391
11392/*
11393 * Get next line from a list.
11394 * Called by do_cmdline() to get the next line.
11395 * Returns allocated string, or NULL for end of function.
11396 */
11397
11398 static char_u *
11399get_list_line(
11400 int c UNUSED,
11401 void *cookie,
11402 int indent UNUSED)
11403{
11404 listitem_T **p = (listitem_T **)cookie;
11405 listitem_T *item = *p;
11406 char_u buf[NUMBUFLEN];
11407 char_u *s;
11408
11409 if (item == NULL)
11410 return NULL;
11411 s = get_tv_string_buf_chk(&item->li_tv, buf);
11412 *p = item->li_next;
11413 return s == NULL ? NULL : vim_strsave(s);
11414}
11415
11416/*
11417 * "execute()" function
11418 */
11419 static void
11420f_execute(typval_T *argvars, typval_T *rettv)
11421{
11422 char_u *cmd = NULL;
11423 list_T *list = NULL;
11424 int save_msg_silent = msg_silent;
11425 int save_emsg_silent = emsg_silent;
11426 int save_emsg_noredir = emsg_noredir;
11427 int save_redir_execute = redir_execute;
11428 garray_T save_ga;
11429
11430 rettv->vval.v_string = NULL;
11431 rettv->v_type = VAR_STRING;
11432
11433 if (argvars[0].v_type == VAR_LIST)
11434 {
11435 list = argvars[0].vval.v_list;
11436 if (list == NULL || list->lv_first == NULL)
11437 /* empty list, no commands, empty output */
11438 return;
11439 ++list->lv_refcount;
11440 }
11441 else
11442 {
11443 cmd = get_tv_string_chk(&argvars[0]);
11444 if (cmd == NULL)
11445 return;
11446 }
11447
Bram Moolenaar79815f12016-07-09 17:07:29 +020011448 if (argvars[1].v_type != VAR_UNKNOWN)
11449 {
11450 char_u buf[NUMBUFLEN];
11451 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
11452
11453 if (s == NULL)
11454 return;
11455 if (STRNCMP(s, "silent", 6) == 0)
11456 ++msg_silent;
11457 if (STRCMP(s, "silent!") == 0)
11458 {
11459 emsg_silent = TRUE;
11460 emsg_noredir = TRUE;
11461 }
11462 }
11463 else
11464 ++msg_silent;
11465
Bram Moolenaared59aa62016-07-09 17:41:12 +020011466 if (redir_execute)
11467 save_ga = redir_execute_ga;
11468 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
11469 redir_execute = TRUE;
11470
Bram Moolenaar79815f12016-07-09 17:07:29 +020011471 if (cmd != NULL)
11472 do_cmdline_cmd(cmd);
11473 else
11474 {
11475 listitem_T *item = list->lv_first;
11476
11477 do_cmdline(NULL, get_list_line, (void *)&item,
11478 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
11479 --list->lv_refcount;
11480 }
11481
11482 rettv->vval.v_string = redir_execute_ga.ga_data;
11483 msg_silent = save_msg_silent;
11484 emsg_silent = save_emsg_silent;
11485 emsg_noredir = save_emsg_noredir;
11486
11487 redir_execute = save_redir_execute;
11488 if (redir_execute)
11489 redir_execute_ga = save_ga;
11490
11491 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
11492 * line. Put it back in the first column. */
11493 msg_col = 0;
11494}
11495
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011496/*
11497 * "exepath()" function
11498 */
11499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011500f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011501{
11502 char_u *p = NULL;
11503
Bram Moolenaarb5971142015-03-21 17:32:19 +010011504 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011505 rettv->v_type = VAR_STRING;
11506 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507}
11508
11509/*
11510 * "exists()" function
11511 */
11512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011513f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514{
11515 char_u *p;
11516 char_u *name;
11517 int n = FALSE;
11518 int len = 0;
11519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 if (*p == '$') /* environment variable */
11522 {
11523 /* first try "normal" environment variables (fast) */
11524 if (mch_getenv(p + 1) != NULL)
11525 n = TRUE;
11526 else
11527 {
11528 /* try expanding things like $VIM and ${HOME} */
11529 p = expand_env_save(p);
11530 if (p != NULL && *p != '$')
11531 n = TRUE;
11532 vim_free(p);
11533 }
11534 }
11535 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011538 if (*skipwhite(p) != NUL)
11539 n = FALSE; /* trailing garbage */
11540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 else if (*p == '*') /* internal or user defined function */
11542 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011543 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 }
11545 else if (*p == ':')
11546 {
11547 n = cmd_exists(p + 1);
11548 }
11549 else if (*p == '#')
11550 {
11551#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011552 if (p[1] == '#')
11553 n = autocmd_supported(p + 2);
11554 else
11555 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556#endif
11557 }
11558 else /* internal variable */
11559 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011560 char_u *tofree;
11561 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011563 /* get_name_len() takes care of expanding curly braces */
11564 name = p;
11565 len = get_name_len(&p, &tofree, TRUE, FALSE);
11566 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011568 if (tofree != NULL)
11569 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011570 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011571 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011573 /* handle d.key, l[idx], f(expr) */
11574 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11575 if (n)
11576 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 }
11578 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011579 if (*p != NUL)
11580 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011582 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 }
11584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586}
11587
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011588#ifdef FEAT_FLOAT
11589/*
11590 * "exp()" function
11591 */
11592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011593f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011594{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011595 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011596
11597 rettv->v_type = VAR_FLOAT;
11598 if (get_float_arg(argvars, &f) == OK)
11599 rettv->vval.v_float = exp(f);
11600 else
11601 rettv->vval.v_float = 0.0;
11602}
11603#endif
11604
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605/*
11606 * "expand()" function
11607 */
11608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011609f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610{
11611 char_u *s;
11612 int len;
11613 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011614 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011616 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011617 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011620 if (argvars[1].v_type != VAR_UNKNOWN
11621 && argvars[2].v_type != VAR_UNKNOWN
11622 && get_tv_number_chk(&argvars[2], &error)
11623 && !error)
11624 {
11625 rettv->v_type = VAR_LIST;
11626 rettv->vval.v_list = NULL;
11627 }
11628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 if (*s == '%' || *s == '#' || *s == '<')
11631 {
11632 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011633 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011635 if (rettv->v_type == VAR_LIST)
11636 {
11637 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11638 list_append_string(rettv->vval.v_list, result, -1);
11639 else
11640 vim_free(result);
11641 }
11642 else
11643 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 }
11645 else
11646 {
11647 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011648 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011649 if (argvars[1].v_type != VAR_UNKNOWN
11650 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011651 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011652 if (!error)
11653 {
11654 ExpandInit(&xpc);
11655 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011656 if (p_wic)
11657 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011658 if (rettv->v_type == VAR_STRING)
11659 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11660 options, WILD_ALL);
11661 else if (rettv_list_alloc(rettv) != FAIL)
11662 {
11663 int i;
11664
11665 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11666 for (i = 0; i < xpc.xp_numfiles; i++)
11667 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11668 ExpandCleanup(&xpc);
11669 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670 }
11671 else
11672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 }
11674}
11675
11676/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011677 * Go over all entries in "d2" and add them to "d1".
11678 * When "action" is "error" then a duplicate key is an error.
11679 * When "action" is "force" then a duplicate key is overwritten.
11680 * Otherwise duplicate keys are ignored ("action" is "keep").
11681 */
11682 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011683dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011684{
11685 dictitem_T *di1;
11686 hashitem_T *hi2;
11687 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011688 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011689
11690 todo = (int)d2->dv_hashtab.ht_used;
11691 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11692 {
11693 if (!HASHITEM_EMPTY(hi2))
11694 {
11695 --todo;
11696 di1 = dict_find(d1, hi2->hi_key, -1);
11697 if (d1->dv_scope != 0)
11698 {
11699 /* Disallow replacing a builtin function in l: and g:.
11700 * Check the key to be valid when adding to any
11701 * scope. */
11702 if (d1->dv_scope == VAR_DEF_SCOPE
11703 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11704 && var_check_func_name(hi2->hi_key,
11705 di1 == NULL))
11706 break;
11707 if (!valid_varname(hi2->hi_key))
11708 break;
11709 }
11710 if (di1 == NULL)
11711 {
11712 di1 = dictitem_copy(HI2DI(hi2));
11713 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11714 dictitem_free(di1);
11715 }
11716 else if (*action == 'e')
11717 {
11718 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11719 break;
11720 }
11721 else if (*action == 'f' && HI2DI(hi2) != di1)
11722 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011723 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11724 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011725 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011726 clear_tv(&di1->di_tv);
11727 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11728 }
11729 }
11730 }
11731}
11732
11733/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011734 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011735 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011736 */
11737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011738f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011739{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011740 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011741
Bram Moolenaare9a41262005-01-15 22:18:47 +000011742 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011743 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011744 list_T *l1, *l2;
11745 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011747 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011748
Bram Moolenaare9a41262005-01-15 22:18:47 +000011749 l1 = argvars[0].vval.v_list;
11750 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011751 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011752 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011753 {
11754 if (argvars[2].v_type != VAR_UNKNOWN)
11755 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011756 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011757 if (error)
11758 return; /* type error; errmsg already given */
11759
Bram Moolenaar758711c2005-02-02 23:11:38 +000011760 if (before == l1->lv_len)
11761 item = NULL;
11762 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011763 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011764 item = list_find(l1, before);
11765 if (item == NULL)
11766 {
11767 EMSGN(_(e_listidx), before);
11768 return;
11769 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011770 }
11771 }
11772 else
11773 item = NULL;
11774 list_extend(l1, l2, item);
11775
Bram Moolenaare9a41262005-01-15 22:18:47 +000011776 copy_tv(&argvars[0], rettv);
11777 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011778 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011779 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11780 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011781 dict_T *d1, *d2;
11782 char_u *action;
11783 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011784
11785 d1 = argvars[0].vval.v_dict;
11786 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011787 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011788 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011789 {
11790 /* Check the third argument. */
11791 if (argvars[2].v_type != VAR_UNKNOWN)
11792 {
11793 static char *(av[]) = {"keep", "force", "error"};
11794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 action = get_tv_string_chk(&argvars[2]);
11796 if (action == NULL)
11797 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011798 for (i = 0; i < 3; ++i)
11799 if (STRCMP(action, av[i]) == 0)
11800 break;
11801 if (i == 3)
11802 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011803 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011804 return;
11805 }
11806 }
11807 else
11808 action = (char_u *)"force";
11809
Bram Moolenaara9922d62013-05-30 13:01:18 +020011810 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011811
Bram Moolenaare9a41262005-01-15 22:18:47 +000011812 copy_tv(&argvars[0], rettv);
11813 }
11814 }
11815 else
11816 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011817}
11818
11819/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011820 * "feedkeys()" function
11821 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011823f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011824{
11825 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011826 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011827 char_u *keys, *flags;
11828 char_u nbuf[NUMBUFLEN];
11829 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011830 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011831 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011832 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011833
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011834 /* This is not allowed in the sandbox. If the commands would still be
11835 * executed in the sandbox it would be OK, but it probably happens later,
11836 * when "sandbox" is no longer set. */
11837 if (check_secure())
11838 return;
11839
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011840 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011841
11842 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011843 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011844 flags = get_tv_string_buf(&argvars[1], nbuf);
11845 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011846 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011847 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011848 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011849 case 'n': remap = FALSE; break;
11850 case 'm': remap = TRUE; break;
11851 case 't': typed = TRUE; break;
11852 case 'i': insert = TRUE; break;
11853 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011854 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011855 }
11856 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011857 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011858
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011859 if (*keys != NUL || execute)
11860 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011861 /* Need to escape K_SPECIAL and CSI before putting the string in the
11862 * typeahead buffer. */
11863 keys_esc = vim_strsave_escape_csi(keys);
11864 if (keys_esc != NULL)
11865 {
11866 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011867 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011868 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011869 if (vgetc_busy)
11870 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011871 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011872 {
11873 int save_msg_scroll = msg_scroll;
11874
11875 /* Avoid a 1 second delay when the keys start Insert mode. */
11876 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011877
Bram Moolenaar245c4102016-04-20 17:37:41 +020011878 if (!dangerous)
11879 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011880 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011881 if (!dangerous)
11882 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011883 msg_scroll |= save_msg_scroll;
11884 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011885 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011886 }
11887}
11888
11889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890 * "filereadable()" function
11891 */
11892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011893f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011895 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 char_u *p;
11897 int n;
11898
Bram Moolenaarc236c162008-07-13 17:41:49 +000011899#ifndef O_NONBLOCK
11900# define O_NONBLOCK 0
11901#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011903 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11904 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 {
11906 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011907 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908 }
11909 else
11910 n = FALSE;
11911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913}
11914
11915/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011916 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 * rights to write into.
11918 */
11919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011920f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011922 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923}
11924
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011925 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011926findfilendir(
11927 typval_T *argvars UNUSED,
11928 typval_T *rettv,
11929 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011930{
11931#ifdef FEAT_SEARCHPATH
11932 char_u *fname;
11933 char_u *fresult = NULL;
11934 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11935 char_u *p;
11936 char_u pathbuf[NUMBUFLEN];
11937 int count = 1;
11938 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011939 int error = FALSE;
11940#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011941
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011942 rettv->vval.v_string = NULL;
11943 rettv->v_type = VAR_STRING;
11944
11945#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011947
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011948 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011950 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11951 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011952 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011953 else
11954 {
11955 if (*p != NUL)
11956 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011959 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011960 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011961 }
11962
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011963 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11964 error = TRUE;
11965
11966 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011968 do
11969 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011970 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011971 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011972 fresult = find_file_in_path_option(first ? fname : NULL,
11973 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011974 0, first, path,
11975 find_what,
11976 curbuf->b_ffname,
11977 find_what == FINDFILE_DIR
11978 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011979 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011980
11981 if (fresult != NULL && rettv->v_type == VAR_LIST)
11982 list_append_string(rettv->vval.v_list, fresult, -1);
11983
11984 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011986
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011987 if (rettv->v_type == VAR_STRING)
11988 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011989#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011990}
11991
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011992static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011993static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011994
11995/*
11996 * Implementation of map() and filter().
11997 */
11998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011999filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012000{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012001 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 listitem_T *li, *nli;
12003 list_T *l = NULL;
12004 dictitem_T *di;
12005 hashtab_T *ht;
12006 hashitem_T *hi;
12007 dict_T *d = NULL;
12008 typval_T save_val;
12009 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012010 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012011 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012012 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020012013 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012014 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012015 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012016 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012017
Bram Moolenaare9a41262005-01-15 22:18:47 +000012018 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012019 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012020 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012021 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012022 return;
12023 }
12024 else if (argvars[0].v_type == VAR_DICT)
12025 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012026 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012027 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012028 return;
12029 }
12030 else
12031 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000012032 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012033 return;
12034 }
12035
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012036 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012037 /* On type errors, the preceding call has already displayed an error
12038 * message. Avoid a misleading error message for an empty string that
12039 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012040 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012042 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012043
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012044 /* We reset "did_emsg" to be able to detect whether an error
12045 * occurred during evaluation of the expression. */
12046 save_did_emsg = did_emsg;
12047 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012048
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012049 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012050 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 vimvars[VV_KEY].vv_type = VAR_STRING;
12053
12054 ht = &d->dv_hashtab;
12055 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012056 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012057 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 if (!HASHITEM_EMPTY(hi))
12060 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012061 int r;
12062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012063 --todo;
12064 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012065 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012066 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12067 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012068 break;
12069 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012070 r = filter_map_one(&di->di_tv, expr, map, &rem);
12071 clear_tv(&vimvars[VV_KEY].vv_tv);
12072 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073 break;
12074 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012075 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012076 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12077 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012078 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012079 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012081 }
12082 }
12083 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012084 }
12085 else
12086 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012087 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012089 for (li = l->lv_first; li != NULL; li = nli)
12090 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012091 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012092 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012093 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012094 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012095 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012096 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012097 break;
12098 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012099 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012100 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012101 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012102 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012103
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012104 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012105 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012106
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012107 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012108 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012109
12110 copy_tv(&argvars[0], rettv);
12111}
12112
12113 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012114filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012115{
Bram Moolenaar33570922005-01-25 22:26:29 +000012116 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012117 typval_T argv[3];
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012118 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000012119 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012120 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012121 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012122
Bram Moolenaar33570922005-01-25 22:26:29 +000012123 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012124 argv[0] = vimvars[VV_KEY].vv_tv;
12125 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012126 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012127 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012128 s = expr->vval.v_string;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012129 if (call_func(s, (int)STRLEN(s),
12130 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
12131 goto theend;
12132 }
12133 else if (expr->v_type == VAR_PARTIAL)
12134 {
12135 partial_T *partial = expr->vval.v_partial;
12136
12137 s = partial->pt_name;
12138 if (call_func(s, (int)STRLEN(s),
12139 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12140 == FAIL)
12141 goto theend;
12142 }
12143 else
12144 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012145 s = get_tv_string_buf_chk(expr, buf);
12146 if (s == NULL)
12147 goto theend;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012148 s = skipwhite(s);
12149 if (eval1(&s, &rettv, TRUE) == FAIL)
12150 goto theend;
12151 if (*s != NUL) /* check for trailing chars after expr */
12152 {
12153 EMSG2(_(e_invexpr2), s);
12154 goto theend;
12155 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012156 }
12157 if (map)
12158 {
12159 /* map(): replace the list item value */
12160 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012161 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012162 *tv = rettv;
12163 }
12164 else
12165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012166 int error = FALSE;
12167
Bram Moolenaare9a41262005-01-15 22:18:47 +000012168 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012169 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012170 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012171 /* On type error, nothing has been removed; return FAIL to stop the
12172 * loop. The error message was given by get_tv_number_chk(). */
12173 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012174 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012175 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012176 retval = OK;
12177theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012178 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012179 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012180}
12181
12182/*
12183 * "filter()" function
12184 */
12185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012186f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012187{
12188 filter_map(argvars, rettv, FALSE);
12189}
12190
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012191/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012192 * "finddir({fname}[, {path}[, {count}]])" function
12193 */
12194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012196{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012197 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012198}
12199
12200/*
12201 * "findfile({fname}[, {path}[, {count}]])" function
12202 */
12203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012204f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012205{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012206 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012207}
12208
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012209#ifdef FEAT_FLOAT
12210/*
12211 * "float2nr({float})" function
12212 */
12213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012214f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012215{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012216 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012217
12218 if (get_float_arg(argvars, &f) == OK)
12219 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012220# ifdef FEAT_NUM64
12221 if (f < -0x7fffffffffffffff)
12222 rettv->vval.v_number = -0x7fffffffffffffff;
12223 else if (f > 0x7fffffffffffffff)
12224 rettv->vval.v_number = 0x7fffffffffffffff;
12225 else
12226 rettv->vval.v_number = (varnumber_T)f;
12227# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012228 if (f < -0x7fffffff)
12229 rettv->vval.v_number = -0x7fffffff;
12230 else if (f > 0x7fffffff)
12231 rettv->vval.v_number = 0x7fffffff;
12232 else
12233 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012234# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012235 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012236}
12237
12238/*
12239 * "floor({float})" function
12240 */
12241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012242f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012243{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012244 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012245
12246 rettv->v_type = VAR_FLOAT;
12247 if (get_float_arg(argvars, &f) == OK)
12248 rettv->vval.v_float = floor(f);
12249 else
12250 rettv->vval.v_float = 0.0;
12251}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012252
12253/*
12254 * "fmod()" function
12255 */
12256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012257f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012258{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012259 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012260
12261 rettv->v_type = VAR_FLOAT;
12262 if (get_float_arg(argvars, &fx) == OK
12263 && get_float_arg(&argvars[1], &fy) == OK)
12264 rettv->vval.v_float = fmod(fx, fy);
12265 else
12266 rettv->vval.v_float = 0.0;
12267}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012268#endif
12269
Bram Moolenaar0d660222005-01-07 21:51:51 +000012270/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012271 * "fnameescape({string})" function
12272 */
12273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012274f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012275{
12276 rettv->vval.v_string = vim_strsave_fnameescape(
12277 get_tv_string(&argvars[0]), FALSE);
12278 rettv->v_type = VAR_STRING;
12279}
12280
12281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 * "fnamemodify({fname}, {mods})" function
12283 */
12284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012285f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286{
12287 char_u *fname;
12288 char_u *mods;
12289 int usedlen = 0;
12290 int len;
12291 char_u *fbuf = NULL;
12292 char_u buf[NUMBUFLEN];
12293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012294 fname = get_tv_string_chk(&argvars[0]);
12295 mods = get_tv_string_buf_chk(&argvars[1], buf);
12296 if (fname == NULL || mods == NULL)
12297 fname = NULL;
12298 else
12299 {
12300 len = (int)STRLEN(fname);
12301 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012306 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 vim_free(fbuf);
12310}
12311
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012312static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313
12314/*
12315 * "foldclosed()" function
12316 */
12317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012318foldclosed_both(
12319 typval_T *argvars UNUSED,
12320 typval_T *rettv,
12321 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322{
12323#ifdef FEAT_FOLDING
12324 linenr_T lnum;
12325 linenr_T first, last;
12326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12329 {
12330 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12331 {
12332 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012333 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 return;
12337 }
12338 }
12339#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341}
12342
12343/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012344 * "foldclosed()" function
12345 */
12346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012347f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012348{
12349 foldclosed_both(argvars, rettv, FALSE);
12350}
12351
12352/*
12353 * "foldclosedend()" function
12354 */
12355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012356f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012357{
12358 foldclosed_both(argvars, rettv, TRUE);
12359}
12360
12361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 * "foldlevel()" function
12363 */
12364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012365f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366{
12367#ifdef FEAT_FOLDING
12368 linenr_T lnum;
12369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012370 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012372 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375
12376/*
12377 * "foldtext()" function
12378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012380f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381{
12382#ifdef FEAT_FOLDING
12383 linenr_T lnum;
12384 char_u *s;
12385 char_u *r;
12386 int len;
12387 char *txt;
12388#endif
12389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012390 rettv->v_type = VAR_STRING;
12391 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012393 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12394 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12395 <= curbuf->b_ml.ml_line_count
12396 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 {
12398 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012399 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12400 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 {
12402 if (!linewhite(lnum))
12403 break;
12404 ++lnum;
12405 }
12406
12407 /* Find interesting text in this line. */
12408 s = skipwhite(ml_get(lnum));
12409 /* skip C comment-start */
12410 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012413 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012414 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012415 {
12416 s = skipwhite(ml_get(lnum + 1));
12417 if (*s == '*')
12418 s = skipwhite(s + 1);
12419 }
12420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 txt = _("+-%s%3ld lines: ");
12422 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012423 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 + 20 /* for %3ld */
12425 + STRLEN(s))); /* concatenated */
12426 if (r != NULL)
12427 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012428 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12429 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12430 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 len = (int)STRLEN(r);
12432 STRCAT(r, s);
12433 /* remove 'foldmarker' and 'commentstring' */
12434 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 }
12437 }
12438#endif
12439}
12440
12441/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012442 * "foldtextresult(lnum)" function
12443 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012445f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012446{
12447#ifdef FEAT_FOLDING
12448 linenr_T lnum;
12449 char_u *text;
12450 char_u buf[51];
12451 foldinfo_T foldinfo;
12452 int fold_count;
12453#endif
12454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012455 rettv->v_type = VAR_STRING;
12456 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012457#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012459 /* treat illegal types and illegal string values for {lnum} the same */
12460 if (lnum < 0)
12461 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012462 fold_count = foldedCount(curwin, lnum, &foldinfo);
12463 if (fold_count > 0)
12464 {
12465 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12466 &foldinfo, buf);
12467 if (text == buf)
12468 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012469 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012470 }
12471#endif
12472}
12473
12474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 * "foreground()" function
12476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012478f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480#ifdef FEAT_GUI
12481 if (gui.in_use)
12482 gui_mch_set_foreground();
12483#else
12484# ifdef WIN32
12485 win32_set_foreground();
12486# endif
12487#endif
12488}
12489
12490/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012491 * "function()" function
12492 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012494f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012495{
12496 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012497 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012498 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012499 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012500
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012501 if (argvars[0].v_type == VAR_FUNC)
12502 {
12503 /* function(MyFunc, [arg], dict) */
12504 s = argvars[0].vval.v_string;
12505 }
12506 else if (argvars[0].v_type == VAR_PARTIAL
12507 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012508 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012509 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012510 arg_pt = argvars[0].vval.v_partial;
12511 s = arg_pt->pt_name;
12512 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012513 else
12514 {
12515 /* function('MyFunc', [arg], dict) */
12516 s = get_tv_string(&argvars[0]);
12517 use_string = TRUE;
12518 }
12519
12520 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012521 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012522 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012523 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12524 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012525 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012526 else
12527 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012528 int dict_idx = 0;
12529 int arg_idx = 0;
12530 list_T *list = NULL;
12531
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012532 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012533 {
12534 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012535 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012536
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012537 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12538 * also be called from another script. Using trans_function_name()
12539 * would also work, but some plugins depend on the name being
12540 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012541 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012542 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12543 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012544 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012545 STRCPY(name, sid_buf);
12546 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012547 }
12548 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012549 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012550 name = vim_strsave(s);
12551
12552 if (argvars[1].v_type != VAR_UNKNOWN)
12553 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012554 if (argvars[2].v_type != VAR_UNKNOWN)
12555 {
12556 /* function(name, [args], dict) */
12557 arg_idx = 1;
12558 dict_idx = 2;
12559 }
12560 else if (argvars[1].v_type == VAR_DICT)
12561 /* function(name, dict) */
12562 dict_idx = 1;
12563 else
12564 /* function(name, [args]) */
12565 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012566 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012567 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012568 if (argvars[dict_idx].v_type != VAR_DICT)
12569 {
12570 EMSG(_("E922: expected a dict"));
12571 vim_free(name);
12572 return;
12573 }
12574 if (argvars[dict_idx].vval.v_dict == NULL)
12575 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012576 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012577 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012578 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012579 if (argvars[arg_idx].v_type != VAR_LIST)
12580 {
12581 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12582 vim_free(name);
12583 return;
12584 }
12585 list = argvars[arg_idx].vval.v_list;
12586 if (list == NULL || list->lv_len == 0)
12587 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012588 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012589 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012590 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012591 {
12592 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012593
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012594 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012595 if (pt == NULL)
12596 vim_free(name);
12597 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012598 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012599 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012600 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012601 listitem_T *li;
12602 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012603 int arg_len = 0;
12604 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012605
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012606 if (arg_pt != NULL)
12607 arg_len = arg_pt->pt_argc;
12608 if (list != NULL)
12609 lv_len = list->lv_len;
12610 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012611 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012612 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012613 if (pt->pt_argv == NULL)
12614 {
12615 vim_free(pt);
12616 vim_free(name);
12617 return;
12618 }
12619 else
12620 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012621 for (i = 0; i < arg_len; i++)
12622 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12623 if (lv_len > 0)
12624 for (li = list->lv_first; li != NULL;
12625 li = li->li_next)
12626 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012627 }
12628 }
12629
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012630 /* For "function(dict.func, [], dict)" and "func" is a partial
12631 * use "dict". That is backwards compatible. */
12632 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012633 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012634 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012635 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12636 ++pt->pt_dict->dv_refcount;
12637 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012638 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012639 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012640 /* If the dict was bound automatically the result is also
12641 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012642 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012643 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012644 if (pt->pt_dict != NULL)
12645 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012646 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012647
12648 pt->pt_refcount = 1;
12649 pt->pt_name = name;
12650 func_ref(pt->pt_name);
12651 }
12652 rettv->v_type = VAR_PARTIAL;
12653 rettv->vval.v_partial = pt;
12654 }
12655 else
12656 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012657 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012658 rettv->v_type = VAR_FUNC;
12659 rettv->vval.v_string = name;
12660 func_ref(name);
12661 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012662 }
12663}
12664
12665/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012666 * "garbagecollect()" function
12667 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012669f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012670{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012671 /* This is postponed until we are back at the toplevel, because we may be
12672 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12673 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012674
12675 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12676 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012677}
12678
12679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012680 * "get()" function
12681 */
12682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012683f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012684{
Bram Moolenaar33570922005-01-25 22:26:29 +000012685 listitem_T *li;
12686 list_T *l;
12687 dictitem_T *di;
12688 dict_T *d;
12689 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012690
Bram Moolenaare9a41262005-01-15 22:18:47 +000012691 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012692 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012693 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012695 int error = FALSE;
12696
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012697 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012698 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012699 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012700 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012701 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012702 else if (argvars[0].v_type == VAR_DICT)
12703 {
12704 if ((d = argvars[0].vval.v_dict) != NULL)
12705 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012706 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012707 if (di != NULL)
12708 tv = &di->di_tv;
12709 }
12710 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012711 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012712 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012713 partial_T *pt;
12714 partial_T fref_pt;
12715
12716 if (argvars[0].v_type == VAR_PARTIAL)
12717 pt = argvars[0].vval.v_partial;
12718 else
12719 {
12720 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12721 fref_pt.pt_name = argvars[0].vval.v_string;
12722 pt = &fref_pt;
12723 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012724
12725 if (pt != NULL)
12726 {
12727 char_u *what = get_tv_string(&argvars[1]);
12728
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012729 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012730 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012731 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012732 if (pt->pt_name == NULL)
12733 rettv->vval.v_string = NULL;
12734 else
12735 rettv->vval.v_string = vim_strsave(pt->pt_name);
12736 }
12737 else if (STRCMP(what, "dict") == 0)
12738 {
12739 rettv->v_type = VAR_DICT;
12740 rettv->vval.v_dict = pt->pt_dict;
12741 if (pt->pt_dict != NULL)
12742 ++pt->pt_dict->dv_refcount;
12743 }
12744 else if (STRCMP(what, "args") == 0)
12745 {
12746 rettv->v_type = VAR_LIST;
12747 if (rettv_list_alloc(rettv) == OK)
12748 {
12749 int i;
12750
12751 for (i = 0; i < pt->pt_argc; ++i)
12752 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12753 }
12754 }
12755 else
12756 EMSG2(_(e_invarg2), what);
12757 return;
12758 }
12759 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012760 else
12761 EMSG2(_(e_listdictarg), "get()");
12762
12763 if (tv == NULL)
12764 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012765 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012766 copy_tv(&argvars[2], rettv);
12767 }
12768 else
12769 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012770}
12771
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012772static 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 +000012773
12774/*
12775 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012776 * Return a range (from start to end) of lines in rettv from the specified
12777 * buffer.
12778 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012779 */
12780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012781get_buffer_lines(
12782 buf_T *buf,
12783 linenr_T start,
12784 linenr_T end,
12785 int retlist,
12786 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012787{
12788 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012789
Bram Moolenaar959a1432013-12-14 12:17:38 +010012790 rettv->v_type = VAR_STRING;
12791 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012792 if (retlist && rettv_list_alloc(rettv) == FAIL)
12793 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012794
12795 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12796 return;
12797
12798 if (!retlist)
12799 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012800 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12801 p = ml_get_buf(buf, start, FALSE);
12802 else
12803 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012804 rettv->vval.v_string = vim_strsave(p);
12805 }
12806 else
12807 {
12808 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012809 return;
12810
12811 if (start < 1)
12812 start = 1;
12813 if (end > buf->b_ml.ml_line_count)
12814 end = buf->b_ml.ml_line_count;
12815 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012816 if (list_append_string(rettv->vval.v_list,
12817 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012818 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012819 }
12820}
12821
12822/*
12823 * "getbufline()" function
12824 */
12825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012826f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012827{
12828 linenr_T lnum;
12829 linenr_T end;
12830 buf_T *buf;
12831
12832 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12833 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012834 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012835 --emsg_off;
12836
Bram Moolenaar661b1822005-07-28 22:36:45 +000012837 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012838 if (argvars[2].v_type == VAR_UNKNOWN)
12839 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012840 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012841 end = get_tv_lnum_buf(&argvars[2], buf);
12842
Bram Moolenaar342337a2005-07-21 21:11:17 +000012843 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012844}
12845
Bram Moolenaar0d660222005-01-07 21:51:51 +000012846/*
12847 * "getbufvar()" function
12848 */
12849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012850f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012851{
12852 buf_T *buf;
12853 buf_T *save_curbuf;
12854 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012855 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012856 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012858 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12859 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012860 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012861 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012862
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012863 rettv->v_type = VAR_STRING;
12864 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012865
12866 if (buf != NULL && varname != NULL)
12867 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012868 /* set curbuf to be our buf, temporarily */
12869 save_curbuf = curbuf;
12870 curbuf = buf;
12871
Bram Moolenaar0d660222005-01-07 21:51:51 +000012872 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012873 {
12874 if (get_option_tv(&varname, rettv, TRUE) == OK)
12875 done = TRUE;
12876 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012877 else if (STRCMP(varname, "changedtick") == 0)
12878 {
12879 rettv->v_type = VAR_NUMBER;
12880 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012881 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012882 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012883 else
12884 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012885 /* Look up the variable. */
12886 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12887 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12888 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012889 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012890 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012891 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012892 done = TRUE;
12893 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012894 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012895
12896 /* restore previous notion of curbuf */
12897 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012898 }
12899
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012900 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12901 /* use the default value */
12902 copy_tv(&argvars[2], rettv);
12903
Bram Moolenaar0d660222005-01-07 21:51:51 +000012904 --emsg_off;
12905}
12906
12907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 * "getchar()" function
12909 */
12910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012911f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912{
12913 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012914 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012916 /* Position the cursor. Needed after a message that ends in a space. */
12917 windgoto(msg_row, msg_col);
12918
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 ++no_mapping;
12920 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012921 for (;;)
12922 {
12923 if (argvars[0].v_type == VAR_UNKNOWN)
12924 /* getchar(): blocking wait. */
12925 n = safe_vgetc();
12926 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12927 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012928 n = vpeekc_any();
12929 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012930 /* illegal argument or getchar(0) and no char avail: return zero */
12931 n = 0;
12932 else
12933 /* getchar(0) and char avail: return char */
12934 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012935
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012936 if (n == K_IGNORE)
12937 continue;
12938 break;
12939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 --no_mapping;
12941 --allow_keys;
12942
Bram Moolenaar219b8702006-11-01 14:32:36 +000012943 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012944 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012945 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12946 vimvars[VV_MOUSE_COL].vv_nr = 0;
12947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 if (IS_SPECIAL(n) || mod_mask != 0)
12950 {
12951 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12952 int i = 0;
12953
12954 /* Turn a special key into three bytes, plus modifier. */
12955 if (mod_mask != 0)
12956 {
12957 temp[i++] = K_SPECIAL;
12958 temp[i++] = KS_MODIFIER;
12959 temp[i++] = mod_mask;
12960 }
12961 if (IS_SPECIAL(n))
12962 {
12963 temp[i++] = K_SPECIAL;
12964 temp[i++] = K_SECOND(n);
12965 temp[i++] = K_THIRD(n);
12966 }
12967#ifdef FEAT_MBYTE
12968 else if (has_mbyte)
12969 i += (*mb_char2bytes)(n, temp + i);
12970#endif
12971 else
12972 temp[i++] = n;
12973 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012974 rettv->v_type = VAR_STRING;
12975 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012976
12977#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012978 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012979 {
12980 int row = mouse_row;
12981 int col = mouse_col;
12982 win_T *win;
12983 linenr_T lnum;
12984# ifdef FEAT_WINDOWS
12985 win_T *wp;
12986# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012987 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012988
12989 if (row >= 0 && col >= 0)
12990 {
12991 /* Find the window at the mouse coordinates and compute the
12992 * text position. */
12993 win = mouse_find_win(&row, &col);
12994 (void)mouse_comp_pos(win, &row, &col, &lnum);
12995# ifdef FEAT_WINDOWS
12996 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012997 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012998# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012999 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020013000 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000013001 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
13002 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
13003 }
13004 }
13005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006 }
13007}
13008
13009/*
13010 * "getcharmod()" function
13011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013013f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013015 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016}
13017
13018/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013019 * "getcharsearch()" function
13020 */
13021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013022f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013023{
13024 if (rettv_dict_alloc(rettv) != FAIL)
13025 {
13026 dict_T *dict = rettv->vval.v_dict;
13027
13028 dict_add_nr_str(dict, "char", 0L, last_csearch());
13029 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
13030 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
13031 }
13032}
13033
13034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 * "getcmdline()" function
13036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013038f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013040 rettv->v_type = VAR_STRING;
13041 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042}
13043
13044/*
13045 * "getcmdpos()" function
13046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013048f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051}
13052
13053/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013054 * "getcmdtype()" function
13055 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013057f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013058{
13059 rettv->v_type = VAR_STRING;
13060 rettv->vval.v_string = alloc(2);
13061 if (rettv->vval.v_string != NULL)
13062 {
13063 rettv->vval.v_string[0] = get_cmdline_type();
13064 rettv->vval.v_string[1] = NUL;
13065 }
13066}
13067
13068/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013069 * "getcmdwintype()" function
13070 */
13071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013072f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013073{
13074 rettv->v_type = VAR_STRING;
13075 rettv->vval.v_string = NULL;
13076#ifdef FEAT_CMDWIN
13077 rettv->vval.v_string = alloc(2);
13078 if (rettv->vval.v_string != NULL)
13079 {
13080 rettv->vval.v_string[0] = cmdwin_type;
13081 rettv->vval.v_string[1] = NUL;
13082 }
13083#endif
13084}
13085
13086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087 * "getcwd()" function
13088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013090f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013092 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013093 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013096 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010013097
13098 wp = find_tabwin(&argvars[0], &argvars[1]);
13099 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013101 if (wp->w_localdir != NULL)
13102 rettv->vval.v_string = vim_strsave(wp->w_localdir);
13103 else if(globaldir != NULL)
13104 rettv->vval.v_string = vim_strsave(globaldir);
13105 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020013106 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013107 cwd = alloc(MAXPATHL);
13108 if (cwd != NULL)
13109 {
13110 if (mch_dirname(cwd, MAXPATHL) != FAIL)
13111 rettv->vval.v_string = vim_strsave(cwd);
13112 vim_free(cwd);
13113 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020013114 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010013115#ifdef BACKSLASH_IN_FILENAME
13116 if (rettv->vval.v_string != NULL)
13117 slash_adjust(rettv->vval.v_string);
13118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 }
13120}
13121
13122/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013123 * "getfontname()" function
13124 */
13125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013126f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013127{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128 rettv->v_type = VAR_STRING;
13129 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013130#ifdef FEAT_GUI
13131 if (gui.in_use)
13132 {
13133 GuiFont font;
13134 char_u *name = NULL;
13135
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013136 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013137 {
13138 /* Get the "Normal" font. Either the name saved by
13139 * hl_set_font_name() or from the font ID. */
13140 font = gui.norm_font;
13141 name = hl_get_font_name();
13142 }
13143 else
13144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013146 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13147 return;
13148 font = gui_mch_get_font(name, FALSE);
13149 if (font == NOFONT)
13150 return; /* Invalid font name, return empty string. */
13151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013153 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013154 gui_mch_free_font(font);
13155 }
13156#endif
13157}
13158
13159/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013160 * "getfperm({fname})" function
13161 */
13162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013163f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013164{
13165 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013166 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013167 char_u *perm = NULL;
13168 char_u flags[] = "rwx";
13169 int i;
13170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013174 if (mch_stat((char *)fname, &st) >= 0)
13175 {
13176 perm = vim_strsave((char_u *)"---------");
13177 if (perm != NULL)
13178 {
13179 for (i = 0; i < 9; i++)
13180 {
13181 if (st.st_mode & (1 << (8 - i)))
13182 perm[i] = flags[i % 3];
13183 }
13184 }
13185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013187}
13188
13189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 * "getfsize({fname})" function
13191 */
13192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013193f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194{
13195 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013196 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013200 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201
13202 if (mch_stat((char *)fname, &st) >= 0)
13203 {
13204 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013209
13210 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013211 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013212 rettv->vval.v_number = -2;
13213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 }
13215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217}
13218
13219/*
13220 * "getftime({fname})" function
13221 */
13222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013223f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224{
13225 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013226 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229
13230 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234}
13235
13236/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013237 * "getftype({fname})" function
13238 */
13239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013240f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013241{
13242 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013243 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013244 char_u *type = NULL;
13245 char *t;
13246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013247 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013250 if (mch_lstat((char *)fname, &st) >= 0)
13251 {
13252#ifdef S_ISREG
13253 if (S_ISREG(st.st_mode))
13254 t = "file";
13255 else if (S_ISDIR(st.st_mode))
13256 t = "dir";
13257# ifdef S_ISLNK
13258 else if (S_ISLNK(st.st_mode))
13259 t = "link";
13260# endif
13261# ifdef S_ISBLK
13262 else if (S_ISBLK(st.st_mode))
13263 t = "bdev";
13264# endif
13265# ifdef S_ISCHR
13266 else if (S_ISCHR(st.st_mode))
13267 t = "cdev";
13268# endif
13269# ifdef S_ISFIFO
13270 else if (S_ISFIFO(st.st_mode))
13271 t = "fifo";
13272# endif
13273# ifdef S_ISSOCK
13274 else if (S_ISSOCK(st.st_mode))
13275 t = "fifo";
13276# endif
13277 else
13278 t = "other";
13279#else
13280# ifdef S_IFMT
13281 switch (st.st_mode & S_IFMT)
13282 {
13283 case S_IFREG: t = "file"; break;
13284 case S_IFDIR: t = "dir"; break;
13285# ifdef S_IFLNK
13286 case S_IFLNK: t = "link"; break;
13287# endif
13288# ifdef S_IFBLK
13289 case S_IFBLK: t = "bdev"; break;
13290# endif
13291# ifdef S_IFCHR
13292 case S_IFCHR: t = "cdev"; break;
13293# endif
13294# ifdef S_IFIFO
13295 case S_IFIFO: t = "fifo"; break;
13296# endif
13297# ifdef S_IFSOCK
13298 case S_IFSOCK: t = "socket"; break;
13299# endif
13300 default: t = "other";
13301 }
13302# else
13303 if (mch_isdir(fname))
13304 t = "dir";
13305 else
13306 t = "file";
13307# endif
13308#endif
13309 type = vim_strsave((char_u *)t);
13310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013312}
13313
13314/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013315 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013316 */
13317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013318f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013319{
13320 linenr_T lnum;
13321 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013322 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013323
13324 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013325 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013326 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013327 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013328 retlist = FALSE;
13329 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013331 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013332 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013333 retlist = TRUE;
13334 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013335
Bram Moolenaar342337a2005-07-21 21:11:17 +000013336 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013337}
13338
13339/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013340 * "getmatches()" function
13341 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013343f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013344{
13345#ifdef FEAT_SEARCH_EXTRA
13346 dict_T *dict;
13347 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013348 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013349
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013350 if (rettv_list_alloc(rettv) == OK)
13351 {
13352 while (cur != NULL)
13353 {
13354 dict = dict_alloc();
13355 if (dict == NULL)
13356 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013357 if (cur->match.regprog == NULL)
13358 {
13359 /* match added with matchaddpos() */
13360 for (i = 0; i < MAXPOSMATCH; ++i)
13361 {
13362 llpos_T *llpos;
13363 char buf[6];
13364 list_T *l;
13365
13366 llpos = &cur->pos.pos[i];
13367 if (llpos->lnum == 0)
13368 break;
13369 l = list_alloc();
13370 if (l == NULL)
13371 break;
13372 list_append_number(l, (varnumber_T)llpos->lnum);
13373 if (llpos->col > 0)
13374 {
13375 list_append_number(l, (varnumber_T)llpos->col);
13376 list_append_number(l, (varnumber_T)llpos->len);
13377 }
13378 sprintf(buf, "pos%d", i + 1);
13379 dict_add_list(dict, buf, l);
13380 }
13381 }
13382 else
13383 {
13384 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13385 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013386 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013387 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13388 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013389# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013390 if (cur->conceal_char)
13391 {
13392 char_u buf[MB_MAXBYTES + 1];
13393
13394 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13395 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13396 }
13397# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013398 list_append_dict(rettv->vval.v_list, dict);
13399 cur = cur->next;
13400 }
13401 }
13402#endif
13403}
13404
13405/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013406 * "getpid()" function
13407 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013409f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013410{
13411 rettv->vval.v_number = mch_get_pid();
13412}
13413
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013415getpos_both(
13416 typval_T *argvars,
13417 typval_T *rettv,
13418 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013419{
Bram Moolenaara5525202006-03-02 22:52:09 +000013420 pos_T *fp;
13421 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013422 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013423
13424 if (rettv_list_alloc(rettv) == OK)
13425 {
13426 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013427 if (getcurpos)
13428 fp = &curwin->w_cursor;
13429 else
13430 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013431 if (fnum != -1)
13432 list_append_number(l, (varnumber_T)fnum);
13433 else
13434 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013435 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13436 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013437 list_append_number(l, (fp != NULL)
13438 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013439 : (varnumber_T)0);
13440 list_append_number(l,
13441#ifdef FEAT_VIRTUALEDIT
13442 (fp != NULL) ? (varnumber_T)fp->coladd :
13443#endif
13444 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013445 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013446 {
13447 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013448 list_append_number(l, curwin->w_curswant == MAXCOL ?
13449 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013450 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013451 }
13452 else
13453 rettv->vval.v_number = FALSE;
13454}
13455
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013456
13457/*
13458 * "getcurpos()" function
13459 */
13460 static void
13461f_getcurpos(typval_T *argvars, typval_T *rettv)
13462{
13463 getpos_both(argvars, rettv, TRUE);
13464}
13465
13466/*
13467 * "getpos(string)" function
13468 */
13469 static void
13470f_getpos(typval_T *argvars, typval_T *rettv)
13471{
13472 getpos_both(argvars, rettv, FALSE);
13473}
13474
Bram Moolenaara5525202006-03-02 22:52:09 +000013475/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013476 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013477 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013479f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013480{
13481#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013482 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013483#endif
13484
Bram Moolenaar2641f772005-03-25 21:58:17 +000013485#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013486 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013487 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013488 wp = NULL;
13489 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13490 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013491 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013492 if (wp == NULL)
13493 return;
13494 }
13495
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013496 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013497 }
13498#endif
13499}
13500
13501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 * "getreg()" function
13503 */
13504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013505f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506{
13507 char_u *strregname;
13508 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013509 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013510 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013511 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013513 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013515 strregname = get_tv_string_chk(&argvars[0]);
13516 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013517 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013518 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013519 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013520 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013521 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013522 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013525 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013526
13527 if (error)
13528 return;
13529
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 regname = (strregname == NULL ? '"' : *strregname);
13531 if (regname == 0)
13532 regname = '"';
13533
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013534 if (return_list)
13535 {
13536 rettv->v_type = VAR_LIST;
13537 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13538 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013539 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013540 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013541 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013542 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013543 }
13544 else
13545 {
13546 rettv->v_type = VAR_STRING;
13547 rettv->vval.v_string = get_reg_contents(regname,
13548 arg2 ? GREG_EXPR_SRC : 0);
13549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550}
13551
13552/*
13553 * "getregtype()" function
13554 */
13555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013556f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557{
13558 char_u *strregname;
13559 int regname;
13560 char_u buf[NUMBUFLEN + 2];
13561 long reglen = 0;
13562
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013563 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013564 {
13565 strregname = get_tv_string_chk(&argvars[0]);
13566 if (strregname == NULL) /* type error; errmsg already given */
13567 {
13568 rettv->v_type = VAR_STRING;
13569 rettv->vval.v_string = NULL;
13570 return;
13571 }
13572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573 else
13574 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013575 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576
13577 regname = (strregname == NULL ? '"' : *strregname);
13578 if (regname == 0)
13579 regname = '"';
13580
13581 buf[0] = NUL;
13582 buf[1] = NUL;
13583 switch (get_reg_type(regname, &reglen))
13584 {
13585 case MLINE: buf[0] = 'V'; break;
13586 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 case MBLOCK:
13588 buf[0] = Ctrl_V;
13589 sprintf((char *)buf + 1, "%ld", reglen + 1);
13590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 rettv->v_type = VAR_STRING;
13593 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594}
13595
13596/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013597 * "gettabvar()" function
13598 */
13599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013600f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013601{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013602 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013603 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013604 dictitem_T *v;
13605 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013606 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013607
13608 rettv->v_type = VAR_STRING;
13609 rettv->vval.v_string = NULL;
13610
13611 varname = get_tv_string_chk(&argvars[1]);
13612 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13613 if (tp != NULL && varname != NULL)
13614 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013615 /* Set tp to be our tabpage, temporarily. Also set the window to the
13616 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013617 if (switch_win(&oldcurwin, &oldtabpage,
13618 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013619 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013620 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013621 /* look up the variable */
13622 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13623 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13624 if (v != NULL)
13625 {
13626 copy_tv(&v->di_tv, rettv);
13627 done = TRUE;
13628 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013629 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013630
13631 /* restore previous notion of curwin */
13632 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013633 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013634
13635 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013636 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013637}
13638
13639/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013640 * "gettabwinvar()" function
13641 */
13642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013643f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013644{
13645 getwinvar(argvars, rettv, 1);
13646}
13647
13648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 * "getwinposx()" function
13650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013652f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655#ifdef FEAT_GUI
13656 if (gui.in_use)
13657 {
13658 int x, y;
13659
13660 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013661 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 }
13663#endif
13664}
13665
13666/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013667 * "win_findbuf()" function
13668 */
13669 static void
13670f_win_findbuf(typval_T *argvars, typval_T *rettv)
13671{
13672 if (rettv_list_alloc(rettv) != FAIL)
13673 win_findbuf(argvars, rettv->vval.v_list);
13674}
13675
13676/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013677 * "win_getid()" function
13678 */
13679 static void
13680f_win_getid(typval_T *argvars, typval_T *rettv)
13681{
13682 rettv->vval.v_number = win_getid(argvars);
13683}
13684
13685/*
13686 * "win_gotoid()" function
13687 */
13688 static void
13689f_win_gotoid(typval_T *argvars, typval_T *rettv)
13690{
13691 rettv->vval.v_number = win_gotoid(argvars);
13692}
13693
13694/*
13695 * "win_id2tabwin()" function
13696 */
13697 static void
13698f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13699{
13700 if (rettv_list_alloc(rettv) != FAIL)
13701 win_id2tabwin(argvars, rettv->vval.v_list);
13702}
13703
13704/*
13705 * "win_id2win()" function
13706 */
13707 static void
13708f_win_id2win(typval_T *argvars, typval_T *rettv)
13709{
13710 rettv->vval.v_number = win_id2win(argvars);
13711}
13712
13713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 * "getwinposy()" function
13715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013717f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#ifdef FEAT_GUI
13721 if (gui.in_use)
13722 {
13723 int x, y;
13724
13725 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 }
13728#endif
13729}
13730
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013731/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013732 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013733 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013734 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013735find_win_by_nr(
13736 typval_T *vp,
13737 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013738{
13739#ifdef FEAT_WINDOWS
13740 win_T *wp;
13741#endif
13742 int nr;
13743
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013744 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013745
13746#ifdef FEAT_WINDOWS
13747 if (nr < 0)
13748 return NULL;
13749 if (nr == 0)
13750 return curwin;
13751
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013752 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13753 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013754 if (nr >= LOWEST_WIN_ID)
13755 {
13756 if (wp->w_id == nr)
13757 return wp;
13758 }
13759 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013760 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013761 if (nr >= LOWEST_WIN_ID)
13762 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013763 return wp;
13764#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013765 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013766 return curwin;
13767 return NULL;
13768#endif
13769}
13770
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013772 * Find window specified by "wvp" in tabpage "tvp".
13773 */
13774 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013775find_tabwin(
13776 typval_T *wvp, /* VAR_UNKNOWN for current window */
13777 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013778{
13779 win_T *wp = NULL;
13780 tabpage_T *tp = NULL;
13781 long n;
13782
13783 if (wvp->v_type != VAR_UNKNOWN)
13784 {
13785 if (tvp->v_type != VAR_UNKNOWN)
13786 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013787 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013788 if (n >= 0)
13789 tp = find_tabpage(n);
13790 }
13791 else
13792 tp = curtab;
13793
13794 if (tp != NULL)
13795 wp = find_win_by_nr(wvp, tp);
13796 }
13797 else
13798 wp = curwin;
13799
13800 return wp;
13801}
13802
13803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 * "getwinvar()" function
13805 */
13806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013807f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013809 getwinvar(argvars, rettv, 0);
13810}
13811
13812/*
13813 * getwinvar() and gettabwinvar()
13814 */
13815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013816getwinvar(
13817 typval_T *argvars,
13818 typval_T *rettv,
13819 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013820{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013821 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013823 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013824 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013825 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013826#ifdef FEAT_WINDOWS
13827 win_T *oldcurwin;
13828 tabpage_T *oldtabpage;
13829 int need_switch_win;
13830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013832#ifdef FEAT_WINDOWS
13833 if (off == 1)
13834 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13835 else
13836 tp = curtab;
13837#endif
13838 win = find_win_by_nr(&argvars[off], tp);
13839 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013842 rettv->v_type = VAR_STRING;
13843 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844
13845 if (win != NULL && varname != NULL)
13846 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013847#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013848 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013849 * otherwise the window is not valid. Only do this when needed,
13850 * autocommands get blocked. */
13851 need_switch_win = !(tp == curtab && win == curwin);
13852 if (!need_switch_win
13853 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13854#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013855 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013856 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013857 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013858 if (get_option_tv(&varname, rettv, 1) == OK)
13859 done = TRUE;
13860 }
13861 else
13862 {
13863 /* Look up the variable. */
13864 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13865 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13866 varname, FALSE);
13867 if (v != NULL)
13868 {
13869 copy_tv(&v->di_tv, rettv);
13870 done = TRUE;
13871 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013874
Bram Moolenaarba117c22015-09-29 16:53:22 +020013875#ifdef FEAT_WINDOWS
13876 if (need_switch_win)
13877 /* restore previous notion of curwin */
13878 restore_win(oldcurwin, oldtabpage, TRUE);
13879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 }
13881
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013882 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13883 /* use the default return value */
13884 copy_tv(&argvars[off + 2], rettv);
13885
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 --emsg_off;
13887}
13888
13889/*
13890 * "glob()" function
13891 */
13892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013893f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013895 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013897 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013899 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013900 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013901 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013902 if (argvars[1].v_type != VAR_UNKNOWN)
13903 {
13904 if (get_tv_number_chk(&argvars[1], &error))
13905 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013906 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013907 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013908 if (get_tv_number_chk(&argvars[2], &error))
13909 {
13910 rettv->v_type = VAR_LIST;
13911 rettv->vval.v_list = NULL;
13912 }
13913 if (argvars[3].v_type != VAR_UNKNOWN
13914 && get_tv_number_chk(&argvars[3], &error))
13915 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013916 }
13917 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013918 if (!error)
13919 {
13920 ExpandInit(&xpc);
13921 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013922 if (p_wic)
13923 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013924 if (rettv->v_type == VAR_STRING)
13925 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013926 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013927 else if (rettv_list_alloc(rettv) != FAIL)
13928 {
13929 int i;
13930
13931 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13932 NULL, options, WILD_ALL_KEEP);
13933 for (i = 0; i < xpc.xp_numfiles; i++)
13934 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13935
13936 ExpandCleanup(&xpc);
13937 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013938 }
13939 else
13940 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941}
13942
13943/*
13944 * "globpath()" function
13945 */
13946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013947f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013949 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013951 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013952 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013953 garray_T ga;
13954 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013956 /* When the optional second argument is non-zero, don't remove matches
13957 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013959 if (argvars[2].v_type != VAR_UNKNOWN)
13960 {
13961 if (get_tv_number_chk(&argvars[2], &error))
13962 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013963 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013964 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013965 if (get_tv_number_chk(&argvars[3], &error))
13966 {
13967 rettv->v_type = VAR_LIST;
13968 rettv->vval.v_list = NULL;
13969 }
13970 if (argvars[4].v_type != VAR_UNKNOWN
13971 && get_tv_number_chk(&argvars[4], &error))
13972 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013973 }
13974 }
13975 if (file != NULL && !error)
13976 {
13977 ga_init2(&ga, (int)sizeof(char_u *), 10);
13978 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13979 if (rettv->v_type == VAR_STRING)
13980 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13981 else if (rettv_list_alloc(rettv) != FAIL)
13982 for (i = 0; i < ga.ga_len; ++i)
13983 list_append_string(rettv->vval.v_list,
13984 ((char_u **)(ga.ga_data))[i], -1);
13985 ga_clear_strings(&ga);
13986 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013988 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989}
13990
13991/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013992 * "glob2regpat()" function
13993 */
13994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013995f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013996{
13997 char_u *pat = get_tv_string_chk(&argvars[0]);
13998
13999 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010014000 rettv->vval.v_string = (pat == NULL)
14001 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014002}
14003
14004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 * "has()" function
14006 */
14007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014008f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009{
14010 int i;
14011 char_u *name;
14012 int n = FALSE;
14013 static char *(has_list[]) =
14014 {
14015#ifdef AMIGA
14016 "amiga",
14017# ifdef FEAT_ARP
14018 "arp",
14019# endif
14020#endif
14021#ifdef __BEOS__
14022 "beos",
14023#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000014024#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 "mac",
14026#endif
14027#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010014028 "macunix", /* built with 'darwin' enabled */
14029#endif
14030#if defined(__APPLE__) && __APPLE__ == 1
14031 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033#ifdef __QNX__
14034 "qnx",
14035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036#ifdef UNIX
14037 "unix",
14038#endif
14039#ifdef VMS
14040 "vms",
14041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042#ifdef WIN32
14043 "win32",
14044#endif
14045#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
14046 "win32unix",
14047#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010014048#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 "win64",
14050#endif
14051#ifdef EBCDIC
14052 "ebcdic",
14053#endif
14054#ifndef CASE_INSENSITIVE_FILENAME
14055 "fname_case",
14056#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014057#ifdef HAVE_ACL
14058 "acl",
14059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060#ifdef FEAT_ARABIC
14061 "arabic",
14062#endif
14063#ifdef FEAT_AUTOCMD
14064 "autocmd",
14065#endif
14066#ifdef FEAT_BEVAL
14067 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000014068# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
14069 "balloon_multiline",
14070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071#endif
14072#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
14073 "builtin_terms",
14074# ifdef ALL_BUILTIN_TCAPS
14075 "all_builtin_terms",
14076# endif
14077#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020014078#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
14079 || defined(FEAT_GUI_W32) \
14080 || defined(FEAT_GUI_MOTIF))
14081 "browsefilter",
14082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083#ifdef FEAT_BYTEOFF
14084 "byte_offset",
14085#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014086#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010014087 "channel",
14088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089#ifdef FEAT_CINDENT
14090 "cindent",
14091#endif
14092#ifdef FEAT_CLIENTSERVER
14093 "clientserver",
14094#endif
14095#ifdef FEAT_CLIPBOARD
14096 "clipboard",
14097#endif
14098#ifdef FEAT_CMDL_COMPL
14099 "cmdline_compl",
14100#endif
14101#ifdef FEAT_CMDHIST
14102 "cmdline_hist",
14103#endif
14104#ifdef FEAT_COMMENTS
14105 "comments",
14106#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014107#ifdef FEAT_CONCEAL
14108 "conceal",
14109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110#ifdef FEAT_CRYPT
14111 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010014112 "crypt-blowfish",
14113 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114#endif
14115#ifdef FEAT_CSCOPE
14116 "cscope",
14117#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014118#ifdef FEAT_CURSORBIND
14119 "cursorbind",
14120#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014121#ifdef CURSOR_SHAPE
14122 "cursorshape",
14123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124#ifdef DEBUG
14125 "debug",
14126#endif
14127#ifdef FEAT_CON_DIALOG
14128 "dialog_con",
14129#endif
14130#ifdef FEAT_GUI_DIALOG
14131 "dialog_gui",
14132#endif
14133#ifdef FEAT_DIFF
14134 "diff",
14135#endif
14136#ifdef FEAT_DIGRAPHS
14137 "digraphs",
14138#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014139#ifdef FEAT_DIRECTX
14140 "directx",
14141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142#ifdef FEAT_DND
14143 "dnd",
14144#endif
14145#ifdef FEAT_EMACS_TAGS
14146 "emacs_tags",
14147#endif
14148 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014149 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150#ifdef FEAT_SEARCH_EXTRA
14151 "extra_search",
14152#endif
14153#ifdef FEAT_FKMAP
14154 "farsi",
14155#endif
14156#ifdef FEAT_SEARCHPATH
14157 "file_in_path",
14158#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014159#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014160 "filterpipe",
14161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162#ifdef FEAT_FIND_ID
14163 "find_in_path",
14164#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014165#ifdef FEAT_FLOAT
14166 "float",
14167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168#ifdef FEAT_FOLDING
14169 "folding",
14170#endif
14171#ifdef FEAT_FOOTER
14172 "footer",
14173#endif
14174#if !defined(USE_SYSTEM) && defined(UNIX)
14175 "fork",
14176#endif
14177#ifdef FEAT_GETTEXT
14178 "gettext",
14179#endif
14180#ifdef FEAT_GUI
14181 "gui",
14182#endif
14183#ifdef FEAT_GUI_ATHENA
14184# ifdef FEAT_GUI_NEXTAW
14185 "gui_neXtaw",
14186# else
14187 "gui_athena",
14188# endif
14189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190#ifdef FEAT_GUI_GTK
14191 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014192# ifdef USE_GTK3
14193 "gui_gtk3",
14194# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014198#ifdef FEAT_GUI_GNOME
14199 "gui_gnome",
14200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201#ifdef FEAT_GUI_MAC
14202 "gui_mac",
14203#endif
14204#ifdef FEAT_GUI_MOTIF
14205 "gui_motif",
14206#endif
14207#ifdef FEAT_GUI_PHOTON
14208 "gui_photon",
14209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210#ifdef FEAT_GUI_W32
14211 "gui_win32",
14212#endif
14213#ifdef FEAT_HANGULIN
14214 "hangul_input",
14215#endif
14216#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14217 "iconv",
14218#endif
14219#ifdef FEAT_INS_EXPAND
14220 "insert_expand",
14221#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014222#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014223 "job",
14224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225#ifdef FEAT_JUMPLIST
14226 "jumplist",
14227#endif
14228#ifdef FEAT_KEYMAP
14229 "keymap",
14230#endif
14231#ifdef FEAT_LANGMAP
14232 "langmap",
14233#endif
14234#ifdef FEAT_LIBCALL
14235 "libcall",
14236#endif
14237#ifdef FEAT_LINEBREAK
14238 "linebreak",
14239#endif
14240#ifdef FEAT_LISP
14241 "lispindent",
14242#endif
14243#ifdef FEAT_LISTCMDS
14244 "listcmds",
14245#endif
14246#ifdef FEAT_LOCALMAP
14247 "localmap",
14248#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014249#ifdef FEAT_LUA
14250# ifndef DYNAMIC_LUA
14251 "lua",
14252# endif
14253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254#ifdef FEAT_MENU
14255 "menu",
14256#endif
14257#ifdef FEAT_SESSION
14258 "mksession",
14259#endif
14260#ifdef FEAT_MODIFY_FNAME
14261 "modify_fname",
14262#endif
14263#ifdef FEAT_MOUSE
14264 "mouse",
14265#endif
14266#ifdef FEAT_MOUSESHAPE
14267 "mouseshape",
14268#endif
14269#if defined(UNIX) || defined(VMS)
14270# ifdef FEAT_MOUSE_DEC
14271 "mouse_dec",
14272# endif
14273# ifdef FEAT_MOUSE_GPM
14274 "mouse_gpm",
14275# endif
14276# ifdef FEAT_MOUSE_JSB
14277 "mouse_jsbterm",
14278# endif
14279# ifdef FEAT_MOUSE_NET
14280 "mouse_netterm",
14281# endif
14282# ifdef FEAT_MOUSE_PTERM
14283 "mouse_pterm",
14284# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014285# ifdef FEAT_MOUSE_SGR
14286 "mouse_sgr",
14287# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014288# ifdef FEAT_SYSMOUSE
14289 "mouse_sysmouse",
14290# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014291# ifdef FEAT_MOUSE_URXVT
14292 "mouse_urxvt",
14293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294# ifdef FEAT_MOUSE_XTERM
14295 "mouse_xterm",
14296# endif
14297#endif
14298#ifdef FEAT_MBYTE
14299 "multi_byte",
14300#endif
14301#ifdef FEAT_MBYTE_IME
14302 "multi_byte_ime",
14303#endif
14304#ifdef FEAT_MULTI_LANG
14305 "multi_lang",
14306#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014307#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014308#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014309 "mzscheme",
14310#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014311#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014312#ifdef FEAT_NUM64
14313 "num64",
14314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315#ifdef FEAT_OLE
14316 "ole",
14317#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014318 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319#ifdef FEAT_PATH_EXTRA
14320 "path_extra",
14321#endif
14322#ifdef FEAT_PERL
14323#ifndef DYNAMIC_PERL
14324 "perl",
14325#endif
14326#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014327#ifdef FEAT_PERSISTENT_UNDO
14328 "persistent_undo",
14329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330#ifdef FEAT_PYTHON
14331#ifndef DYNAMIC_PYTHON
14332 "python",
14333#endif
14334#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014335#ifdef FEAT_PYTHON3
14336#ifndef DYNAMIC_PYTHON3
14337 "python3",
14338#endif
14339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340#ifdef FEAT_POSTSCRIPT
14341 "postscript",
14342#endif
14343#ifdef FEAT_PRINTER
14344 "printer",
14345#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014346#ifdef FEAT_PROFILE
14347 "profile",
14348#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014349#ifdef FEAT_RELTIME
14350 "reltime",
14351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352#ifdef FEAT_QUICKFIX
14353 "quickfix",
14354#endif
14355#ifdef FEAT_RIGHTLEFT
14356 "rightleft",
14357#endif
14358#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14359 "ruby",
14360#endif
14361#ifdef FEAT_SCROLLBIND
14362 "scrollbind",
14363#endif
14364#ifdef FEAT_CMDL_INFO
14365 "showcmd",
14366 "cmdline_info",
14367#endif
14368#ifdef FEAT_SIGNS
14369 "signs",
14370#endif
14371#ifdef FEAT_SMARTINDENT
14372 "smartindent",
14373#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014374#ifdef STARTUPTIME
14375 "startuptime",
14376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377#ifdef FEAT_STL_OPT
14378 "statusline",
14379#endif
14380#ifdef FEAT_SUN_WORKSHOP
14381 "sun_workshop",
14382#endif
14383#ifdef FEAT_NETBEANS_INTG
14384 "netbeans_intg",
14385#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014386#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014387 "spell",
14388#endif
14389#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 "syntax",
14391#endif
14392#if defined(USE_SYSTEM) || !defined(UNIX)
14393 "system",
14394#endif
14395#ifdef FEAT_TAG_BINS
14396 "tag_binary",
14397#endif
14398#ifdef FEAT_TAG_OLDSTATIC
14399 "tag_old_static",
14400#endif
14401#ifdef FEAT_TAG_ANYWHITE
14402 "tag_any_white",
14403#endif
14404#ifdef FEAT_TCL
14405# ifndef DYNAMIC_TCL
14406 "tcl",
14407# endif
14408#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014409#ifdef FEAT_TERMGUICOLORS
14410 "termguicolors",
14411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412#ifdef TERMINFO
14413 "terminfo",
14414#endif
14415#ifdef FEAT_TERMRESPONSE
14416 "termresponse",
14417#endif
14418#ifdef FEAT_TEXTOBJ
14419 "textobjects",
14420#endif
14421#ifdef HAVE_TGETENT
14422 "tgetent",
14423#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014424#ifdef FEAT_TIMERS
14425 "timers",
14426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427#ifdef FEAT_TITLE
14428 "title",
14429#endif
14430#ifdef FEAT_TOOLBAR
14431 "toolbar",
14432#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014433#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14434 "unnamedplus",
14435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436#ifdef FEAT_USR_CMDS
14437 "user-commands", /* was accidentally included in 5.4 */
14438 "user_commands",
14439#endif
14440#ifdef FEAT_VIMINFO
14441 "viminfo",
14442#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014443#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 "vertsplit",
14445#endif
14446#ifdef FEAT_VIRTUALEDIT
14447 "virtualedit",
14448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450#ifdef FEAT_VISUALEXTRA
14451 "visualextra",
14452#endif
14453#ifdef FEAT_VREPLACE
14454 "vreplace",
14455#endif
14456#ifdef FEAT_WILDIGN
14457 "wildignore",
14458#endif
14459#ifdef FEAT_WILDMENU
14460 "wildmenu",
14461#endif
14462#ifdef FEAT_WINDOWS
14463 "windows",
14464#endif
14465#ifdef FEAT_WAK
14466 "winaltkeys",
14467#endif
14468#ifdef FEAT_WRITEBACKUP
14469 "writebackup",
14470#endif
14471#ifdef FEAT_XIM
14472 "xim",
14473#endif
14474#ifdef FEAT_XFONTSET
14475 "xfontset",
14476#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014477#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014478 "xpm",
14479 "xpm_w32", /* for backward compatibility */
14480#else
14481# if defined(HAVE_XPM)
14482 "xpm",
14483# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485#ifdef USE_XSMP
14486 "xsmp",
14487#endif
14488#ifdef USE_XSMP_INTERACT
14489 "xsmp_interact",
14490#endif
14491#ifdef FEAT_XCLIPBOARD
14492 "xterm_clipboard",
14493#endif
14494#ifdef FEAT_XTERM_SAVE
14495 "xterm_save",
14496#endif
14497#if defined(UNIX) && defined(FEAT_X11)
14498 "X11",
14499#endif
14500 NULL
14501 };
14502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014503 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 for (i = 0; has_list[i] != NULL; ++i)
14505 if (STRICMP(name, has_list[i]) == 0)
14506 {
14507 n = TRUE;
14508 break;
14509 }
14510
14511 if (n == FALSE)
14512 {
14513 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014514 {
14515 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014516 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014517 && vim_isdigit(name[6])
14518 && vim_isdigit(name[8])
14519 && vim_isdigit(name[10]))
14520 {
14521 int major = atoi((char *)name + 6);
14522 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014523
14524 /* Expect "patch-9.9.01234". */
14525 n = (major < VIM_VERSION_MAJOR
14526 || (major == VIM_VERSION_MAJOR
14527 && (minor < VIM_VERSION_MINOR
14528 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014529 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014530 }
14531 else
14532 n = has_patch(atoi((char *)name + 5));
14533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 else if (STRICMP(name, "vim_starting") == 0)
14535 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014536#ifdef FEAT_MBYTE
14537 else if (STRICMP(name, "multi_byte_encoding") == 0)
14538 n = has_mbyte;
14539#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014540#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14541 else if (STRICMP(name, "balloon_multiline") == 0)
14542 n = multiline_balloon_available();
14543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544#ifdef DYNAMIC_TCL
14545 else if (STRICMP(name, "tcl") == 0)
14546 n = tcl_enabled(FALSE);
14547#endif
14548#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14549 else if (STRICMP(name, "iconv") == 0)
14550 n = iconv_enabled(FALSE);
14551#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014552#ifdef DYNAMIC_LUA
14553 else if (STRICMP(name, "lua") == 0)
14554 n = lua_enabled(FALSE);
14555#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014556#ifdef DYNAMIC_MZSCHEME
14557 else if (STRICMP(name, "mzscheme") == 0)
14558 n = mzscheme_enabled(FALSE);
14559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560#ifdef DYNAMIC_RUBY
14561 else if (STRICMP(name, "ruby") == 0)
14562 n = ruby_enabled(FALSE);
14563#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014564#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565#ifdef DYNAMIC_PYTHON
14566 else if (STRICMP(name, "python") == 0)
14567 n = python_enabled(FALSE);
14568#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014569#endif
14570#ifdef FEAT_PYTHON3
14571#ifdef DYNAMIC_PYTHON3
14572 else if (STRICMP(name, "python3") == 0)
14573 n = python3_enabled(FALSE);
14574#endif
14575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576#ifdef DYNAMIC_PERL
14577 else if (STRICMP(name, "perl") == 0)
14578 n = perl_enabled(FALSE);
14579#endif
14580#ifdef FEAT_GUI
14581 else if (STRICMP(name, "gui_running") == 0)
14582 n = (gui.in_use || gui.starting);
14583# ifdef FEAT_GUI_W32
14584 else if (STRICMP(name, "gui_win32s") == 0)
14585 n = gui_is_win32s();
14586# endif
14587# ifdef FEAT_BROWSE
14588 else if (STRICMP(name, "browse") == 0)
14589 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14590# endif
14591#endif
14592#ifdef FEAT_SYN_HL
14593 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014594 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595#endif
14596#if defined(WIN3264)
14597 else if (STRICMP(name, "win95") == 0)
14598 n = mch_windows95();
14599#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014600#ifdef FEAT_NETBEANS_INTG
14601 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014602 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604 }
14605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607}
14608
14609/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014610 * "has_key()" function
14611 */
14612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014613f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014614{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014615 if (argvars[0].v_type != VAR_DICT)
14616 {
14617 EMSG(_(e_dictreq));
14618 return;
14619 }
14620 if (argvars[0].vval.v_dict == NULL)
14621 return;
14622
14623 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014624 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014625}
14626
14627/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014628 * "haslocaldir()" function
14629 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014631f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014632{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014633 win_T *wp = NULL;
14634
14635 wp = find_tabwin(&argvars[0], &argvars[1]);
14636 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014637}
14638
14639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 * "hasmapto()" function
14641 */
14642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014643f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644{
14645 char_u *name;
14646 char_u *mode;
14647 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014648 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014650 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014651 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652 mode = (char_u *)"nvo";
14653 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014655 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014656 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014657 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659
Bram Moolenaar2c932302006-03-18 21:42:09 +000014660 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014661 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664}
14665
14666/*
14667 * "histadd()" function
14668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014670f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671{
14672#ifdef FEAT_CMDHIST
14673 int histype;
14674 char_u *str;
14675 char_u buf[NUMBUFLEN];
14676#endif
14677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679 if (check_restricted() || check_secure())
14680 return;
14681#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014682 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14683 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684 if (histype >= 0)
14685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 if (*str != NUL)
14688 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014689 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014691 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692 return;
14693 }
14694 }
14695#endif
14696}
14697
14698/*
14699 * "histdel()" function
14700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014702f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703{
14704#ifdef FEAT_CMDHIST
14705 int n;
14706 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014707 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014709 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14710 if (str == NULL)
14711 n = 0;
14712 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014714 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014715 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014717 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014718 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 else
14720 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014722 get_tv_string_buf(&argvars[1], buf));
14723 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724#endif
14725}
14726
14727/*
14728 * "histget()" function
14729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014731f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732{
14733#ifdef FEAT_CMDHIST
14734 int type;
14735 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014736 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014738 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14739 if (str == NULL)
14740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 {
14743 type = get_histtype(str);
14744 if (argvars[1].v_type == VAR_UNKNOWN)
14745 idx = get_history_idx(type);
14746 else
14747 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14748 /* -1 on type error */
14749 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755}
14756
14757/*
14758 * "histnr()" function
14759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014761f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762{
14763 int i;
14764
14765#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014766 char_u *history = get_tv_string_chk(&argvars[0]);
14767
14768 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 if (i >= HIST_CMD && i < HIST_COUNT)
14770 i = get_history_idx(i);
14771 else
14772#endif
14773 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775}
14776
14777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778 * "highlightID(name)" function
14779 */
14780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014781f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014783 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784}
14785
14786/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014787 * "highlight_exists()" function
14788 */
14789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014790f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014791{
14792 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14793}
14794
14795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 * "hostname()" function
14797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014799f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800{
14801 char_u hostname[256];
14802
14803 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014804 rettv->v_type = VAR_STRING;
14805 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806}
14807
14808/*
14809 * iconv() function
14810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014812f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813{
14814#ifdef FEAT_MBYTE
14815 char_u buf1[NUMBUFLEN];
14816 char_u buf2[NUMBUFLEN];
14817 char_u *from, *to, *str;
14818 vimconv_T vimconv;
14819#endif
14820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014821 rettv->v_type = VAR_STRING;
14822 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823
14824#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014825 str = get_tv_string(&argvars[0]);
14826 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14827 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 vimconv.vc_type = CONV_NONE;
14829 convert_setup(&vimconv, from, to);
14830
14831 /* If the encodings are equal, no conversion needed. */
14832 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014835 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836
14837 convert_setup(&vimconv, NULL, NULL);
14838 vim_free(from);
14839 vim_free(to);
14840#endif
14841}
14842
14843/*
14844 * "indent()" function
14845 */
14846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014847f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848{
14849 linenr_T lnum;
14850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014851 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014855 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856}
14857
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014858/*
14859 * "index()" function
14860 */
14861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014862f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014863{
Bram Moolenaar33570922005-01-25 22:26:29 +000014864 list_T *l;
14865 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014866 long idx = 0;
14867 int ic = FALSE;
14868
14869 rettv->vval.v_number = -1;
14870 if (argvars[0].v_type != VAR_LIST)
14871 {
14872 EMSG(_(e_listreq));
14873 return;
14874 }
14875 l = argvars[0].vval.v_list;
14876 if (l != NULL)
14877 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014878 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014879 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014881 int error = FALSE;
14882
Bram Moolenaar758711c2005-02-02 23:11:38 +000014883 /* Start at specified item. Use the cached index that list_find()
14884 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014885 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014886 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014887 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014888 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014889 if (error)
14890 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014891 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014892
Bram Moolenaar758711c2005-02-02 23:11:38 +000014893 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014894 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014895 {
14896 rettv->vval.v_number = idx;
14897 break;
14898 }
14899 }
14900}
14901
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902static int inputsecret_flag = 0;
14903
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014904static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014905
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014907 * This function is used by f_input() and f_inputdialog() functions. The third
14908 * argument to f_input() specifies the type of completion to use at the
14909 * prompt. The third argument to f_inputdialog() specifies the value to return
14910 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 */
14912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014913get_user_input(
14914 typval_T *argvars,
14915 typval_T *rettv,
14916 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 char_u *p = NULL;
14920 int c;
14921 char_u buf[NUMBUFLEN];
14922 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014923 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014924 int xp_type = EXPAND_NOTHING;
14925 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014927 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929
14930#ifdef NO_CONSOLE_INPUT
14931 /* While starting up, there is no place to enter text. */
14932 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934#endif
14935
14936 cmd_silent = FALSE; /* Want to see the prompt. */
14937 if (prompt != NULL)
14938 {
14939 /* Only the part of the message after the last NL is considered as
14940 * prompt for the command line */
14941 p = vim_strrchr(prompt, '\n');
14942 if (p == NULL)
14943 p = prompt;
14944 else
14945 {
14946 ++p;
14947 c = *p;
14948 *p = NUL;
14949 msg_start();
14950 msg_clr_eos();
14951 msg_puts_attr(prompt, echo_attr);
14952 msg_didout = FALSE;
14953 msg_starthere();
14954 *p = c;
14955 }
14956 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014958 if (argvars[1].v_type != VAR_UNKNOWN)
14959 {
14960 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14961 if (defstr != NULL)
14962 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014964 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014965 {
14966 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014967 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014968 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014969
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014970 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014971 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014972
Bram Moolenaar4463f292005-09-25 22:20:24 +000014973 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14974 if (xp_name == NULL)
14975 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014976
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014977 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014978
Bram Moolenaar4463f292005-09-25 22:20:24 +000014979 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14980 &xp_arg) == FAIL)
14981 return;
14982 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014983 }
14984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014985 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014986 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014987 int save_ex_normal_busy = ex_normal_busy;
14988 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014989 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014990 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14991 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014992 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014993 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014994 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014995 && argvars[1].v_type != VAR_UNKNOWN
14996 && argvars[2].v_type != VAR_UNKNOWN)
14997 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14998 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014999
15000 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015002 /* since the user typed this, no need to wait for return */
15003 need_wait_return = FALSE;
15004 msg_didout = FALSE;
15005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 cmd_silent = cmd_silent_save;
15007}
15008
15009/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015010 * "input()" function
15011 * Also handles inputsecret() when inputsecret is set.
15012 */
15013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015014f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015015{
15016 get_user_input(argvars, rettv, FALSE);
15017}
15018
15019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 * "inputdialog()" function
15021 */
15022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015023f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024{
15025#if defined(FEAT_GUI_TEXTDIALOG)
15026 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
15027 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
15028 {
15029 char_u *message;
15030 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015031 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015033 message = get_tv_string_chk(&argvars[0]);
15034 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000015035 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000015036 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 else
15038 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015039 if (message != NULL && defstr != NULL
15040 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010015041 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015042 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 else
15044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015045 if (message != NULL && defstr != NULL
15046 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015047 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015048 rettv->vval.v_string = vim_strsave(
15049 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 }
15055 else
15056#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015057 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058}
15059
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015060/*
15061 * "inputlist()" function
15062 */
15063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015064f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015065{
15066 listitem_T *li;
15067 int selected;
15068 int mouse_used;
15069
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015070#ifdef NO_CONSOLE_INPUT
15071 /* While starting up, there is no place to enter text. */
15072 if (no_console_input())
15073 return;
15074#endif
15075 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
15076 {
15077 EMSG2(_(e_listarg), "inputlist()");
15078 return;
15079 }
15080
15081 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000015082 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015083 lines_left = Rows; /* avoid more prompt */
15084 msg_scroll = TRUE;
15085 msg_clr_eos();
15086
15087 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
15088 {
15089 msg_puts(get_tv_string(&li->li_tv));
15090 msg_putchar('\n');
15091 }
15092
15093 /* Ask for choice. */
15094 selected = prompt_for_number(&mouse_used);
15095 if (mouse_used)
15096 selected -= lines_left;
15097
15098 rettv->vval.v_number = selected;
15099}
15100
15101
Bram Moolenaar071d4272004-06-13 20:20:40 +000015102static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
15103
15104/*
15105 * "inputrestore()" function
15106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015108f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109{
15110 if (ga_userinput.ga_len > 0)
15111 {
15112 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
15114 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015115 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 }
15117 else if (p_verbose > 1)
15118 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000015119 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015120 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121 }
15122}
15123
15124/*
15125 * "inputsave()" function
15126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015128f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015130 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 if (ga_grow(&ga_userinput, 1) == OK)
15132 {
15133 save_typeahead((tasave_T *)(ga_userinput.ga_data)
15134 + ga_userinput.ga_len);
15135 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015136 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137 }
15138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140}
15141
15142/*
15143 * "inputsecret()" function
15144 */
15145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015146f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147{
15148 ++cmdline_star;
15149 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015150 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 --cmdline_star;
15152 --inputsecret_flag;
15153}
15154
15155/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015156 * "insert()" function
15157 */
15158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015159f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015160{
15161 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015162 listitem_T *item;
15163 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015164 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015165
15166 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015168 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015169 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015170 {
15171 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015172 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015173 if (error)
15174 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015175
Bram Moolenaar758711c2005-02-02 23:11:38 +000015176 if (before == l->lv_len)
15177 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015178 else
15179 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015180 item = list_find(l, before);
15181 if (item == NULL)
15182 {
15183 EMSGN(_(e_listidx), before);
15184 l = NULL;
15185 }
15186 }
15187 if (l != NULL)
15188 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015189 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015190 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015191 }
15192 }
15193}
15194
15195/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015196 * "invert(expr)" function
15197 */
15198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015199f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015200{
15201 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15202}
15203
15204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 * "isdirectory()" function
15206 */
15207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015208f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015210 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211}
15212
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015213/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015214 * "islocked()" function
15215 */
15216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015217f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015218{
15219 lval_T lv;
15220 char_u *end;
15221 dictitem_T *di;
15222
15223 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015224 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15225 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015226 if (end != NULL && lv.ll_name != NULL)
15227 {
15228 if (*end != NUL)
15229 EMSG(_(e_trailing));
15230 else
15231 {
15232 if (lv.ll_tv == NULL)
15233 {
15234 if (check_changedtick(lv.ll_name))
15235 rettv->vval.v_number = 1; /* always locked */
15236 else
15237 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015238 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015239 if (di != NULL)
15240 {
15241 /* Consider a variable locked when:
15242 * 1. the variable itself is locked
15243 * 2. the value of the variable is locked.
15244 * 3. the List or Dict value is locked.
15245 */
15246 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15247 || tv_islocked(&di->di_tv));
15248 }
15249 }
15250 }
15251 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015252 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015253 else if (lv.ll_newkey != NULL)
15254 EMSG2(_(e_dictkey), lv.ll_newkey);
15255 else if (lv.ll_list != NULL)
15256 /* List item. */
15257 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15258 else
15259 /* Dictionary item. */
15260 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15261 }
15262 }
15263
15264 clear_lval(&lv);
15265}
15266
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015267#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15268/*
15269 * "isnan()" function
15270 */
15271 static void
15272f_isnan(typval_T *argvars, typval_T *rettv)
15273{
15274 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15275 && isnan(argvars[0].vval.v_float);
15276}
15277#endif
15278
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015279static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015280
15281/*
15282 * Turn a dict into a list:
15283 * "what" == 0: list of keys
15284 * "what" == 1: list of values
15285 * "what" == 2: list of items
15286 */
15287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015288dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015289{
Bram Moolenaar33570922005-01-25 22:26:29 +000015290 list_T *l2;
15291 dictitem_T *di;
15292 hashitem_T *hi;
15293 listitem_T *li;
15294 listitem_T *li2;
15295 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015296 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015297
Bram Moolenaar8c711452005-01-14 21:53:12 +000015298 if (argvars[0].v_type != VAR_DICT)
15299 {
15300 EMSG(_(e_dictreq));
15301 return;
15302 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015303 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015304 return;
15305
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015306 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015307 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015308
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015309 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015310 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015312 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015313 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015314 --todo;
15315 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015316
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015317 li = listitem_alloc();
15318 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015319 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015320 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015321
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015322 if (what == 0)
15323 {
15324 /* keys() */
15325 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015326 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015327 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15328 }
15329 else if (what == 1)
15330 {
15331 /* values() */
15332 copy_tv(&di->di_tv, &li->li_tv);
15333 }
15334 else
15335 {
15336 /* items() */
15337 l2 = list_alloc();
15338 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015339 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015340 li->li_tv.vval.v_list = l2;
15341 if (l2 == NULL)
15342 break;
15343 ++l2->lv_refcount;
15344
15345 li2 = listitem_alloc();
15346 if (li2 == NULL)
15347 break;
15348 list_append(l2, li2);
15349 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015350 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015351 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15352
15353 li2 = listitem_alloc();
15354 if (li2 == NULL)
15355 break;
15356 list_append(l2, li2);
15357 copy_tv(&di->di_tv, &li2->li_tv);
15358 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015359 }
15360 }
15361}
15362
15363/*
15364 * "items(dict)" function
15365 */
15366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015367f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015368{
15369 dict_list(argvars, rettv, 2);
15370}
15371
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015372#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015373/*
15374 * Get the job from the argument.
15375 * Returns NULL if the job is invalid.
15376 */
15377 static job_T *
15378get_job_arg(typval_T *tv)
15379{
15380 job_T *job;
15381
15382 if (tv->v_type != VAR_JOB)
15383 {
15384 EMSG2(_(e_invarg2), get_tv_string(tv));
15385 return NULL;
15386 }
15387 job = tv->vval.v_job;
15388
15389 if (job == NULL)
15390 EMSG(_("E916: not a valid job"));
15391 return job;
15392}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015393
Bram Moolenaar835dc632016-02-07 14:27:38 +010015394/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015395 * "job_getchannel()" function
15396 */
15397 static void
15398f_job_getchannel(typval_T *argvars, typval_T *rettv)
15399{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015400 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015401
Bram Moolenaar65edff82016-02-21 16:40:11 +010015402 if (job != NULL)
15403 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015404 rettv->v_type = VAR_CHANNEL;
15405 rettv->vval.v_channel = job->jv_channel;
15406 if (job->jv_channel != NULL)
15407 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015408 }
15409}
15410
15411/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015412 * "job_info()" function
15413 */
15414 static void
15415f_job_info(typval_T *argvars, typval_T *rettv)
15416{
15417 job_T *job = get_job_arg(&argvars[0]);
15418
15419 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15420 job_info(job, rettv->vval.v_dict);
15421}
15422
15423/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015424 * "job_setoptions()" function
15425 */
15426 static void
15427f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15428{
15429 job_T *job = get_job_arg(&argvars[0]);
15430 jobopt_T opt;
15431
15432 if (job == NULL)
15433 return;
15434 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015435 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15436 job_set_options(job, &opt);
15437 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015438}
15439
15440/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015441 * "job_start()" function
15442 */
15443 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015444f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015445{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015446 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015447 if (check_restricted() || check_secure())
15448 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015449 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015450}
15451
15452/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015453 * "job_status()" function
15454 */
15455 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015456f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015457{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015458 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015459
Bram Moolenaar65edff82016-02-21 16:40:11 +010015460 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015461 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015462 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015463 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015464 }
15465}
15466
15467/*
15468 * "job_stop()" function
15469 */
15470 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015471f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015472{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015473 job_T *job = get_job_arg(&argvars[0]);
15474
15475 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015476 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015477}
15478#endif
15479
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015481 * "join()" function
15482 */
15483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015484f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015485{
15486 garray_T ga;
15487 char_u *sep;
15488
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015489 if (argvars[0].v_type != VAR_LIST)
15490 {
15491 EMSG(_(e_listreq));
15492 return;
15493 }
15494 if (argvars[0].vval.v_list == NULL)
15495 return;
15496 if (argvars[1].v_type == VAR_UNKNOWN)
15497 sep = (char_u *)" ";
15498 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015499 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015500
15501 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015502
15503 if (sep != NULL)
15504 {
15505 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015506 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015507 ga_append(&ga, NUL);
15508 rettv->vval.v_string = (char_u *)ga.ga_data;
15509 }
15510 else
15511 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015512}
15513
15514/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015515 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015516 */
15517 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015518f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015519{
15520 js_read_T reader;
15521
15522 reader.js_buf = get_tv_string(&argvars[0]);
15523 reader.js_fill = NULL;
15524 reader.js_used = 0;
15525 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15526 EMSG(_(e_invarg));
15527}
15528
15529/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015530 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015531 */
15532 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015533f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015534{
15535 rettv->v_type = VAR_STRING;
15536 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15537}
15538
15539/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015540 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015541 */
15542 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015543f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015544{
15545 js_read_T reader;
15546
15547 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015548 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015549 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015550 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015551 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015552}
15553
15554/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015555 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015556 */
15557 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015558f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015559{
15560 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015561 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015562}
15563
15564/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015565 * "keys()" function
15566 */
15567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015568f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015569{
15570 dict_list(argvars, rettv, 0);
15571}
15572
15573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 * "last_buffer_nr()" function.
15575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015577f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578{
15579 int n = 0;
15580 buf_T *buf;
15581
15582 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15583 if (n < buf->b_fnum)
15584 n = buf->b_fnum;
15585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015586 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015587}
15588
15589/*
15590 * "len()" function
15591 */
15592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015593f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015594{
15595 switch (argvars[0].v_type)
15596 {
15597 case VAR_STRING:
15598 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015599 rettv->vval.v_number = (varnumber_T)STRLEN(
15600 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015601 break;
15602 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015603 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015604 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015605 case VAR_DICT:
15606 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15607 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015608 case VAR_UNKNOWN:
15609 case VAR_SPECIAL:
15610 case VAR_FLOAT:
15611 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015612 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015613 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015614 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015615 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015616 break;
15617 }
15618}
15619
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015620static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015621
15622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015623libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015624{
15625#ifdef FEAT_LIBCALL
15626 char_u *string_in;
15627 char_u **string_result;
15628 int nr_result;
15629#endif
15630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015631 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015632 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015633 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015634
15635 if (check_restricted() || check_secure())
15636 return;
15637
15638#ifdef FEAT_LIBCALL
15639 /* The first two args must be strings, otherwise its meaningless */
15640 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15641 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015642 string_in = NULL;
15643 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015644 string_in = argvars[2].vval.v_string;
15645 if (type == VAR_NUMBER)
15646 string_result = NULL;
15647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015648 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015649 if (mch_libcall(argvars[0].vval.v_string,
15650 argvars[1].vval.v_string,
15651 string_in,
15652 argvars[2].vval.v_number,
15653 string_result,
15654 &nr_result) == OK
15655 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015656 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015657 }
15658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659}
15660
15661/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015662 * "libcall()" function
15663 */
15664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015665f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015666{
15667 libcall_common(argvars, rettv, VAR_STRING);
15668}
15669
15670/*
15671 * "libcallnr()" function
15672 */
15673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015674f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015675{
15676 libcall_common(argvars, rettv, VAR_NUMBER);
15677}
15678
15679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 * "line(string)" function
15681 */
15682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015683f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684{
15685 linenr_T lnum = 0;
15686 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015687 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015689 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 if (fp != NULL)
15691 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015692 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693}
15694
15695/*
15696 * "line2byte(lnum)" function
15697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015699f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700{
15701#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703#else
15704 linenr_T lnum;
15705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015706 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015708 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015710 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15711 if (rettv->vval.v_number >= 0)
15712 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713#endif
15714}
15715
15716/*
15717 * "lispindent(lnum)" function
15718 */
15719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015720f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721{
15722#ifdef FEAT_LISP
15723 pos_T pos;
15724 linenr_T lnum;
15725
15726 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15729 {
15730 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015731 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 curwin->w_cursor = pos;
15733 }
15734 else
15735#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015736 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737}
15738
15739/*
15740 * "localtime()" function
15741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015743f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015745 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746}
15747
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015748static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749
15750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015751get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752{
15753 char_u *keys;
15754 char_u *which;
15755 char_u buf[NUMBUFLEN];
15756 char_u *keys_buf = NULL;
15757 char_u *rhs;
15758 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015759 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015760 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015761 mapblock_T *mp;
15762 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763
15764 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015765 rettv->v_type = VAR_STRING;
15766 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 if (*keys == NUL)
15770 return;
15771
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015772 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015774 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015775 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015776 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015777 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015778 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015779 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015780 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 else
15783 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015784 if (which == NULL)
15785 return;
15786
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 mode = get_map_mode(&which, 0);
15788
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015789 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015790 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015792
15793 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015795 /* Return a string. */
15796 if (rhs != NULL)
15797 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798
Bram Moolenaarbd743252010-10-20 21:23:33 +020015799 }
15800 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15801 {
15802 /* Return a dictionary. */
15803 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15804 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15805 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806
Bram Moolenaarbd743252010-10-20 21:23:33 +020015807 dict_add_nr_str(dict, "lhs", 0L, lhs);
15808 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15809 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15810 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15811 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15812 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15813 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015814 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015815 dict_add_nr_str(dict, "mode", 0L, mapmode);
15816
15817 vim_free(lhs);
15818 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819 }
15820}
15821
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015822#ifdef FEAT_FLOAT
15823/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015824 * "log()" function
15825 */
15826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015827f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015828{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015829 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015830
15831 rettv->v_type = VAR_FLOAT;
15832 if (get_float_arg(argvars, &f) == OK)
15833 rettv->vval.v_float = log(f);
15834 else
15835 rettv->vval.v_float = 0.0;
15836}
15837
15838/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015839 * "log10()" function
15840 */
15841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015842f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015843{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015844 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015845
15846 rettv->v_type = VAR_FLOAT;
15847 if (get_float_arg(argvars, &f) == OK)
15848 rettv->vval.v_float = log10(f);
15849 else
15850 rettv->vval.v_float = 0.0;
15851}
15852#endif
15853
Bram Moolenaar1dced572012-04-05 16:54:08 +020015854#ifdef FEAT_LUA
15855/*
15856 * "luaeval()" function
15857 */
15858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015859f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015860{
15861 char_u *str;
15862 char_u buf[NUMBUFLEN];
15863
15864 str = get_tv_string_buf(&argvars[0], buf);
15865 do_luaeval(str, argvars + 1, rettv);
15866}
15867#endif
15868
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015870 * "map()" function
15871 */
15872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015873f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015874{
15875 filter_map(argvars, rettv, TRUE);
15876}
15877
15878/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015879 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 */
15881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015882f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015884 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885}
15886
15887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015888 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 */
15890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015891f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015893 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894}
15895
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015896static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897
15898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015899find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015901 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015902 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015903 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 char_u *pat;
15905 regmatch_T regmatch;
15906 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015907 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908 char_u *save_cpo;
15909 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015910 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015911 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015912 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015913 list_T *l = NULL;
15914 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015915 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015916 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917
15918 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15919 save_cpo = p_cpo;
15920 p_cpo = (char_u *)"";
15921
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015922 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015923 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015924 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015925 /* type 3: return empty list when there are no matches.
15926 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015927 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015928 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015929 if (type == 4
15930 && (list_append_string(rettv->vval.v_list,
15931 (char_u *)"", 0) == FAIL
15932 || list_append_number(rettv->vval.v_list,
15933 (varnumber_T)-1) == FAIL
15934 || list_append_number(rettv->vval.v_list,
15935 (varnumber_T)-1) == FAIL
15936 || list_append_number(rettv->vval.v_list,
15937 (varnumber_T)-1) == FAIL))
15938 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015939 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015940 rettv->vval.v_list = NULL;
15941 goto theend;
15942 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015943 }
15944 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015946 rettv->v_type = VAR_STRING;
15947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015950 if (argvars[0].v_type == VAR_LIST)
15951 {
15952 if ((l = argvars[0].vval.v_list) == NULL)
15953 goto theend;
15954 li = l->lv_first;
15955 }
15956 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015957 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015958 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015959 len = (long)STRLEN(str);
15960 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015962 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15963 if (pat == NULL)
15964 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015965
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015966 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015968 int error = FALSE;
15969
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015970 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015971 if (error)
15972 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015973 if (l != NULL)
15974 {
15975 li = list_find(l, start);
15976 if (li == NULL)
15977 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015978 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015979 }
15980 else
15981 {
15982 if (start < 0)
15983 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015984 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015985 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015986 /* When "count" argument is there ignore matches before "start",
15987 * otherwise skip part of the string. Differs when pattern is "^"
15988 * or "\<". */
15989 if (argvars[3].v_type != VAR_UNKNOWN)
15990 startcol = start;
15991 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015992 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015993 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015994 len -= start;
15995 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015996 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015997
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015998 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015999 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016000 if (error)
16001 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002 }
16003
16004 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16005 if (regmatch.regprog != NULL)
16006 {
16007 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016008
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016009 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016010 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016011 if (l != NULL)
16012 {
16013 if (li == NULL)
16014 {
16015 match = FALSE;
16016 break;
16017 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016018 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016019 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016020 if (str == NULL)
16021 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016022 }
16023
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000016024 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016025
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016026 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016027 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016028 if (l == NULL && !match)
16029 break;
16030
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016031 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016032 if (l != NULL)
16033 {
16034 li = li->li_next;
16035 ++idx;
16036 }
16037 else
16038 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016039#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016040 startcol = (colnr_T)(regmatch.startp[0]
16041 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016042#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020016043 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016044#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016045 if (startcol > (colnr_T)len
16046 || str + startcol <= regmatch.startp[0])
16047 {
16048 match = FALSE;
16049 break;
16050 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016051 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016052 }
16053
16054 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016056 if (type == 4)
16057 {
16058 listitem_T *li1 = rettv->vval.v_list->lv_first;
16059 listitem_T *li2 = li1->li_next;
16060 listitem_T *li3 = li2->li_next;
16061 listitem_T *li4 = li3->li_next;
16062
Bram Moolenaar3c809342016-06-01 22:34:48 +020016063 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016064 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
16065 (int)(regmatch.endp[0] - regmatch.startp[0]));
16066 li3->li_tv.vval.v_number =
16067 (varnumber_T)(regmatch.startp[0] - expr);
16068 li4->li_tv.vval.v_number =
16069 (varnumber_T)(regmatch.endp[0] - expr);
16070 if (l != NULL)
16071 li2->li_tv.vval.v_number = (varnumber_T)idx;
16072 }
16073 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016074 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016075 int i;
16076
16077 /* return list with matched string and submatches */
16078 for (i = 0; i < NSUBEXP; ++i)
16079 {
16080 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016081 {
16082 if (list_append_string(rettv->vval.v_list,
16083 (char_u *)"", 0) == FAIL)
16084 break;
16085 }
16086 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016087 regmatch.startp[i],
16088 (int)(regmatch.endp[i] - regmatch.startp[i]))
16089 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016090 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016091 }
16092 }
16093 else if (type == 2)
16094 {
16095 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016096 if (l != NULL)
16097 copy_tv(&li->li_tv, rettv);
16098 else
16099 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016101 }
16102 else if (l != NULL)
16103 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 else
16105 {
16106 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016107 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 (varnumber_T)(regmatch.startp[0] - str);
16109 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016110 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016112 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 }
16114 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016115 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 }
16117
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016118 if (type == 4 && l == NULL)
16119 /* matchstrpos() without a list: drop the second item. */
16120 listitem_remove(rettv->vval.v_list,
16121 rettv->vval.v_list->lv_first->li_next);
16122
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016124 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 p_cpo = save_cpo;
16126}
16127
16128/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129 * "match()" function
16130 */
16131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016132f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016133{
16134 find_some_match(argvars, rettv, 1);
16135}
16136
16137/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016138 * "matchadd()" function
16139 */
16140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016141f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016142{
16143#ifdef FEAT_SEARCH_EXTRA
16144 char_u buf[NUMBUFLEN];
16145 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16146 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16147 int prio = 10; /* default priority */
16148 int id = -1;
16149 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016150 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016151
16152 rettv->vval.v_number = -1;
16153
16154 if (grp == NULL || pat == NULL)
16155 return;
16156 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016157 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016158 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016159 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016160 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016161 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016162 if (argvars[4].v_type != VAR_UNKNOWN)
16163 {
16164 if (argvars[4].v_type != VAR_DICT)
16165 {
16166 EMSG(_(e_dictreq));
16167 return;
16168 }
16169 if (dict_find(argvars[4].vval.v_dict,
16170 (char_u *)"conceal", -1) != NULL)
16171 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16172 (char_u *)"conceal", FALSE);
16173 }
16174 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016175 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016176 if (error == TRUE)
16177 return;
16178 if (id >= 1 && id <= 3)
16179 {
16180 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16181 return;
16182 }
16183
Bram Moolenaar6561d522015-07-21 15:48:27 +020016184 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16185 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016186#endif
16187}
16188
16189/*
16190 * "matchaddpos()" function
16191 */
16192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016193f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016194{
16195#ifdef FEAT_SEARCH_EXTRA
16196 char_u buf[NUMBUFLEN];
16197 char_u *group;
16198 int prio = 10;
16199 int id = -1;
16200 int error = FALSE;
16201 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016202 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016203
16204 rettv->vval.v_number = -1;
16205
16206 group = get_tv_string_buf_chk(&argvars[0], buf);
16207 if (group == NULL)
16208 return;
16209
16210 if (argvars[1].v_type != VAR_LIST)
16211 {
16212 EMSG2(_(e_listarg), "matchaddpos()");
16213 return;
16214 }
16215 l = argvars[1].vval.v_list;
16216 if (l == NULL)
16217 return;
16218
16219 if (argvars[2].v_type != VAR_UNKNOWN)
16220 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016221 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016222 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016223 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016224 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016225 if (argvars[4].v_type != VAR_UNKNOWN)
16226 {
16227 if (argvars[4].v_type != VAR_DICT)
16228 {
16229 EMSG(_(e_dictreq));
16230 return;
16231 }
16232 if (dict_find(argvars[4].vval.v_dict,
16233 (char_u *)"conceal", -1) != NULL)
16234 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16235 (char_u *)"conceal", FALSE);
16236 }
16237 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016238 }
16239 if (error == TRUE)
16240 return;
16241
16242 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16243 if (id == 1 || id == 2)
16244 {
16245 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16246 return;
16247 }
16248
Bram Moolenaar6561d522015-07-21 15:48:27 +020016249 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16250 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016251#endif
16252}
16253
16254/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016255 * "matcharg()" function
16256 */
16257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016258f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016259{
16260 if (rettv_list_alloc(rettv) == OK)
16261 {
16262#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016263 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016264 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016265
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016266 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016267 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016268 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16269 {
16270 list_append_string(rettv->vval.v_list,
16271 syn_id2name(m->hlg_id), -1);
16272 list_append_string(rettv->vval.v_list, m->pattern, -1);
16273 }
16274 else
16275 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016276 list_append_string(rettv->vval.v_list, NULL, -1);
16277 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016278 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016279 }
16280#endif
16281 }
16282}
16283
16284/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016285 * "matchdelete()" function
16286 */
16287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016288f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016289{
16290#ifdef FEAT_SEARCH_EXTRA
16291 rettv->vval.v_number = match_delete(curwin,
16292 (int)get_tv_number(&argvars[0]), TRUE);
16293#endif
16294}
16295
16296/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297 * "matchend()" function
16298 */
16299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016300f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301{
16302 find_some_match(argvars, rettv, 0);
16303}
16304
16305/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016306 * "matchlist()" function
16307 */
16308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016309f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016310{
16311 find_some_match(argvars, rettv, 3);
16312}
16313
16314/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016315 * "matchstr()" function
16316 */
16317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016318f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016319{
16320 find_some_match(argvars, rettv, 2);
16321}
16322
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016323/*
16324 * "matchstrpos()" function
16325 */
16326 static void
16327f_matchstrpos(typval_T *argvars, typval_T *rettv)
16328{
16329 find_some_match(argvars, rettv, 4);
16330}
16331
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016332static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016333
16334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016335max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016336{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016337 varnumber_T n = 0;
16338 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016339 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016340
16341 if (argvars[0].v_type == VAR_LIST)
16342 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 list_T *l;
16344 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016345
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016346 l = argvars[0].vval.v_list;
16347 if (l != NULL)
16348 {
16349 li = l->lv_first;
16350 if (li != NULL)
16351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016352 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016353 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016354 {
16355 li = li->li_next;
16356 if (li == NULL)
16357 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016358 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016359 if (domax ? i > n : i < n)
16360 n = i;
16361 }
16362 }
16363 }
16364 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016365 else if (argvars[0].v_type == VAR_DICT)
16366 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016367 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016368 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016369 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016370 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016371
16372 d = argvars[0].vval.v_dict;
16373 if (d != NULL)
16374 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016375 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016376 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016377 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016378 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016379 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016380 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016381 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016382 if (first)
16383 {
16384 n = i;
16385 first = FALSE;
16386 }
16387 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016388 n = i;
16389 }
16390 }
16391 }
16392 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016393 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016394 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016395 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016396}
16397
16398/*
16399 * "max()" function
16400 */
16401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016402f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016403{
16404 max_min(argvars, rettv, TRUE);
16405}
16406
16407/*
16408 * "min()" function
16409 */
16410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016411f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016412{
16413 max_min(argvars, rettv, FALSE);
16414}
16415
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016416static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016417
16418/*
16419 * Create the directory in which "dir" is located, and higher levels when
16420 * needed.
16421 */
16422 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016423mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016424{
16425 char_u *p;
16426 char_u *updir;
16427 int r = FAIL;
16428
16429 /* Get end of directory name in "dir".
16430 * We're done when it's "/" or "c:/". */
16431 p = gettail_sep(dir);
16432 if (p <= get_past_head(dir))
16433 return OK;
16434
16435 /* If the directory exists we're done. Otherwise: create it.*/
16436 updir = vim_strnsave(dir, (int)(p - dir));
16437 if (updir == NULL)
16438 return FAIL;
16439 if (mch_isdir(updir))
16440 r = OK;
16441 else if (mkdir_recurse(updir, prot) == OK)
16442 r = vim_mkdir_emsg(updir, prot);
16443 vim_free(updir);
16444 return r;
16445}
16446
16447#ifdef vim_mkdir
16448/*
16449 * "mkdir()" function
16450 */
16451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016452f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016453{
16454 char_u *dir;
16455 char_u buf[NUMBUFLEN];
16456 int prot = 0755;
16457
16458 rettv->vval.v_number = FAIL;
16459 if (check_restricted() || check_secure())
16460 return;
16461
16462 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016463 if (*dir == NUL)
16464 rettv->vval.v_number = FAIL;
16465 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016466 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016467 if (*gettail(dir) == NUL)
16468 /* remove trailing slashes */
16469 *gettail_sep(dir) = NUL;
16470
16471 if (argvars[1].v_type != VAR_UNKNOWN)
16472 {
16473 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016474 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016475 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16476 mkdir_recurse(dir, prot);
16477 }
16478 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016479 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016480}
16481#endif
16482
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484 * "mode()" function
16485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016487f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016489 char_u buf[3];
16490
16491 buf[1] = NUL;
16492 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016494 if (time_for_testing == 93784)
16495 {
16496 /* Testing the two-character code. */
16497 buf[0] = 'x';
16498 buf[1] = '!';
16499 }
16500 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 {
16502 if (VIsual_select)
16503 buf[0] = VIsual_mode + 's' - 'v';
16504 else
16505 buf[0] = VIsual_mode;
16506 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016507 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016508 || State == CONFIRM)
16509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016511 if (State == ASKMORE)
16512 buf[1] = 'm';
16513 else if (State == CONFIRM)
16514 buf[1] = '?';
16515 }
16516 else if (State == EXTERNCMD)
16517 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 else if (State & INSERT)
16519 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016520#ifdef FEAT_VREPLACE
16521 if (State & VREPLACE_FLAG)
16522 {
16523 buf[0] = 'R';
16524 buf[1] = 'v';
16525 }
16526 else
16527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 if (State & REPLACE_FLAG)
16529 buf[0] = 'R';
16530 else
16531 buf[0] = 'i';
16532 }
16533 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016536 if (exmode_active)
16537 buf[1] = 'v';
16538 }
16539 else if (exmode_active)
16540 {
16541 buf[0] = 'c';
16542 buf[1] = 'e';
16543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016547 if (finish_op)
16548 buf[1] = 'o';
16549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016551 /* Clear out the minor mode when the argument is not a non-zero number or
16552 * non-empty string. */
16553 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016554 buf[1] = NUL;
16555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016556 rettv->vval.v_string = vim_strsave(buf);
16557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558}
16559
Bram Moolenaar429fa852013-04-15 12:27:36 +020016560#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016561/*
16562 * "mzeval()" function
16563 */
16564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016565f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016566{
16567 char_u *str;
16568 char_u buf[NUMBUFLEN];
16569
16570 str = get_tv_string_buf(&argvars[0], buf);
16571 do_mzeval(str, rettv);
16572}
Bram Moolenaar75676462013-01-30 14:55:42 +010016573
16574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016575mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016576{
16577 typval_T argvars[3];
16578
16579 argvars[0].v_type = VAR_STRING;
16580 argvars[0].vval.v_string = name;
16581 copy_tv(args, &argvars[1]);
16582 argvars[2].v_type = VAR_UNKNOWN;
16583 f_call(argvars, rettv);
16584 clear_tv(&argvars[1]);
16585}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016586#endif
16587
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016589 * "nextnonblank()" function
16590 */
16591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016592f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016593{
16594 linenr_T lnum;
16595
16596 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016598 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016599 {
16600 lnum = 0;
16601 break;
16602 }
16603 if (*skipwhite(ml_get(lnum)) != NUL)
16604 break;
16605 }
16606 rettv->vval.v_number = lnum;
16607}
16608
16609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610 * "nr2char()" function
16611 */
16612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016613f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614{
16615 char_u buf[NUMBUFLEN];
16616
16617#ifdef FEAT_MBYTE
16618 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016619 {
16620 int utf8 = 0;
16621
16622 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016623 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016624 if (utf8)
16625 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16626 else
16627 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 else
16630#endif
16631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016632 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 buf[1] = NUL;
16634 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016635 rettv->v_type = VAR_STRING;
16636 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016637}
16638
16639/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016640 * "or(expr, expr)" function
16641 */
16642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016643f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016644{
16645 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16646 | get_tv_number_chk(&argvars[1], NULL);
16647}
16648
16649/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016650 * "pathshorten()" function
16651 */
16652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016653f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016654{
16655 char_u *p;
16656
16657 rettv->v_type = VAR_STRING;
16658 p = get_tv_string_chk(&argvars[0]);
16659 if (p == NULL)
16660 rettv->vval.v_string = NULL;
16661 else
16662 {
16663 p = vim_strsave(p);
16664 rettv->vval.v_string = p;
16665 if (p != NULL)
16666 shorten_dir(p);
16667 }
16668}
16669
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016670#ifdef FEAT_PERL
16671/*
16672 * "perleval()" function
16673 */
16674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016675f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016676{
16677 char_u *str;
16678 char_u buf[NUMBUFLEN];
16679
16680 str = get_tv_string_buf(&argvars[0], buf);
16681 do_perleval(str, rettv);
16682}
16683#endif
16684
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016685#ifdef FEAT_FLOAT
16686/*
16687 * "pow()" function
16688 */
16689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016690f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016691{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016692 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016693
16694 rettv->v_type = VAR_FLOAT;
16695 if (get_float_arg(argvars, &fx) == OK
16696 && get_float_arg(&argvars[1], &fy) == OK)
16697 rettv->vval.v_float = pow(fx, fy);
16698 else
16699 rettv->vval.v_float = 0.0;
16700}
16701#endif
16702
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016703/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016704 * "prevnonblank()" function
16705 */
16706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016707f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016708{
16709 linenr_T lnum;
16710
16711 lnum = get_tv_lnum(argvars);
16712 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16713 lnum = 0;
16714 else
16715 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16716 --lnum;
16717 rettv->vval.v_number = lnum;
16718}
16719
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016720/* This dummy va_list is here because:
16721 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16722 * - locally in the function results in a "used before set" warning
16723 * - using va_start() to initialize it gives "function with fixed args" error */
16724static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016725
Bram Moolenaar8c711452005-01-14 21:53:12 +000016726/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016727 * "printf()" function
16728 */
16729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016730f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016731{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016732 char_u buf[NUMBUFLEN];
16733 int len;
16734 char_u *s;
16735 int saved_did_emsg = did_emsg;
16736 char *fmt;
16737
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016738 rettv->v_type = VAR_STRING;
16739 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016740
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016741 /* Get the required length, allocate the buffer and do it for real. */
16742 did_emsg = FALSE;
16743 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16744 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16745 if (!did_emsg)
16746 {
16747 s = alloc(len + 1);
16748 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016749 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016750 rettv->vval.v_string = s;
16751 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016752 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016753 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016754 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016755}
16756
16757/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016758 * "pumvisible()" function
16759 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016761f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016762{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016763#ifdef FEAT_INS_EXPAND
16764 if (pum_visible())
16765 rettv->vval.v_number = 1;
16766#endif
16767}
16768
Bram Moolenaardb913952012-06-29 12:54:53 +020016769#ifdef FEAT_PYTHON3
16770/*
16771 * "py3eval()" function
16772 */
16773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016774f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016775{
16776 char_u *str;
16777 char_u buf[NUMBUFLEN];
16778
16779 str = get_tv_string_buf(&argvars[0], buf);
16780 do_py3eval(str, rettv);
16781}
16782#endif
16783
16784#ifdef FEAT_PYTHON
16785/*
16786 * "pyeval()" function
16787 */
16788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016789f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016790{
16791 char_u *str;
16792 char_u buf[NUMBUFLEN];
16793
16794 str = get_tv_string_buf(&argvars[0], buf);
16795 do_pyeval(str, rettv);
16796}
16797#endif
16798
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016799/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016800 * "range()" function
16801 */
16802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016803f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016804{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016805 varnumber_T start;
16806 varnumber_T end;
16807 varnumber_T stride = 1;
16808 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016809 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016811 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016812 if (argvars[1].v_type == VAR_UNKNOWN)
16813 {
16814 end = start - 1;
16815 start = 0;
16816 }
16817 else
16818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016819 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016820 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016821 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016822 }
16823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016824 if (error)
16825 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016826 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016827 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016828 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016829 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016830 else
16831 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016832 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016833 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016834 if (list_append_number(rettv->vval.v_list,
16835 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016836 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016837 }
16838}
16839
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016840/*
16841 * "readfile()" function
16842 */
16843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016844f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016845{
16846 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016847 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016848 char_u *fname;
16849 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016850 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16851 int io_size = sizeof(buf);
16852 int readlen; /* size of last fread() */
16853 char_u *prev = NULL; /* previously read bytes, if any */
16854 long prevlen = 0; /* length of data in prev */
16855 long prevsize = 0; /* size of prev buffer */
16856 long maxline = MAXLNUM;
16857 long cnt = 0;
16858 char_u *p; /* position in buf */
16859 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016860
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016861 if (argvars[1].v_type != VAR_UNKNOWN)
16862 {
16863 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16864 binary = TRUE;
16865 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016866 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016867 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016868
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016869 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016870 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016871
16872 /* Always open the file in binary mode, library functions have a mind of
16873 * their own about CR-LF conversion. */
16874 fname = get_tv_string(&argvars[0]);
16875 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16876 {
16877 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16878 return;
16879 }
16880
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016881 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016882 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016883 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016884
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016885 /* This for loop processes what was read, but is also entered at end
16886 * of file so that either:
16887 * - an incomplete line gets written
16888 * - a "binary" file gets an empty line at the end if it ends in a
16889 * newline. */
16890 for (p = buf, start = buf;
16891 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16892 ++p)
16893 {
16894 if (*p == '\n' || readlen <= 0)
16895 {
16896 listitem_T *li;
16897 char_u *s = NULL;
16898 long_u len = p - start;
16899
16900 /* Finished a line. Remove CRs before NL. */
16901 if (readlen > 0 && !binary)
16902 {
16903 while (len > 0 && start[len - 1] == '\r')
16904 --len;
16905 /* removal may cross back to the "prev" string */
16906 if (len == 0)
16907 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16908 --prevlen;
16909 }
16910 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016911 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016912 else
16913 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016914 /* Change "prev" buffer to be the right size. This way
16915 * the bytes are only copied once, and very long lines are
16916 * allocated only once. */
16917 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016918 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016919 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016920 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016921 prev = NULL; /* the list will own the string */
16922 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016923 }
16924 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016925 if (s == NULL)
16926 {
16927 do_outofmem_msg((long_u) prevlen + len + 1);
16928 failed = TRUE;
16929 break;
16930 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016931
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016932 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016933 {
16934 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016935 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016936 break;
16937 }
16938 li->li_tv.v_type = VAR_STRING;
16939 li->li_tv.v_lock = 0;
16940 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016941 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016942
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016943 start = p + 1; /* step over newline */
16944 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016945 break;
16946 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016947 else if (*p == NUL)
16948 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016949#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016950 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16951 * when finding the BF and check the previous two bytes. */
16952 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016953 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016954 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16955 * + 1, these may be in the "prev" string. */
16956 char_u back1 = p >= buf + 1 ? p[-1]
16957 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16958 char_u back2 = p >= buf + 2 ? p[-2]
16959 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16960 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016961
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016962 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016963 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016964 char_u *dest = p - 2;
16965
16966 /* Usually a BOM is at the beginning of a file, and so at
16967 * the beginning of a line; then we can just step over it.
16968 */
16969 if (start == dest)
16970 start = p + 1;
16971 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016972 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016973 /* have to shuffle buf to close gap */
16974 int adjust_prevlen = 0;
16975
16976 if (dest < buf)
16977 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016978 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016979 dest = buf;
16980 }
16981 if (readlen > p - buf + 1)
16982 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16983 readlen -= 3 - adjust_prevlen;
16984 prevlen -= adjust_prevlen;
16985 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016986 }
16987 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016988 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016989#endif
16990 } /* for */
16991
16992 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16993 break;
16994 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016995 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016996 /* There's part of a line in buf, store it in "prev". */
16997 if (p - start + prevlen >= prevsize)
16998 {
16999 /* need bigger "prev" buffer */
17000 char_u *newprev;
17001
17002 /* A common use case is ordinary text files and "prev" gets a
17003 * fragment of a line, so the first allocation is made
17004 * small, to avoid repeatedly 'allocing' large and
17005 * 'reallocing' small. */
17006 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017007 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017008 else
17009 {
17010 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017011 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017012 prevsize = grow50pc > growmin ? grow50pc : growmin;
17013 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020017014 newprev = prev == NULL ? alloc(prevsize)
17015 : vim_realloc(prev, prevsize);
17016 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017017 {
17018 do_outofmem_msg((long_u)prevsize);
17019 failed = TRUE;
17020 break;
17021 }
17022 prev = newprev;
17023 }
17024 /* Add the line part to end of "prev". */
17025 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017026 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017027 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017028 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017029
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017030 /*
17031 * For a negative line count use only the lines at the end of the file,
17032 * free the rest.
17033 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017034 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017035 while (cnt > -maxline)
17036 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017037 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017038 --cnt;
17039 }
17040
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017041 if (failed)
17042 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020017043 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017044 /* readfile doc says an empty list is returned on error */
17045 rettv->vval.v_list = list_alloc();
17046 }
17047
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017048 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017049 fclose(fd);
17050}
17051
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017052#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017053static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017054
17055/*
17056 * Convert a List to proftime_T.
17057 * Return FAIL when there is something wrong.
17058 */
17059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017060list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017061{
17062 long n1, n2;
17063 int error = FALSE;
17064
17065 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
17066 || arg->vval.v_list->lv_len != 2)
17067 return FAIL;
17068 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
17069 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
17070# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017071 tm->HighPart = n1;
17072 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017073# else
17074 tm->tv_sec = n1;
17075 tm->tv_usec = n2;
17076# endif
17077 return error ? FAIL : OK;
17078}
17079#endif /* FEAT_RELTIME */
17080
17081/*
17082 * "reltime()" function
17083 */
17084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017085f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017086{
17087#ifdef FEAT_RELTIME
17088 proftime_T res;
17089 proftime_T start;
17090
17091 if (argvars[0].v_type == VAR_UNKNOWN)
17092 {
17093 /* No arguments: get current time. */
17094 profile_start(&res);
17095 }
17096 else if (argvars[1].v_type == VAR_UNKNOWN)
17097 {
17098 if (list2proftime(&argvars[0], &res) == FAIL)
17099 return;
17100 profile_end(&res);
17101 }
17102 else
17103 {
17104 /* Two arguments: compute the difference. */
17105 if (list2proftime(&argvars[0], &start) == FAIL
17106 || list2proftime(&argvars[1], &res) == FAIL)
17107 return;
17108 profile_sub(&res, &start);
17109 }
17110
17111 if (rettv_list_alloc(rettv) == OK)
17112 {
17113 long n1, n2;
17114
17115# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017116 n1 = res.HighPart;
17117 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017118# else
17119 n1 = res.tv_sec;
17120 n2 = res.tv_usec;
17121# endif
17122 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17123 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17124 }
17125#endif
17126}
17127
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017128#ifdef FEAT_FLOAT
17129/*
17130 * "reltimefloat()" function
17131 */
17132 static void
17133f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17134{
17135# ifdef FEAT_RELTIME
17136 proftime_T tm;
17137# endif
17138
17139 rettv->v_type = VAR_FLOAT;
17140 rettv->vval.v_float = 0;
17141# ifdef FEAT_RELTIME
17142 if (list2proftime(&argvars[0], &tm) == OK)
17143 rettv->vval.v_float = profile_float(&tm);
17144# endif
17145}
17146#endif
17147
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017148/*
17149 * "reltimestr()" function
17150 */
17151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017152f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017153{
17154#ifdef FEAT_RELTIME
17155 proftime_T tm;
17156#endif
17157
17158 rettv->v_type = VAR_STRING;
17159 rettv->vval.v_string = NULL;
17160#ifdef FEAT_RELTIME
17161 if (list2proftime(&argvars[0], &tm) == OK)
17162 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17163#endif
17164}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017165
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017167static void make_connection(void);
17168static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017169
17170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017171make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017172{
17173 if (X_DISPLAY == NULL
17174# ifdef FEAT_GUI
17175 && !gui.in_use
17176# endif
17177 )
17178 {
17179 x_force_connect = TRUE;
17180 setup_term_clip();
17181 x_force_connect = FALSE;
17182 }
17183}
17184
17185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017186check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017187{
17188 make_connection();
17189 if (X_DISPLAY == NULL)
17190 {
17191 EMSG(_("E240: No connection to Vim server"));
17192 return FAIL;
17193 }
17194 return OK;
17195}
17196#endif
17197
17198#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017200remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201{
17202 char_u *server_name;
17203 char_u *keys;
17204 char_u *r = NULL;
17205 char_u buf[NUMBUFLEN];
17206# ifdef WIN32
17207 HWND w;
17208# else
17209 Window w;
17210# endif
17211
17212 if (check_restricted() || check_secure())
17213 return;
17214
17215# ifdef FEAT_X11
17216 if (check_connection() == FAIL)
17217 return;
17218# endif
17219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017220 server_name = get_tv_string_chk(&argvars[0]);
17221 if (server_name == NULL)
17222 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017223 keys = get_tv_string_buf(&argvars[1], buf);
17224# ifdef WIN32
17225 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17226# else
17227 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17228 < 0)
17229# endif
17230 {
17231 if (r != NULL)
17232 EMSG(r); /* sending worked but evaluation failed */
17233 else
17234 EMSG2(_("E241: Unable to send to %s"), server_name);
17235 return;
17236 }
17237
17238 rettv->vval.v_string = r;
17239
17240 if (argvars[2].v_type != VAR_UNKNOWN)
17241 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017242 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017243 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017244 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017246 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017247 v.di_tv.v_type = VAR_STRING;
17248 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017249 idvar = get_tv_string_chk(&argvars[2]);
17250 if (idvar != NULL)
17251 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017252 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017253 }
17254}
17255#endif
17256
17257/*
17258 * "remote_expr()" function
17259 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017261f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017262{
17263 rettv->v_type = VAR_STRING;
17264 rettv->vval.v_string = NULL;
17265#ifdef FEAT_CLIENTSERVER
17266 remote_common(argvars, rettv, TRUE);
17267#endif
17268}
17269
17270/*
17271 * "remote_foreground()" function
17272 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017274f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017275{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017276#ifdef FEAT_CLIENTSERVER
17277# ifdef WIN32
17278 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 {
17280 char_u *server_name = get_tv_string_chk(&argvars[0]);
17281
17282 if (server_name != NULL)
17283 serverForeground(server_name);
17284 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017285# else
17286 /* Send a foreground() expression to the server. */
17287 argvars[1].v_type = VAR_STRING;
17288 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17289 argvars[2].v_type = VAR_UNKNOWN;
17290 remote_common(argvars, rettv, TRUE);
17291 vim_free(argvars[1].vval.v_string);
17292# endif
17293#endif
17294}
17295
Bram Moolenaar0d660222005-01-07 21:51:51 +000017296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017297f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017298{
17299#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017300 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301 char_u *s = NULL;
17302# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017303 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017304# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017305 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017306
17307 if (check_restricted() || check_secure())
17308 {
17309 rettv->vval.v_number = -1;
17310 return;
17311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017312 serverid = get_tv_string_chk(&argvars[0]);
17313 if (serverid == NULL)
17314 {
17315 rettv->vval.v_number = -1;
17316 return; /* type error; errmsg already given */
17317 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017318# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017319 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017320 if (n == 0)
17321 rettv->vval.v_number = -1;
17322 else
17323 {
17324 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17325 rettv->vval.v_number = (s != NULL);
17326 }
17327# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017328 if (check_connection() == FAIL)
17329 return;
17330
17331 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017332 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017333# endif
17334
17335 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017337 char_u *retvar;
17338
Bram Moolenaar33570922005-01-25 22:26:29 +000017339 v.di_tv.v_type = VAR_STRING;
17340 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017341 retvar = get_tv_string_chk(&argvars[1]);
17342 if (retvar != NULL)
17343 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017344 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017345 }
17346#else
17347 rettv->vval.v_number = -1;
17348#endif
17349}
17350
Bram Moolenaar0d660222005-01-07 21:51:51 +000017351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017352f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017353{
17354 char_u *r = NULL;
17355
17356#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017357 char_u *serverid = get_tv_string_chk(&argvars[0]);
17358
17359 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017360 {
17361# ifdef WIN32
17362 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017363 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017364
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017365 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017366 if (n != 0)
17367 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17368 if (r == NULL)
17369# else
17370 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017372# endif
17373 EMSG(_("E277: Unable to read a server reply"));
17374 }
17375#endif
17376 rettv->v_type = VAR_STRING;
17377 rettv->vval.v_string = r;
17378}
17379
17380/*
17381 * "remote_send()" function
17382 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017384f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017385{
17386 rettv->v_type = VAR_STRING;
17387 rettv->vval.v_string = NULL;
17388#ifdef FEAT_CLIENTSERVER
17389 remote_common(argvars, rettv, FALSE);
17390#endif
17391}
17392
17393/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017394 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017395 */
17396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017397f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017398{
Bram Moolenaar33570922005-01-25 22:26:29 +000017399 list_T *l;
17400 listitem_T *item, *item2;
17401 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017402 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017403 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017404 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017405 dict_T *d;
17406 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017407 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017408
Bram Moolenaar8c711452005-01-14 21:53:12 +000017409 if (argvars[0].v_type == VAR_DICT)
17410 {
17411 if (argvars[2].v_type != VAR_UNKNOWN)
17412 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017413 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017414 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017416 key = get_tv_string_chk(&argvars[1]);
17417 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017419 di = dict_find(d, key, -1);
17420 if (di == NULL)
17421 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017422 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17423 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017424 {
17425 *rettv = di->di_tv;
17426 init_tv(&di->di_tv);
17427 dictitem_remove(d, di);
17428 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017430 }
17431 }
17432 else if (argvars[0].v_type != VAR_LIST)
17433 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017434 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017435 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017437 int error = FALSE;
17438
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017439 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017440 if (error)
17441 ; /* type error: do nothing, errmsg already given */
17442 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017443 EMSGN(_(e_listidx), idx);
17444 else
17445 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017446 if (argvars[2].v_type == VAR_UNKNOWN)
17447 {
17448 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017449 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017450 *rettv = item->li_tv;
17451 vim_free(item);
17452 }
17453 else
17454 {
17455 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017456 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017457 if (error)
17458 ; /* type error: do nothing */
17459 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017460 EMSGN(_(e_listidx), end);
17461 else
17462 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017463 int cnt = 0;
17464
17465 for (li = item; li != NULL; li = li->li_next)
17466 {
17467 ++cnt;
17468 if (li == item2)
17469 break;
17470 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017471 if (li == NULL) /* didn't find "item2" after "item" */
17472 EMSG(_(e_invrange));
17473 else
17474 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017475 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017476 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017477 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017478 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017479 l->lv_first = item;
17480 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017481 item->li_prev = NULL;
17482 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017483 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017484 }
17485 }
17486 }
17487 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017488 }
17489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490}
17491
17492/*
17493 * "rename({from}, {to})" function
17494 */
17495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017496f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497{
17498 char_u buf[NUMBUFLEN];
17499
17500 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017503 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17504 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505}
17506
17507/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017508 * "repeat()" function
17509 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017511f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017512{
17513 char_u *p;
17514 int n;
17515 int slen;
17516 int len;
17517 char_u *r;
17518 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017519
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017520 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017521 if (argvars[0].v_type == VAR_LIST)
17522 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017523 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017524 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017525 if (list_extend(rettv->vval.v_list,
17526 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017527 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017528 }
17529 else
17530 {
17531 p = get_tv_string(&argvars[0]);
17532 rettv->v_type = VAR_STRING;
17533 rettv->vval.v_string = NULL;
17534
17535 slen = (int)STRLEN(p);
17536 len = slen * n;
17537 if (len <= 0)
17538 return;
17539
17540 r = alloc(len + 1);
17541 if (r != NULL)
17542 {
17543 for (i = 0; i < n; i++)
17544 mch_memmove(r + i * slen, p, (size_t)slen);
17545 r[len] = NUL;
17546 }
17547
17548 rettv->vval.v_string = r;
17549 }
17550}
17551
17552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 * "resolve()" function
17554 */
17555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017556f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557{
17558 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017559#ifdef HAVE_READLINK
17560 char_u *buf = NULL;
17561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017563 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564#ifdef FEAT_SHORTCUT
17565 {
17566 char_u *v = NULL;
17567
17568 v = mch_resolve_shortcut(p);
17569 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017570 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017572 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 }
17574#else
17575# ifdef HAVE_READLINK
17576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 char_u *cpy;
17578 int len;
17579 char_u *remain = NULL;
17580 char_u *q;
17581 int is_relative_to_current = FALSE;
17582 int has_trailing_pathsep = FALSE;
17583 int limit = 100;
17584
17585 p = vim_strsave(p);
17586
17587 if (p[0] == '.' && (vim_ispathsep(p[1])
17588 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17589 is_relative_to_current = TRUE;
17590
17591 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017592 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017595 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597
17598 q = getnextcomp(p);
17599 if (*q != NUL)
17600 {
17601 /* Separate the first path component in "p", and keep the
17602 * remainder (beginning with the path separator). */
17603 remain = vim_strsave(q - 1);
17604 q[-1] = NUL;
17605 }
17606
Bram Moolenaard9462e32011-04-11 21:35:11 +020017607 buf = alloc(MAXPATHL + 1);
17608 if (buf == NULL)
17609 goto fail;
17610
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 for (;;)
17612 {
17613 for (;;)
17614 {
17615 len = readlink((char *)p, (char *)buf, MAXPATHL);
17616 if (len <= 0)
17617 break;
17618 buf[len] = NUL;
17619
17620 if (limit-- == 0)
17621 {
17622 vim_free(p);
17623 vim_free(remain);
17624 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 goto fail;
17627 }
17628
17629 /* Ensure that the result will have a trailing path separator
17630 * if the argument has one. */
17631 if (remain == NULL && has_trailing_pathsep)
17632 add_pathsep(buf);
17633
17634 /* Separate the first path component in the link value and
17635 * concatenate the remainders. */
17636 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17637 if (*q != NUL)
17638 {
17639 if (remain == NULL)
17640 remain = vim_strsave(q - 1);
17641 else
17642 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017643 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 if (cpy != NULL)
17645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 vim_free(remain);
17647 remain = cpy;
17648 }
17649 }
17650 q[-1] = NUL;
17651 }
17652
17653 q = gettail(p);
17654 if (q > p && *q == NUL)
17655 {
17656 /* Ignore trailing path separator. */
17657 q[-1] = NUL;
17658 q = gettail(p);
17659 }
17660 if (q > p && !mch_isFullName(buf))
17661 {
17662 /* symlink is relative to directory of argument */
17663 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17664 if (cpy != NULL)
17665 {
17666 STRCPY(cpy, p);
17667 STRCPY(gettail(cpy), buf);
17668 vim_free(p);
17669 p = cpy;
17670 }
17671 }
17672 else
17673 {
17674 vim_free(p);
17675 p = vim_strsave(buf);
17676 }
17677 }
17678
17679 if (remain == NULL)
17680 break;
17681
17682 /* Append the first path component of "remain" to "p". */
17683 q = getnextcomp(remain + 1);
17684 len = q - remain - (*q != NUL);
17685 cpy = vim_strnsave(p, STRLEN(p) + len);
17686 if (cpy != NULL)
17687 {
17688 STRNCAT(cpy, remain, len);
17689 vim_free(p);
17690 p = cpy;
17691 }
17692 /* Shorten "remain". */
17693 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017694 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 else
17696 {
17697 vim_free(remain);
17698 remain = NULL;
17699 }
17700 }
17701
17702 /* If the result is a relative path name, make it explicitly relative to
17703 * the current directory if and only if the argument had this form. */
17704 if (!vim_ispathsep(*p))
17705 {
17706 if (is_relative_to_current
17707 && *p != NUL
17708 && !(p[0] == '.'
17709 && (p[1] == NUL
17710 || vim_ispathsep(p[1])
17711 || (p[1] == '.'
17712 && (p[2] == NUL
17713 || vim_ispathsep(p[2]))))))
17714 {
17715 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017716 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 if (cpy != NULL)
17718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 vim_free(p);
17720 p = cpy;
17721 }
17722 }
17723 else if (!is_relative_to_current)
17724 {
17725 /* Strip leading "./". */
17726 q = p;
17727 while (q[0] == '.' && vim_ispathsep(q[1]))
17728 q += 2;
17729 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017730 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 }
17732 }
17733
17734 /* Ensure that the result will have no trailing path separator
17735 * if the argument had none. But keep "/" or "//". */
17736 if (!has_trailing_pathsep)
17737 {
17738 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017739 if (after_pathsep(p, q))
17740 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 }
17742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017743 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 }
17745# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017746 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747# endif
17748#endif
17749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017750 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751
17752#ifdef HAVE_READLINK
17753fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017754 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757}
17758
17759/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017760 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 */
17762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017763f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764{
Bram Moolenaar33570922005-01-25 22:26:29 +000017765 list_T *l;
17766 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767
Bram Moolenaar0d660222005-01-07 21:51:51 +000017768 if (argvars[0].v_type != VAR_LIST)
17769 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017770 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017771 && !tv_check_lock(l->lv_lock,
17772 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017773 {
17774 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017775 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017776 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017777 while (li != NULL)
17778 {
17779 ni = li->li_prev;
17780 list_append(l, li);
17781 li = ni;
17782 }
17783 rettv->vval.v_list = l;
17784 rettv->v_type = VAR_LIST;
17785 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017786 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788}
17789
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017790#define SP_NOMOVE 0x01 /* don't move cursor */
17791#define SP_REPEAT 0x02 /* repeat to find outer pair */
17792#define SP_RETCOUNT 0x04 /* return matchcount */
17793#define SP_SETPCMARK 0x08 /* set previous context mark */
17794#define SP_START 0x10 /* accept match at start position */
17795#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17796#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017797#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017798
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017799static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017800
17801/*
17802 * Get flags for a search function.
17803 * Possibly sets "p_ws".
17804 * Returns BACKWARD, FORWARD or zero (for an error).
17805 */
17806 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017807get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017808{
17809 int dir = FORWARD;
17810 char_u *flags;
17811 char_u nbuf[NUMBUFLEN];
17812 int mask;
17813
17814 if (varp->v_type != VAR_UNKNOWN)
17815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017816 flags = get_tv_string_buf_chk(varp, nbuf);
17817 if (flags == NULL)
17818 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017819 while (*flags != NUL)
17820 {
17821 switch (*flags)
17822 {
17823 case 'b': dir = BACKWARD; break;
17824 case 'w': p_ws = TRUE; break;
17825 case 'W': p_ws = FALSE; break;
17826 default: mask = 0;
17827 if (flagsp != NULL)
17828 switch (*flags)
17829 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017830 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017831 case 'e': mask = SP_END; break;
17832 case 'm': mask = SP_RETCOUNT; break;
17833 case 'n': mask = SP_NOMOVE; break;
17834 case 'p': mask = SP_SUBPAT; break;
17835 case 'r': mask = SP_REPEAT; break;
17836 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017837 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017838 }
17839 if (mask == 0)
17840 {
17841 EMSG2(_(e_invarg2), flags);
17842 dir = 0;
17843 }
17844 else
17845 *flagsp |= mask;
17846 }
17847 if (dir == 0)
17848 break;
17849 ++flags;
17850 }
17851 }
17852 return dir;
17853}
17854
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017856 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017859search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017861 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 char_u *pat;
17863 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017864 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 int save_p_ws = p_ws;
17866 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017867 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017868 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017869 proftime_T tm;
17870#ifdef FEAT_RELTIME
17871 long time_limit = 0;
17872#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017873 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017874 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017876 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017877 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017878 if (dir == 0)
17879 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017880 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017881 if (flags & SP_START)
17882 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017883 if (flags & SP_END)
17884 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017885 if (flags & SP_COLUMN)
17886 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017887
Bram Moolenaar76929292008-01-06 19:07:36 +000017888 /* Optional arguments: line number to stop searching and timeout. */
17889 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017890 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017891 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017892 if (lnum_stop < 0)
17893 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017894#ifdef FEAT_RELTIME
17895 if (argvars[3].v_type != VAR_UNKNOWN)
17896 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017897 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017898 if (time_limit < 0)
17899 goto theend;
17900 }
17901#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017902 }
17903
Bram Moolenaar76929292008-01-06 19:07:36 +000017904#ifdef FEAT_RELTIME
17905 /* Set the time limit, if there is one. */
17906 profile_setlimit(time_limit, &tm);
17907#endif
17908
Bram Moolenaar231334e2005-07-25 20:46:57 +000017909 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017910 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017911 * Check to make sure only those flags are set.
17912 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17913 * flags cannot be set. Check for that condition also.
17914 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017915 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017916 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017918 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017919 goto theend;
17920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017922 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017923 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017924 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017925 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017927 if (flags & SP_SUBPAT)
17928 retval = subpatnum;
17929 else
17930 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017931 if (flags & SP_SETPCMARK)
17932 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017934 if (match_pos != NULL)
17935 {
17936 /* Store the match cursor position */
17937 match_pos->lnum = pos.lnum;
17938 match_pos->col = pos.col + 1;
17939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 /* "/$" will put the cursor after the end of the line, may need to
17941 * correct that here */
17942 check_cursor();
17943 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017944
17945 /* If 'n' flag is used: restore cursor position. */
17946 if (flags & SP_NOMOVE)
17947 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017948 else
17949 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017950theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017952
17953 return retval;
17954}
17955
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017956#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017957
17958/*
17959 * round() is not in C90, use ceil() or floor() instead.
17960 */
17961 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017962vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017963{
17964 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17965}
17966
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017967/*
17968 * "round({float})" function
17969 */
17970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017971f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017972{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017973 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017974
17975 rettv->v_type = VAR_FLOAT;
17976 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017977 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017978 else
17979 rettv->vval.v_float = 0.0;
17980}
17981#endif
17982
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017984 * "screenattr()" function
17985 */
17986 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017987f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017988{
17989 int row;
17990 int col;
17991 int c;
17992
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017993 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17994 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017995 if (row < 0 || row >= screen_Rows
17996 || col < 0 || col >= screen_Columns)
17997 c = -1;
17998 else
17999 c = ScreenAttrs[LineOffset[row] + col];
18000 rettv->vval.v_number = c;
18001}
18002
18003/*
18004 * "screenchar()" function
18005 */
18006 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010018007f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020018008{
18009 int row;
18010 int col;
18011 int off;
18012 int c;
18013
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018014 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
18015 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020018016 if (row < 0 || row >= screen_Rows
18017 || col < 0 || col >= screen_Columns)
18018 c = -1;
18019 else
18020 {
18021 off = LineOffset[row] + col;
18022#ifdef FEAT_MBYTE
18023 if (enc_utf8 && ScreenLinesUC[off] != 0)
18024 c = ScreenLinesUC[off];
18025 else
18026#endif
18027 c = ScreenLines[off];
18028 }
18029 rettv->vval.v_number = c;
18030}
18031
18032/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018033 * "screencol()" function
18034 *
18035 * First column is 1 to be consistent with virtcol().
18036 */
18037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018038f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018039{
18040 rettv->vval.v_number = screen_screencol() + 1;
18041}
18042
18043/*
18044 * "screenrow()" function
18045 */
18046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018047f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018048{
18049 rettv->vval.v_number = screen_screenrow() + 1;
18050}
18051
18052/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018053 * "search()" function
18054 */
18055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018056f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018057{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018058 int flags = 0;
18059
18060 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061}
18062
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018064 * "searchdecl()" function
18065 */
18066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018067f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018068{
18069 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018070 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018071 int error = FALSE;
18072 char_u *name;
18073
18074 rettv->vval.v_number = 1; /* default: FAIL */
18075
18076 name = get_tv_string_chk(&argvars[0]);
18077 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000018078 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018079 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018080 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018081 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018082 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018083 if (!error && name != NULL)
18084 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018085 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018086}
18087
18088/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018089 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018091 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018092searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093{
18094 char_u *spat, *mpat, *epat;
18095 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 int dir;
18098 int flags = 0;
18099 char_u nbuf1[NUMBUFLEN];
18100 char_u nbuf2[NUMBUFLEN];
18101 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018102 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018103 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018104 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018107 spat = get_tv_string_chk(&argvars[0]);
18108 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18109 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18110 if (spat == NULL || mpat == NULL || epat == NULL)
18111 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 /* Handle the optional fourth argument: flags */
18114 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018115 if (dir == 0)
18116 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018117
18118 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018119 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18120 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018121 if ((flags & (SP_END | SP_SUBPAT)) != 0
18122 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018123 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018124 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018125 goto theend;
18126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018128 /* Using 'r' implies 'W', otherwise it doesn't work. */
18129 if (flags & SP_REPEAT)
18130 p_ws = FALSE;
18131
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018132 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018133 if (argvars[3].v_type == VAR_UNKNOWN
18134 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 skip = (char_u *)"";
18136 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018138 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018139 if (argvars[5].v_type != VAR_UNKNOWN)
18140 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018141 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018142 if (lnum_stop < 0)
18143 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018144#ifdef FEAT_RELTIME
18145 if (argvars[6].v_type != VAR_UNKNOWN)
18146 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018147 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018148 if (time_limit < 0)
18149 goto theend;
18150 }
18151#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018152 }
18153 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018154 if (skip == NULL)
18155 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018157 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018158 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018159
18160theend:
18161 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018162
18163 return retval;
18164}
18165
18166/*
18167 * "searchpair()" function
18168 */
18169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018170f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018171{
18172 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18173}
18174
18175/*
18176 * "searchpairpos()" function
18177 */
18178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018179f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018180{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018181 pos_T match_pos;
18182 int lnum = 0;
18183 int col = 0;
18184
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018185 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018186 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018187
18188 if (searchpair_cmn(argvars, &match_pos) > 0)
18189 {
18190 lnum = match_pos.lnum;
18191 col = match_pos.col;
18192 }
18193
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018194 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18195 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018196}
18197
18198/*
18199 * Search for a start/middle/end thing.
18200 * Used by searchpair(), see its documentation for the details.
18201 * Returns 0 or -1 for no match,
18202 */
18203 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018204do_searchpair(
18205 char_u *spat, /* start pattern */
18206 char_u *mpat, /* middle pattern */
18207 char_u *epat, /* end pattern */
18208 int dir, /* BACKWARD or FORWARD */
18209 char_u *skip, /* skip expression */
18210 int flags, /* SP_SETPCMARK and other SP_ values */
18211 pos_T *match_pos,
18212 linenr_T lnum_stop, /* stop at this line if not zero */
18213 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018214{
18215 char_u *save_cpo;
18216 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18217 long retval = 0;
18218 pos_T pos;
18219 pos_T firstpos;
18220 pos_T foundpos;
18221 pos_T save_cursor;
18222 pos_T save_pos;
18223 int n;
18224 int r;
18225 int nest = 1;
18226 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018227 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018228 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018229
18230 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18231 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018232 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018233
Bram Moolenaar76929292008-01-06 19:07:36 +000018234#ifdef FEAT_RELTIME
18235 /* Set the time limit, if there is one. */
18236 profile_setlimit(time_limit, &tm);
18237#endif
18238
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018239 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18240 * start/middle/end (pat3, for the top pair). */
18241 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18242 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18243 if (pat2 == NULL || pat3 == NULL)
18244 goto theend;
18245 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18246 if (*mpat == NUL)
18247 STRCPY(pat3, pat2);
18248 else
18249 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18250 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018251 if (flags & SP_START)
18252 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018253
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 save_cursor = curwin->w_cursor;
18255 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018256 clearpos(&firstpos);
18257 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 pat = pat3;
18259 for (;;)
18260 {
18261 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018262 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18264 /* didn't find it or found the first match again: FAIL */
18265 break;
18266
18267 if (firstpos.lnum == 0)
18268 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018269 if (equalpos(pos, foundpos))
18270 {
18271 /* Found the same position again. Can happen with a pattern that
18272 * has "\zs" at the end and searching backwards. Advance one
18273 * character and try again. */
18274 if (dir == BACKWARD)
18275 decl(&pos);
18276 else
18277 incl(&pos);
18278 }
18279 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018281 /* clear the start flag to avoid getting stuck here */
18282 options &= ~SEARCH_START;
18283
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 /* If the skip pattern matches, ignore this match. */
18285 if (*skip != NUL)
18286 {
18287 save_pos = curwin->w_cursor;
18288 curwin->w_cursor = pos;
18289 r = eval_to_bool(skip, &err, NULL, FALSE);
18290 curwin->w_cursor = save_pos;
18291 if (err)
18292 {
18293 /* Evaluating {skip} caused an error, break here. */
18294 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018295 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 break;
18297 }
18298 if (r)
18299 continue;
18300 }
18301
18302 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18303 {
18304 /* Found end when searching backwards or start when searching
18305 * forward: nested pair. */
18306 ++nest;
18307 pat = pat2; /* nested, don't search for middle */
18308 }
18309 else
18310 {
18311 /* Found end when searching forward or start when searching
18312 * backward: end of (nested) pair; or found middle in outer pair. */
18313 if (--nest == 1)
18314 pat = pat3; /* outer level, search for middle */
18315 }
18316
18317 if (nest == 0)
18318 {
18319 /* Found the match: return matchcount or line number. */
18320 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018321 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018323 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018324 if (flags & SP_SETPCMARK)
18325 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 curwin->w_cursor = pos;
18327 if (!(flags & SP_REPEAT))
18328 break;
18329 nest = 1; /* search for next unmatched */
18330 }
18331 }
18332
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018333 if (match_pos != NULL)
18334 {
18335 /* Store the match cursor position */
18336 match_pos->lnum = curwin->w_cursor.lnum;
18337 match_pos->col = curwin->w_cursor.col + 1;
18338 }
18339
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018341 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 curwin->w_cursor = save_cursor;
18343
18344theend:
18345 vim_free(pat2);
18346 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018347 if (p_cpo == empty_option)
18348 p_cpo = save_cpo;
18349 else
18350 /* Darn, evaluating the {skip} expression changed the value. */
18351 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018352
18353 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354}
18355
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018356/*
18357 * "searchpos()" function
18358 */
18359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018360f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018361{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018362 pos_T match_pos;
18363 int lnum = 0;
18364 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018365 int n;
18366 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018367
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018368 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018369 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018370
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018371 n = search_cmn(argvars, &match_pos, &flags);
18372 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018373 {
18374 lnum = match_pos.lnum;
18375 col = match_pos.col;
18376 }
18377
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018378 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18379 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018380 if (flags & SP_SUBPAT)
18381 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018382}
18383
Bram Moolenaar0d660222005-01-07 21:51:51 +000018384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018385f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018387#ifdef FEAT_CLIENTSERVER
18388 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018389 char_u *server = get_tv_string_chk(&argvars[0]);
18390 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391
Bram Moolenaar0d660222005-01-07 21:51:51 +000018392 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018393 if (server == NULL || reply == NULL)
18394 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018395 if (check_restricted() || check_secure())
18396 return;
18397# ifdef FEAT_X11
18398 if (check_connection() == FAIL)
18399 return;
18400# endif
18401
18402 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018404 EMSG(_("E258: Unable to send to client"));
18405 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018407 rettv->vval.v_number = 0;
18408#else
18409 rettv->vval.v_number = -1;
18410#endif
18411}
18412
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018414f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018415{
18416 char_u *r = NULL;
18417
18418#ifdef FEAT_CLIENTSERVER
18419# ifdef WIN32
18420 r = serverGetVimNames();
18421# else
18422 make_connection();
18423 if (X_DISPLAY != NULL)
18424 r = serverGetVimNames(X_DISPLAY);
18425# endif
18426#endif
18427 rettv->v_type = VAR_STRING;
18428 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429}
18430
18431/*
18432 * "setbufvar()" function
18433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018435f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436{
18437 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018440 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441 char_u nbuf[NUMBUFLEN];
18442
18443 if (check_restricted() || check_secure())
18444 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018445 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18446 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018447 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 varp = &argvars[2];
18449
18450 if (buf != NULL && varname != NULL && varp != NULL)
18451 {
18452 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454
18455 if (*varname == '&')
18456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018457 long numval;
18458 char_u *strval;
18459 int error = FALSE;
18460
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018462 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018463 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018464 if (!error && strval != NULL)
18465 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466 }
18467 else
18468 {
18469 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18470 if (bufvarname != NULL)
18471 {
18472 STRCPY(bufvarname, "b:");
18473 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018474 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 vim_free(bufvarname);
18476 }
18477 }
18478
18479 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482}
18483
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018485f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018486{
18487 dict_T *d;
18488 dictitem_T *di;
18489 char_u *csearch;
18490
18491 if (argvars[0].v_type != VAR_DICT)
18492 {
18493 EMSG(_(e_dictreq));
18494 return;
18495 }
18496
18497 if ((d = argvars[0].vval.v_dict) != NULL)
18498 {
18499 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18500 if (csearch != NULL)
18501 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018502#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018503 if (enc_utf8)
18504 {
18505 int pcc[MAX_MCO];
18506 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018507
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018508 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18509 }
18510 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018511#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018512 set_last_csearch(PTR2CHAR(csearch),
18513 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018514 }
18515
18516 di = dict_find(d, (char_u *)"forward", -1);
18517 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018518 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018519 ? FORWARD : BACKWARD);
18520
18521 di = dict_find(d, (char_u *)"until", -1);
18522 if (di != NULL)
18523 set_csearch_until(!!get_tv_number(&di->di_tv));
18524 }
18525}
18526
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527/*
18528 * "setcmdpos()" function
18529 */
18530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018531f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018533 int pos = (int)get_tv_number(&argvars[0]) - 1;
18534
18535 if (pos >= 0)
18536 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537}
18538
18539/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018540 * "setfperm({fname}, {mode})" function
18541 */
18542 static void
18543f_setfperm(typval_T *argvars, typval_T *rettv)
18544{
18545 char_u *fname;
18546 char_u modebuf[NUMBUFLEN];
18547 char_u *mode_str;
18548 int i;
18549 int mask;
18550 int mode = 0;
18551
18552 rettv->vval.v_number = 0;
18553 fname = get_tv_string_chk(&argvars[0]);
18554 if (fname == NULL)
18555 return;
18556 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18557 if (mode_str == NULL)
18558 return;
18559 if (STRLEN(mode_str) != 9)
18560 {
18561 EMSG2(_(e_invarg2), mode_str);
18562 return;
18563 }
18564
18565 mask = 1;
18566 for (i = 8; i >= 0; --i)
18567 {
18568 if (mode_str[i] != '-')
18569 mode |= mask;
18570 mask = mask << 1;
18571 }
18572 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18573}
18574
18575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 * "setline()" function
18577 */
18578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018579f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580{
18581 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018582 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018583 list_T *l = NULL;
18584 listitem_T *li = NULL;
18585 long added = 0;
18586 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018588 lnum = get_tv_lnum(&argvars[0]);
18589 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018591 l = argvars[1].vval.v_list;
18592 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018594 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018595 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018596
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018597 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018598 for (;;)
18599 {
18600 if (l != NULL)
18601 {
18602 /* list argument, get next string */
18603 if (li == NULL)
18604 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018605 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018606 li = li->li_next;
18607 }
18608
18609 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018610 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018611 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018612
18613 /* When coming here from Insert mode, sync undo, so that this can be
18614 * undone separately from what was previously inserted. */
18615 if (u_sync_once == 2)
18616 {
18617 u_sync_once = 1; /* notify that u_sync() was called */
18618 u_sync(TRUE);
18619 }
18620
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018621 if (lnum <= curbuf->b_ml.ml_line_count)
18622 {
18623 /* existing line, replace it */
18624 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18625 {
18626 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018627 if (lnum == curwin->w_cursor.lnum)
18628 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018629 rettv->vval.v_number = 0; /* OK */
18630 }
18631 }
18632 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18633 {
18634 /* lnum is one past the last line, append the line */
18635 ++added;
18636 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18637 rettv->vval.v_number = 0; /* OK */
18638 }
18639
18640 if (l == NULL) /* only one string argument */
18641 break;
18642 ++lnum;
18643 }
18644
18645 if (added > 0)
18646 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647}
18648
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018649static 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 +000018650
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018652 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018653 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018655set_qf_ll_list(
18656 win_T *wp UNUSED,
18657 typval_T *list_arg UNUSED,
18658 typval_T *action_arg UNUSED,
18659 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018660{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018661#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018662 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018663 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018664 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018665#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018666
Bram Moolenaar2641f772005-03-25 21:58:17 +000018667 rettv->vval.v_number = -1;
18668
18669#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018670 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018671 EMSG(_(e_listreq));
18672 else
18673 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018674 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018675
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018676 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018677 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018678 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018679 if (act == NULL)
18680 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018681 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018682 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018683 else
18684 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018685 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018686 else if (action_arg->v_type == VAR_UNKNOWN)
18687 action = ' ';
18688 else
18689 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018690
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018691 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018692 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018693 rettv->vval.v_number = 0;
18694 }
18695#endif
18696}
18697
18698/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018699 * "setloclist()" function
18700 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018702f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018703{
18704 win_T *win;
18705
18706 rettv->vval.v_number = -1;
18707
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018708 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018709 if (win != NULL)
18710 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18711}
18712
18713/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018714 * "setmatches()" function
18715 */
18716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018717f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018718{
18719#ifdef FEAT_SEARCH_EXTRA
18720 list_T *l;
18721 listitem_T *li;
18722 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018723 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018724
18725 rettv->vval.v_number = -1;
18726 if (argvars[0].v_type != VAR_LIST)
18727 {
18728 EMSG(_(e_listreq));
18729 return;
18730 }
18731 if ((l = argvars[0].vval.v_list) != NULL)
18732 {
18733
18734 /* To some extent make sure that we are dealing with a list from
18735 * "getmatches()". */
18736 li = l->lv_first;
18737 while (li != NULL)
18738 {
18739 if (li->li_tv.v_type != VAR_DICT
18740 || (d = li->li_tv.vval.v_dict) == NULL)
18741 {
18742 EMSG(_(e_invarg));
18743 return;
18744 }
18745 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018746 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18747 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018748 && dict_find(d, (char_u *)"priority", -1) != NULL
18749 && dict_find(d, (char_u *)"id", -1) != NULL))
18750 {
18751 EMSG(_(e_invarg));
18752 return;
18753 }
18754 li = li->li_next;
18755 }
18756
18757 clear_matches(curwin);
18758 li = l->lv_first;
18759 while (li != NULL)
18760 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018761 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018762 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018763 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018764 char_u *group;
18765 int priority;
18766 int id;
18767 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018768
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018769 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018770 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18771 {
18772 if (s == NULL)
18773 {
18774 s = list_alloc();
18775 if (s == NULL)
18776 return;
18777 }
18778
18779 /* match from matchaddpos() */
18780 for (i = 1; i < 9; i++)
18781 {
18782 sprintf((char *)buf, (char *)"pos%d", i);
18783 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18784 {
18785 if (di->di_tv.v_type != VAR_LIST)
18786 return;
18787
18788 list_append_tv(s, &di->di_tv);
18789 s->lv_refcount++;
18790 }
18791 else
18792 break;
18793 }
18794 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018795
18796 group = get_dict_string(d, (char_u *)"group", FALSE);
18797 priority = (int)get_dict_number(d, (char_u *)"priority");
18798 id = (int)get_dict_number(d, (char_u *)"id");
18799 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18800 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18801 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018802 if (i == 0)
18803 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018804 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018805 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018806 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018807 }
18808 else
18809 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018810 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018811 list_unref(s);
18812 s = NULL;
18813 }
18814
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018815 li = li->li_next;
18816 }
18817 rettv->vval.v_number = 0;
18818 }
18819#endif
18820}
18821
18822/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018823 * "setpos()" function
18824 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018826f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018827{
18828 pos_T pos;
18829 int fnum;
18830 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018831 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018832
Bram Moolenaar08250432008-02-13 11:42:46 +000018833 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018834 name = get_tv_string_chk(argvars);
18835 if (name != NULL)
18836 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018837 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018838 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018839 if (--pos.col < 0)
18840 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018841 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018842 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018843 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018844 if (fnum == curbuf->b_fnum)
18845 {
18846 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018847 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018848 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018849 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018850 curwin->w_set_curswant = FALSE;
18851 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018852 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018853 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018854 }
18855 else
18856 EMSG(_(e_invarg));
18857 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018858 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18859 {
18860 /* set mark */
18861 if (setmark_pos(name[1], &pos, fnum) == OK)
18862 rettv->vval.v_number = 0;
18863 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018864 else
18865 EMSG(_(e_invarg));
18866 }
18867 }
18868}
18869
18870/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018871 * "setqflist()" function
18872 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018874f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018875{
18876 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18877}
18878
18879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 * "setreg()" function
18881 */
18882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018883f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884{
18885 int regname;
18886 char_u *strregname;
18887 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018888 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889 int append;
18890 char_u yank_type;
18891 long block_len;
18892
18893 block_len = -1;
18894 yank_type = MAUTO;
18895 append = FALSE;
18896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018897 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018898 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018900 if (strregname == NULL)
18901 return; /* type error; errmsg already given */
18902 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 if (regname == 0 || regname == '@')
18904 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018906 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018908 stropt = get_tv_string_chk(&argvars[2]);
18909 if (stropt == NULL)
18910 return; /* type error */
18911 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 switch (*stropt)
18913 {
18914 case 'a': case 'A': /* append */
18915 append = TRUE;
18916 break;
18917 case 'v': case 'c': /* character-wise selection */
18918 yank_type = MCHAR;
18919 break;
18920 case 'V': case 'l': /* line-wise selection */
18921 yank_type = MLINE;
18922 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 case 'b': case Ctrl_V: /* block-wise selection */
18924 yank_type = MBLOCK;
18925 if (VIM_ISDIGIT(stropt[1]))
18926 {
18927 ++stropt;
18928 block_len = getdigits(&stropt) - 1;
18929 --stropt;
18930 }
18931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 }
18933 }
18934
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018935 if (argvars[1].v_type == VAR_LIST)
18936 {
18937 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018938 char_u **allocval;
18939 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018940 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018941 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018942 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018943 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018944 int len;
18945
18946 /* If the list is NULL handle like an empty list. */
18947 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018948
Bram Moolenaar7d647822014-04-05 21:28:56 +020018949 /* First half: use for pointers to result lines; second half: use for
18950 * pointers to allocated copies. */
18951 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018952 if (lstval == NULL)
18953 return;
18954 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018955 allocval = lstval + len + 2;
18956 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018957
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018958 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018959 li = li->li_next)
18960 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018961 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018962 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018963 goto free_lstval;
18964 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018965 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018966 /* Need to make a copy, next get_tv_string_buf_chk() will
18967 * overwrite the string. */
18968 strval = vim_strsave(buf);
18969 if (strval == NULL)
18970 goto free_lstval;
18971 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018972 }
18973 *curval++ = strval;
18974 }
18975 *curval++ = NULL;
18976
18977 write_reg_contents_lst(regname, lstval, -1,
18978 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018979free_lstval:
18980 while (curallocval > allocval)
18981 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018982 vim_free(lstval);
18983 }
18984 else
18985 {
18986 strval = get_tv_string_chk(&argvars[1]);
18987 if (strval == NULL)
18988 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018989 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018992 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993}
18994
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018995/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018996 * "settabvar()" function
18997 */
18998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018999f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019000{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019001#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019002 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019003 tabpage_T *tp;
19004#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019005 char_u *varname, *tabvarname;
19006 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019007
19008 rettv->vval.v_number = 0;
19009
19010 if (check_restricted() || check_secure())
19011 return;
19012
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019013#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019014 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019015#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019016 varname = get_tv_string_chk(&argvars[1]);
19017 varp = &argvars[2];
19018
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019019 if (varname != NULL && varp != NULL
19020#ifdef FEAT_WINDOWS
19021 && tp != NULL
19022#endif
19023 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019024 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019025#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019026 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019027 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019028#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019029
19030 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
19031 if (tabvarname != NULL)
19032 {
19033 STRCPY(tabvarname, "t:");
19034 STRCPY(tabvarname + 2, varname);
19035 set_var(tabvarname, varp, TRUE);
19036 vim_free(tabvarname);
19037 }
19038
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019039#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019040 /* Restore current tabpage */
19041 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019042 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019043#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019044 }
19045}
19046
19047/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019048 * "settabwinvar()" function
19049 */
19050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019051f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019052{
19053 setwinvar(argvars, rettv, 1);
19054}
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055
19056/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019057 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019060f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019062 setwinvar(argvars, rettv, 0);
19063}
19064
19065/*
19066 * "setwinvar()" and "settabwinvar()" functions
19067 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020019068
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019070setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019071{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 win_T *win;
19073#ifdef FEAT_WINDOWS
19074 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019075 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020019076 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077#endif
19078 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019079 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019081 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082
19083 if (check_restricted() || check_secure())
19084 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019085
19086#ifdef FEAT_WINDOWS
19087 if (off == 1)
19088 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
19089 else
19090 tp = curtab;
19091#endif
19092 win = find_win_by_nr(&argvars[off], tp);
19093 varname = get_tv_string_chk(&argvars[off + 1]);
19094 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095
19096 if (win != NULL && varname != NULL && varp != NULL)
19097 {
19098#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019099 need_switch_win = !(tp == curtab && win == curwin);
19100 if (!need_switch_win
19101 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019104 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019106 long numval;
19107 char_u *strval;
19108 int error = FALSE;
19109
19110 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019111 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019112 strval = get_tv_string_buf_chk(varp, nbuf);
19113 if (!error && strval != NULL)
19114 set_option_value(varname, numval, strval, OPT_LOCAL);
19115 }
19116 else
19117 {
19118 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19119 if (winvarname != NULL)
19120 {
19121 STRCPY(winvarname, "w:");
19122 STRCPY(winvarname + 2, varname);
19123 set_var(winvarname, varp, TRUE);
19124 vim_free(winvarname);
19125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 }
19127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019129 if (need_switch_win)
19130 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131#endif
19132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133}
19134
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019135#ifdef FEAT_CRYPT
19136/*
19137 * "sha256({string})" function
19138 */
19139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019140f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019141{
19142 char_u *p;
19143
19144 p = get_tv_string(&argvars[0]);
19145 rettv->vval.v_string = vim_strsave(
19146 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19147 rettv->v_type = VAR_STRING;
19148}
19149#endif /* FEAT_CRYPT */
19150
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019152 * "shellescape({string})" function
19153 */
19154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019155f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019156{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019157 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019158 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019159 rettv->v_type = VAR_STRING;
19160}
19161
19162/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019163 * shiftwidth() function
19164 */
19165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019166f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019167{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019168 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019169}
19170
19171/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019172 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 */
19174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019175f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019177 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178
Bram Moolenaar0d660222005-01-07 21:51:51 +000019179 p = get_tv_string(&argvars[0]);
19180 rettv->vval.v_string = vim_strsave(p);
19181 simplify_filename(rettv->vval.v_string); /* simplify in place */
19182 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183}
19184
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019185#ifdef FEAT_FLOAT
19186/*
19187 * "sin()" function
19188 */
19189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019190f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019191{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019192 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019193
19194 rettv->v_type = VAR_FLOAT;
19195 if (get_float_arg(argvars, &f) == OK)
19196 rettv->vval.v_float = sin(f);
19197 else
19198 rettv->vval.v_float = 0.0;
19199}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019200
19201/*
19202 * "sinh()" function
19203 */
19204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019205f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019206{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019207 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019208
19209 rettv->v_type = VAR_FLOAT;
19210 if (get_float_arg(argvars, &f) == OK)
19211 rettv->vval.v_float = sinh(f);
19212 else
19213 rettv->vval.v_float = 0.0;
19214}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019215#endif
19216
Bram Moolenaar0d660222005-01-07 21:51:51 +000019217static int
19218#ifdef __BORLANDC__
19219 _RTLENTRYF
19220#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019221 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019222static int
19223#ifdef __BORLANDC__
19224 _RTLENTRYF
19225#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019226 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019227
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019228/* struct used in the array that's given to qsort() */
19229typedef struct
19230{
19231 listitem_T *item;
19232 int idx;
19233} sortItem_T;
19234
Bram Moolenaar0b962472016-02-22 22:51:33 +010019235/* struct storing information about current sort */
19236typedef struct
19237{
19238 int item_compare_ic;
19239 int item_compare_numeric;
19240 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019241#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019242 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019243#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019244 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019245 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019246 dict_T *item_compare_selfdict;
19247 int item_compare_func_err;
19248 int item_compare_keep_zero;
19249} sortinfo_T;
19250static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019251static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019252#define ITEM_COMPARE_FAIL 999
19253
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019255 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019257 static int
19258#ifdef __BORLANDC__
19259_RTLENTRYF
19260#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019261item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019263 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019264 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019265 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019266 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019267 int res;
19268 char_u numbuf1[NUMBUFLEN];
19269 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019271 si1 = (sortItem_T *)s1;
19272 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019273 tv1 = &si1->item->li_tv;
19274 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019275
Bram Moolenaar0b962472016-02-22 22:51:33 +010019276 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019277 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019278 varnumber_T v1 = get_tv_number(tv1);
19279 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019280
19281 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19282 }
19283
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019284#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019285 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019286 {
19287 float_T v1 = get_tv_float(tv1);
19288 float_T v2 = get_tv_float(tv2);
19289
19290 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19291 }
19292#endif
19293
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019294 /* tv2string() puts quotes around a string and allocates memory. Don't do
19295 * that for string variables. Use a single quote when comparing with a
19296 * non-string to do what the docs promise. */
19297 if (tv1->v_type == VAR_STRING)
19298 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019299 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019300 p1 = (char_u *)"'";
19301 else
19302 p1 = tv1->vval.v_string;
19303 }
19304 else
19305 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19306 if (tv2->v_type == VAR_STRING)
19307 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019308 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019309 p2 = (char_u *)"'";
19310 else
19311 p2 = tv2->vval.v_string;
19312 }
19313 else
19314 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019315 if (p1 == NULL)
19316 p1 = (char_u *)"";
19317 if (p2 == NULL)
19318 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019319 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019320 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019321 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019322 res = STRICMP(p1, p2);
19323 else
19324 res = STRCMP(p1, p2);
19325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019327 {
19328 double n1, n2;
19329 n1 = strtod((char *)p1, (char **)&p1);
19330 n2 = strtod((char *)p2, (char **)&p2);
19331 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19332 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019333
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019334 /* When the result would be zero, compare the item indexes. Makes the
19335 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019336 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019337 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019338
Bram Moolenaar0d660222005-01-07 21:51:51 +000019339 vim_free(tofree1);
19340 vim_free(tofree2);
19341 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342}
19343
19344 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019345#ifdef __BORLANDC__
19346_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019348item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019349{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019350 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019351 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019352 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019353 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019354 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019355 char_u *func_name;
19356 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019358 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019359 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019360 return 0;
19361
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019362 si1 = (sortItem_T *)s1;
19363 si2 = (sortItem_T *)s2;
19364
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019365 if (partial == NULL)
19366 func_name = sortinfo->item_compare_func;
19367 else
19368 func_name = partial->pt_name;
19369
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019370 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019371 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019372 copy_tv(&si1->item->li_tv, &argv[0]);
19373 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019374
19375 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019376 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019377 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019378 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019379 clear_tv(&argv[0]);
19380 clear_tv(&argv[1]);
19381
19382 if (res == FAIL)
19383 res = ITEM_COMPARE_FAIL;
19384 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019385 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019386 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019387 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019388 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019389
19390 /* When the result would be zero, compare the pointers themselves. Makes
19391 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019392 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019393 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019394
Bram Moolenaar0d660222005-01-07 21:51:51 +000019395 return res;
19396}
19397
19398/*
19399 * "sort({list})" function
19400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019402do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403{
Bram Moolenaar33570922005-01-25 22:26:29 +000019404 list_T *l;
19405 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019406 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019407 sortinfo_T *old_sortinfo;
19408 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409 long len;
19410 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411
Bram Moolenaar0b962472016-02-22 22:51:33 +010019412 /* Pointer to current info struct used in compare function. Save and
19413 * restore the current one for nested calls. */
19414 old_sortinfo = sortinfo;
19415 sortinfo = &info;
19416
Bram Moolenaar0d660222005-01-07 21:51:51 +000019417 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019418 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 else
19420 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019421 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019422 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019423 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19424 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019425 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019426 rettv->vval.v_list = l;
19427 rettv->v_type = VAR_LIST;
19428 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429
Bram Moolenaar0d660222005-01-07 21:51:51 +000019430 len = list_len(l);
19431 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019432 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433
Bram Moolenaar0b962472016-02-22 22:51:33 +010019434 info.item_compare_ic = FALSE;
19435 info.item_compare_numeric = FALSE;
19436 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019437#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019438 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019439#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019440 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019441 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019442 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019443 if (argvars[1].v_type != VAR_UNKNOWN)
19444 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019445 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019446 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019447 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019448 else if (argvars[1].v_type == VAR_PARTIAL)
19449 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019450 else
19451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019452 int error = FALSE;
19453
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019454 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019455 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019456 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019457 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019458 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019459 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019460 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019461 else if (i != 0)
19462 {
19463 EMSG(_(e_invarg));
19464 goto theend;
19465 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019466 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019467 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019468 if (*info.item_compare_func == NUL)
19469 {
19470 /* empty string means default sort */
19471 info.item_compare_func = NULL;
19472 }
19473 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019474 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019475 info.item_compare_func = NULL;
19476 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019477 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019478 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019479 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019480 info.item_compare_func = NULL;
19481 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019482 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019483#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019484 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019485 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019486 info.item_compare_func = NULL;
19487 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019488 }
19489#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019490 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019491 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019492 info.item_compare_func = NULL;
19493 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019494 }
19495 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019496 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019497
19498 if (argvars[2].v_type != VAR_UNKNOWN)
19499 {
19500 /* optional third argument: {dict} */
19501 if (argvars[2].v_type != VAR_DICT)
19502 {
19503 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019504 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019505 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019506 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019507 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509
Bram Moolenaar0d660222005-01-07 21:51:51 +000019510 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019511 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019512 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019513 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514
Bram Moolenaar327aa022014-03-25 18:24:23 +010019515 i = 0;
19516 if (sort)
19517 {
19518 /* sort(): ptrs will be the list to sort */
19519 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019520 {
19521 ptrs[i].item = li;
19522 ptrs[i].idx = i;
19523 ++i;
19524 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019525
Bram Moolenaar0b962472016-02-22 22:51:33 +010019526 info.item_compare_func_err = FALSE;
19527 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019528 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019529 if ((info.item_compare_func != NULL
19530 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019531 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019532 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019533 EMSG(_("E702: Sort compare function failed"));
19534 else
19535 {
19536 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019537 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019538 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019539 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019540 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019541
Bram Moolenaar0b962472016-02-22 22:51:33 +010019542 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019543 {
19544 /* Clear the List and append the items in sorted order. */
19545 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19546 l->lv_len = 0;
19547 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019548 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019549 }
19550 }
19551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019553 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019554 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019555
19556 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019557 info.item_compare_func_err = FALSE;
19558 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019559 item_compare_func_ptr = info.item_compare_func != NULL
19560 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019561 ? item_compare2 : item_compare;
19562
19563 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19564 li = li->li_next)
19565 {
19566 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19567 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019568 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019569 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019570 {
19571 EMSG(_("E882: Uniq compare function failed"));
19572 break;
19573 }
19574 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019575
Bram Moolenaar0b962472016-02-22 22:51:33 +010019576 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019577 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019578 while (--i >= 0)
19579 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019580 li = ptrs[i].item->li_next;
19581 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019582 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019583 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019584 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019585 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019586 list_fix_watch(l, li);
19587 listitem_free(li);
19588 l->lv_len--;
19589 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019590 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019591 }
19592
19593 vim_free(ptrs);
19594 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019595theend:
19596 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019597}
19598
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019599/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019600 * "sort({list})" function
19601 */
19602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019603f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019604{
19605 do_sort_uniq(argvars, rettv, TRUE);
19606}
19607
19608/*
19609 * "uniq({list})" function
19610 */
19611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019612f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019613{
19614 do_sort_uniq(argvars, rettv, FALSE);
19615}
19616
19617/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019618 * "soundfold({word})" function
19619 */
19620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019621f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019622{
19623 char_u *s;
19624
19625 rettv->v_type = VAR_STRING;
19626 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019627#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019628 rettv->vval.v_string = eval_soundfold(s);
19629#else
19630 rettv->vval.v_string = vim_strsave(s);
19631#endif
19632}
19633
19634/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019635 * "spellbadword()" function
19636 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019638f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019639{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019640 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019641 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019642 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019643
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019644 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019645 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019646
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019647#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019648 if (argvars[0].v_type == VAR_UNKNOWN)
19649 {
19650 /* Find the start and length of the badly spelled word. */
19651 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19652 if (len != 0)
19653 word = ml_get_cursor();
19654 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019655 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019656 {
19657 char_u *str = get_tv_string_chk(&argvars[0]);
19658 int capcol = -1;
19659
19660 if (str != NULL)
19661 {
19662 /* Check the argument for spelling. */
19663 while (*str != NUL)
19664 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019665 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019666 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019667 {
19668 word = str;
19669 break;
19670 }
19671 str += len;
19672 }
19673 }
19674 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019675#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019676
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019677 list_append_string(rettv->vval.v_list, word, len);
19678 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019679 attr == HLF_SPB ? "bad" :
19680 attr == HLF_SPR ? "rare" :
19681 attr == HLF_SPL ? "local" :
19682 attr == HLF_SPC ? "caps" :
19683 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019684}
19685
19686/*
19687 * "spellsuggest()" function
19688 */
19689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019690f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019691{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019692#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019693 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019694 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019695 int maxcount;
19696 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019697 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019698 listitem_T *li;
19699 int need_capital = FALSE;
19700#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019701
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019702 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019703 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019704
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019705#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019706 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019707 {
19708 str = get_tv_string(&argvars[0]);
19709 if (argvars[1].v_type != VAR_UNKNOWN)
19710 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019711 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019712 if (maxcount <= 0)
19713 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019714 if (argvars[2].v_type != VAR_UNKNOWN)
19715 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019716 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019717 if (typeerr)
19718 return;
19719 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019720 }
19721 else
19722 maxcount = 25;
19723
Bram Moolenaar4770d092006-01-12 23:22:24 +000019724 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019725
19726 for (i = 0; i < ga.ga_len; ++i)
19727 {
19728 str = ((char_u **)ga.ga_data)[i];
19729
19730 li = listitem_alloc();
19731 if (li == NULL)
19732 vim_free(str);
19733 else
19734 {
19735 li->li_tv.v_type = VAR_STRING;
19736 li->li_tv.v_lock = 0;
19737 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019738 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019739 }
19740 }
19741 ga_clear(&ga);
19742 }
19743#endif
19744}
19745
Bram Moolenaar0d660222005-01-07 21:51:51 +000019746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019747f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019748{
19749 char_u *str;
19750 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019751 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019752 regmatch_T regmatch;
19753 char_u patbuf[NUMBUFLEN];
19754 char_u *save_cpo;
19755 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019756 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019757 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019758 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019759
19760 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19761 save_cpo = p_cpo;
19762 p_cpo = (char_u *)"";
19763
19764 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019765 if (argvars[1].v_type != VAR_UNKNOWN)
19766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019767 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19768 if (pat == NULL)
19769 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019770 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019771 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019772 }
19773 if (pat == NULL || *pat == NUL)
19774 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019775
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019776 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019778 if (typeerr)
19779 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780
Bram Moolenaar0d660222005-01-07 21:51:51 +000019781 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19782 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019784 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019785 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019786 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019787 if (*str == NUL)
19788 match = FALSE; /* empty item at the end */
19789 else
19790 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019791 if (match)
19792 end = regmatch.startp[0];
19793 else
19794 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019795 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19796 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019797 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019798 if (list_append_string(rettv->vval.v_list, str,
19799 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019800 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019801 }
19802 if (!match)
19803 break;
19804 /* Advance to just after the match. */
19805 if (regmatch.endp[0] > str)
19806 col = 0;
19807 else
19808 {
19809 /* Don't get stuck at the same match. */
19810#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019811 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019812#else
19813 col = 1;
19814#endif
19815 }
19816 str = regmatch.endp[0];
19817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818
Bram Moolenaar473de612013-06-08 18:19:48 +020019819 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821
Bram Moolenaar0d660222005-01-07 21:51:51 +000019822 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823}
19824
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019825#ifdef FEAT_FLOAT
19826/*
19827 * "sqrt()" function
19828 */
19829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019830f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019831{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019832 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019833
19834 rettv->v_type = VAR_FLOAT;
19835 if (get_float_arg(argvars, &f) == OK)
19836 rettv->vval.v_float = sqrt(f);
19837 else
19838 rettv->vval.v_float = 0.0;
19839}
19840
19841/*
19842 * "str2float()" function
19843 */
19844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019845f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019846{
19847 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19848
19849 if (*p == '+')
19850 p = skipwhite(p + 1);
19851 (void)string2float(p, &rettv->vval.v_float);
19852 rettv->v_type = VAR_FLOAT;
19853}
19854#endif
19855
Bram Moolenaar2c932302006-03-18 21:42:09 +000019856/*
19857 * "str2nr()" function
19858 */
19859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019860f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019861{
19862 int base = 10;
19863 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019864 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019865 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019866
19867 if (argvars[1].v_type != VAR_UNKNOWN)
19868 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019869 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019870 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019871 {
19872 EMSG(_(e_invarg));
19873 return;
19874 }
19875 }
19876
19877 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019878 if (*p == '+')
19879 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019880 switch (base)
19881 {
19882 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19883 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19884 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19885 default: what = 0;
19886 }
19887 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019888 rettv->vval.v_number = n;
19889}
19890
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891#ifdef HAVE_STRFTIME
19892/*
19893 * "strftime({format}[, {time}])" function
19894 */
19895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019896f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897{
19898 char_u result_buf[256];
19899 struct tm *curtime;
19900 time_t seconds;
19901 char_u *p;
19902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019903 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019905 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019906 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 seconds = time(NULL);
19908 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019909 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 curtime = localtime(&seconds);
19911 /* MSVC returns NULL for an invalid value of seconds. */
19912 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019913 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 else
19915 {
19916# ifdef FEAT_MBYTE
19917 vimconv_T conv;
19918 char_u *enc;
19919
19920 conv.vc_type = CONV_NONE;
19921 enc = enc_locale();
19922 convert_setup(&conv, p_enc, enc);
19923 if (conv.vc_type != CONV_NONE)
19924 p = string_convert(&conv, p, NULL);
19925# endif
19926 if (p != NULL)
19927 (void)strftime((char *)result_buf, sizeof(result_buf),
19928 (char *)p, curtime);
19929 else
19930 result_buf[0] = NUL;
19931
19932# ifdef FEAT_MBYTE
19933 if (conv.vc_type != CONV_NONE)
19934 vim_free(p);
19935 convert_setup(&conv, enc, p_enc);
19936 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 else
19939# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019940 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941
19942# ifdef FEAT_MBYTE
19943 /* Release conversion descriptors */
19944 convert_setup(&conv, NULL, NULL);
19945 vim_free(enc);
19946# endif
19947 }
19948}
19949#endif
19950
19951/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019952 * "strgetchar()" function
19953 */
19954 static void
19955f_strgetchar(typval_T *argvars, typval_T *rettv)
19956{
19957 char_u *str;
19958 int len;
19959 int error = FALSE;
19960 int charidx;
19961
19962 rettv->vval.v_number = -1;
19963 str = get_tv_string_chk(&argvars[0]);
19964 if (str == NULL)
19965 return;
19966 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019967 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019968 if (error)
19969 return;
19970#ifdef FEAT_MBYTE
19971 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019972 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019973
19974 while (charidx >= 0 && byteidx < len)
19975 {
19976 if (charidx == 0)
19977 {
19978 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19979 break;
19980 }
19981 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019982 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019983 }
19984 }
19985#else
19986 if (charidx < len)
19987 rettv->vval.v_number = str[charidx];
19988#endif
19989}
19990
19991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 * "stridx()" function
19993 */
19994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019995f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996{
19997 char_u buf[NUMBUFLEN];
19998 char_u *needle;
19999 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000020000 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000020002 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020004 needle = get_tv_string_chk(&argvars[1]);
20005 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000020006 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020007 if (needle == NULL || haystack == NULL)
20008 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009
Bram Moolenaar33570922005-01-25 22:26:29 +000020010 if (argvars[2].v_type != VAR_UNKNOWN)
20011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020012 int error = FALSE;
20013
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020014 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020015 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000020016 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020017 if (start_idx >= 0)
20018 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000020019 }
20020
20021 pos = (char_u *)strstr((char *)haystack, (char *)needle);
20022 if (pos != NULL)
20023 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024}
20025
20026/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020027 * "string()" function
20028 */
20029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020030f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020031{
20032 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020033 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020035 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010020036 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
20037 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020038 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020039 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020040 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041}
20042
20043/*
20044 * "strlen()" function
20045 */
20046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020047f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020049 rettv->vval.v_number = (varnumber_T)(STRLEN(
20050 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051}
20052
20053/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020054 * "strchars()" function
20055 */
20056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020057f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020058{
20059 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020060 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020020061#ifdef FEAT_MBYTE
20062 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020063 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020020064#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020065
20066 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020067 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020068 if (skipcc < 0 || skipcc > 1)
20069 EMSG(_(e_invarg));
20070 else
20071 {
20072#ifdef FEAT_MBYTE
20073 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
20074 while (*s != NUL)
20075 {
20076 func_mb_ptr2char_adv(&s);
20077 ++len;
20078 }
20079 rettv->vval.v_number = len;
20080#else
20081 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
20082#endif
20083 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020020084}
20085
20086/*
Bram Moolenaardc536092010-07-18 15:45:49 +020020087 * "strdisplaywidth()" function
20088 */
20089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020090f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020020091{
20092 char_u *s = get_tv_string(&argvars[0]);
20093 int col = 0;
20094
20095 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020096 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020020097
Bram Moolenaar8a09b982010-07-22 22:20:57 +020020098 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020020099}
20100
20101/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020102 * "strwidth()" function
20103 */
20104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020105f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020106{
20107 char_u *s = get_tv_string(&argvars[0]);
20108
20109 rettv->vval.v_number = (varnumber_T)(
20110#ifdef FEAT_MBYTE
20111 mb_string2cells(s, -1)
20112#else
20113 STRLEN(s)
20114#endif
20115 );
20116}
20117
20118/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020119 * "strcharpart()" function
20120 */
20121 static void
20122f_strcharpart(typval_T *argvars, typval_T *rettv)
20123{
20124#ifdef FEAT_MBYTE
20125 char_u *p;
20126 int nchar;
20127 int nbyte = 0;
20128 int charlen;
20129 int len = 0;
20130 int slen;
20131 int error = FALSE;
20132
20133 p = get_tv_string(&argvars[0]);
20134 slen = (int)STRLEN(p);
20135
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020136 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020137 if (!error)
20138 {
20139 if (nchar > 0)
20140 while (nchar > 0 && nbyte < slen)
20141 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020142 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020143 --nchar;
20144 }
20145 else
20146 nbyte = nchar;
20147 if (argvars[2].v_type != VAR_UNKNOWN)
20148 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020149 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020150 while (charlen > 0 && nbyte + len < slen)
20151 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020152 int off = nbyte + len;
20153
20154 if (off < 0)
20155 len += 1;
20156 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020157 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020158 --charlen;
20159 }
20160 }
20161 else
20162 len = slen - nbyte; /* default: all bytes that are available. */
20163 }
20164
20165 /*
20166 * Only return the overlap between the specified part and the actual
20167 * string.
20168 */
20169 if (nbyte < 0)
20170 {
20171 len += nbyte;
20172 nbyte = 0;
20173 }
20174 else if (nbyte > slen)
20175 nbyte = slen;
20176 if (len < 0)
20177 len = 0;
20178 else if (nbyte + len > slen)
20179 len = slen - nbyte;
20180
20181 rettv->v_type = VAR_STRING;
20182 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20183#else
20184 f_strpart(argvars, rettv);
20185#endif
20186}
20187
20188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 * "strpart()" function
20190 */
20191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020192f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193{
20194 char_u *p;
20195 int n;
20196 int len;
20197 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020198 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020200 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 slen = (int)STRLEN(p);
20202
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020203 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020204 if (error)
20205 len = 0;
20206 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020207 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 else
20209 len = slen - n; /* default len: all bytes that are available. */
20210
20211 /*
20212 * Only return the overlap between the specified part and the actual
20213 * string.
20214 */
20215 if (n < 0)
20216 {
20217 len += n;
20218 n = 0;
20219 }
20220 else if (n > slen)
20221 n = slen;
20222 if (len < 0)
20223 len = 0;
20224 else if (n + len > slen)
20225 len = slen - n;
20226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020227 rettv->v_type = VAR_STRING;
20228 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229}
20230
20231/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020232 * "strridx()" function
20233 */
20234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020235f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020236{
20237 char_u buf[NUMBUFLEN];
20238 char_u *needle;
20239 char_u *haystack;
20240 char_u *rest;
20241 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020242 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020244 needle = get_tv_string_chk(&argvars[1]);
20245 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020246
20247 rettv->vval.v_number = -1;
20248 if (needle == NULL || haystack == NULL)
20249 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020250
20251 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020252 if (argvars[2].v_type != VAR_UNKNOWN)
20253 {
20254 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020255 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020256 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020257 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020258 }
20259 else
20260 end_idx = haystack_len;
20261
Bram Moolenaar0d660222005-01-07 21:51:51 +000020262 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020263 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020264 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020265 lastmatch = haystack + end_idx;
20266 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020267 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020268 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020269 for (rest = haystack; *rest != '\0'; ++rest)
20270 {
20271 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020272 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020273 break;
20274 lastmatch = rest;
20275 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020276 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020277
20278 if (lastmatch == NULL)
20279 rettv->vval.v_number = -1;
20280 else
20281 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20282}
20283
20284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 * "strtrans()" function
20286 */
20287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020288f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020290 rettv->v_type = VAR_STRING;
20291 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292}
20293
20294/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020295 * "submatch()" function
20296 */
20297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020298f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020299{
Bram Moolenaar41571762014-04-02 19:00:58 +020020300 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020301 int no;
20302 int retList = 0;
20303
20304 no = (int)get_tv_number_chk(&argvars[0], &error);
20305 if (error)
20306 return;
20307 error = FALSE;
20308 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020309 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020310 if (error)
20311 return;
20312
20313 if (retList == 0)
20314 {
20315 rettv->v_type = VAR_STRING;
20316 rettv->vval.v_string = reg_submatch(no);
20317 }
20318 else
20319 {
20320 rettv->v_type = VAR_LIST;
20321 rettv->vval.v_list = reg_submatch_list(no);
20322 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020323}
20324
20325/*
20326 * "substitute()" function
20327 */
20328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020329f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020330{
20331 char_u patbuf[NUMBUFLEN];
20332 char_u subbuf[NUMBUFLEN];
20333 char_u flagsbuf[NUMBUFLEN];
20334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020335 char_u *str = get_tv_string_chk(&argvars[0]);
20336 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20337 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20338 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20339
Bram Moolenaar0d660222005-01-07 21:51:51 +000020340 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020341 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20342 rettv->vval.v_string = NULL;
20343 else
20344 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020345}
20346
20347/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020348 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020351f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352{
20353 int id = 0;
20354#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020355 linenr_T lnum;
20356 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020358 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020360 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020361 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20362 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020364 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020365 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020366 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367#endif
20368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020369 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370}
20371
20372/*
20373 * "synIDattr(id, what [, mode])" function
20374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020376f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377{
20378 char_u *p = NULL;
20379#ifdef FEAT_SYN_HL
20380 int id;
20381 char_u *what;
20382 char_u *mode;
20383 char_u modebuf[NUMBUFLEN];
20384 int modec;
20385
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020386 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020387 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020388 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020390 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020392 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 modec = 0; /* replace invalid with current */
20394 }
20395 else
20396 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020397#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020398 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 modec = 'g';
20400 else
20401#endif
20402 if (t_colors > 1)
20403 modec = 'c';
20404 else
20405 modec = 't';
20406 }
20407
20408
20409 switch (TOLOWER_ASC(what[0]))
20410 {
20411 case 'b':
20412 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20413 p = highlight_color(id, what, modec);
20414 else /* bold */
20415 p = highlight_has_attr(id, HL_BOLD, modec);
20416 break;
20417
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020418 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 p = highlight_color(id, what, modec);
20420 break;
20421
20422 case 'i':
20423 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20424 p = highlight_has_attr(id, HL_INVERSE, modec);
20425 else /* italic */
20426 p = highlight_has_attr(id, HL_ITALIC, modec);
20427 break;
20428
20429 case 'n': /* name */
20430 p = get_highlight_name(NULL, id - 1);
20431 break;
20432
20433 case 'r': /* reverse */
20434 p = highlight_has_attr(id, HL_INVERSE, modec);
20435 break;
20436
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020437 case 's':
20438 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20439 p = highlight_color(id, what, modec);
20440 else /* standout */
20441 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 break;
20443
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020444 case 'u':
20445 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20446 /* underline */
20447 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20448 else
20449 /* undercurl */
20450 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 break;
20452 }
20453
20454 if (p != NULL)
20455 p = vim_strsave(p);
20456#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020457 rettv->v_type = VAR_STRING;
20458 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459}
20460
20461/*
20462 * "synIDtrans(id)" function
20463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020465f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466{
20467 int id;
20468
20469#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020470 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471
20472 if (id > 0)
20473 id = syn_get_final_id(id);
20474 else
20475#endif
20476 id = 0;
20477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020478 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479}
20480
20481/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020482 * "synconcealed(lnum, col)" function
20483 */
20484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020485f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020486{
20487#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020488 linenr_T lnum;
20489 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020490 int syntax_flags = 0;
20491 int cchar;
20492 int matchid = 0;
20493 char_u str[NUMBUFLEN];
20494#endif
20495
20496 rettv->v_type = VAR_LIST;
20497 rettv->vval.v_list = NULL;
20498
20499#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20500 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020501 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020502
20503 vim_memset(str, NUL, sizeof(str));
20504
20505 if (rettv_list_alloc(rettv) != FAIL)
20506 {
20507 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20508 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20509 && curwin->w_p_cole > 0)
20510 {
20511 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20512 syntax_flags = get_syntax_info(&matchid);
20513
20514 /* get the conceal character */
20515 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20516 {
20517 cchar = syn_get_sub_char();
20518 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20519 cchar = lcs_conceal;
20520 if (cchar != NUL)
20521 {
20522# ifdef FEAT_MBYTE
20523 if (has_mbyte)
20524 (*mb_char2bytes)(cchar, str);
20525 else
20526# endif
20527 str[0] = cchar;
20528 }
20529 }
20530 }
20531
20532 list_append_number(rettv->vval.v_list,
20533 (syntax_flags & HL_CONCEAL) != 0);
20534 /* -1 to auto-determine strlen */
20535 list_append_string(rettv->vval.v_list, str, -1);
20536 list_append_number(rettv->vval.v_list, matchid);
20537 }
20538#endif
20539}
20540
20541/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020542 * "synstack(lnum, col)" function
20543 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020545f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020546{
20547#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020548 linenr_T lnum;
20549 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020550 int i;
20551 int id;
20552#endif
20553
20554 rettv->v_type = VAR_LIST;
20555 rettv->vval.v_list = NULL;
20556
20557#ifdef FEAT_SYN_HL
20558 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020559 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020560
20561 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020562 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020563 && rettv_list_alloc(rettv) != FAIL)
20564 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020565 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020566 for (i = 0; ; ++i)
20567 {
20568 id = syn_get_stack_item(i);
20569 if (id < 0)
20570 break;
20571 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20572 break;
20573 }
20574 }
20575#endif
20576}
20577
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020579get_cmd_output_as_rettv(
20580 typval_T *argvars,
20581 typval_T *rettv,
20582 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020584 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020586 char_u *infile = NULL;
20587 char_u buf[NUMBUFLEN];
20588 int err = FALSE;
20589 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020590 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020591 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020593 rettv->v_type = VAR_STRING;
20594 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020595 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020596 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020597
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020598 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020599 {
20600 /*
20601 * Write the string to a temp file, to be used for input of the shell
20602 * command.
20603 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020604 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020605 {
20606 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020607 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020608 }
20609
20610 fd = mch_fopen((char *)infile, WRITEBIN);
20611 if (fd == NULL)
20612 {
20613 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020614 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020615 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020616 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020617 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020618 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20619 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020620 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020621 else
20622 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020623 size_t len;
20624
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020625 p = get_tv_string_buf_chk(&argvars[1], buf);
20626 if (p == NULL)
20627 {
20628 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020629 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020630 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020631 len = STRLEN(p);
20632 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020633 err = TRUE;
20634 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020635 if (fclose(fd) != 0)
20636 err = TRUE;
20637 if (err)
20638 {
20639 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020640 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020641 }
20642 }
20643
Bram Moolenaar52a72462014-08-29 15:53:52 +020020644 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20645 * echoes typeahead, that messes up the display. */
20646 if (!msg_silent)
20647 flags += SHELL_COOKED;
20648
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020649 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020651 int len;
20652 listitem_T *li;
20653 char_u *s = NULL;
20654 char_u *start;
20655 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020656 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657
Bram Moolenaar52a72462014-08-29 15:53:52 +020020658 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020659 if (res == NULL)
20660 goto errret;
20661
20662 list = list_alloc();
20663 if (list == NULL)
20664 goto errret;
20665
20666 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020667 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020668 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020669 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020670 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020671 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020672
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020673 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020674 if (s == NULL)
20675 goto errret;
20676
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020677 for (p = s; start < end; ++p, ++start)
20678 *p = *start == NUL ? NL : *start;
20679 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020680
20681 li = listitem_alloc();
20682 if (li == NULL)
20683 {
20684 vim_free(s);
20685 goto errret;
20686 }
20687 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020688 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020689 li->li_tv.vval.v_string = s;
20690 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020692
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020693 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020694 rettv->v_type = VAR_LIST;
20695 rettv->vval.v_list = list;
20696 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020698 else
20699 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020700 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020701#ifdef USE_CR
20702 /* translate <CR> into <NL> */
20703 if (res != NULL)
20704 {
20705 char_u *s;
20706
20707 for (s = res; *s; ++s)
20708 {
20709 if (*s == CAR)
20710 *s = NL;
20711 }
20712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713#else
20714# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020715 /* translate <CR><NL> into <NL> */
20716 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020718 char_u *s, *d;
20719
20720 d = res;
20721 for (s = res; *s; ++s)
20722 {
20723 if (s[0] == CAR && s[1] == NL)
20724 ++s;
20725 *d++ = *s;
20726 }
20727 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729# endif
20730#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020731 rettv->vval.v_string = res;
20732 res = NULL;
20733 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020734
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020735errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020736 if (infile != NULL)
20737 {
20738 mch_remove(infile);
20739 vim_free(infile);
20740 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020741 if (res != NULL)
20742 vim_free(res);
20743 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020744 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020745}
20746
20747/*
20748 * "system()" function
20749 */
20750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020751f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020752{
20753 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20754}
20755
20756/*
20757 * "systemlist()" function
20758 */
20759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020760f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020761{
20762 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763}
20764
20765/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020766 * "tabpagebuflist()" function
20767 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020769f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020770{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020771#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020772 tabpage_T *tp;
20773 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020774
20775 if (argvars[0].v_type == VAR_UNKNOWN)
20776 wp = firstwin;
20777 else
20778 {
20779 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20780 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020781 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020782 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020783 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020784 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020785 for (; wp != NULL; wp = wp->w_next)
20786 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020787 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020788 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020789 }
20790#endif
20791}
20792
20793
20794/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020795 * "tabpagenr()" function
20796 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020798f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020799{
20800 int nr = 1;
20801#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020802 char_u *arg;
20803
20804 if (argvars[0].v_type != VAR_UNKNOWN)
20805 {
20806 arg = get_tv_string_chk(&argvars[0]);
20807 nr = 0;
20808 if (arg != NULL)
20809 {
20810 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020811 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020812 else
20813 EMSG2(_(e_invexpr2), arg);
20814 }
20815 }
20816 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020817 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020818#endif
20819 rettv->vval.v_number = nr;
20820}
20821
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020822
20823#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020824static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020825
20826/*
20827 * Common code for tabpagewinnr() and winnr().
20828 */
20829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020830get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020831{
20832 win_T *twin;
20833 int nr = 1;
20834 win_T *wp;
20835 char_u *arg;
20836
20837 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20838 if (argvar->v_type != VAR_UNKNOWN)
20839 {
20840 arg = get_tv_string_chk(argvar);
20841 if (arg == NULL)
20842 nr = 0; /* type error; errmsg already given */
20843 else if (STRCMP(arg, "$") == 0)
20844 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20845 else if (STRCMP(arg, "#") == 0)
20846 {
20847 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20848 if (twin == NULL)
20849 nr = 0;
20850 }
20851 else
20852 {
20853 EMSG2(_(e_invexpr2), arg);
20854 nr = 0;
20855 }
20856 }
20857
20858 if (nr > 0)
20859 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20860 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020861 {
20862 if (wp == NULL)
20863 {
20864 /* didn't find it in this tabpage */
20865 nr = 0;
20866 break;
20867 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020868 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020869 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020870 return nr;
20871}
20872#endif
20873
20874/*
20875 * "tabpagewinnr()" function
20876 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020878f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020879{
20880 int nr = 1;
20881#ifdef FEAT_WINDOWS
20882 tabpage_T *tp;
20883
20884 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20885 if (tp == NULL)
20886 nr = 0;
20887 else
20888 nr = get_winnr(tp, &argvars[1]);
20889#endif
20890 rettv->vval.v_number = nr;
20891}
20892
20893
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020894/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020895 * "tagfiles()" function
20896 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020898f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020899{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020900 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020901 tagname_T tn;
20902 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020903
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020904 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020905 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020906 fname = alloc(MAXPATHL);
20907 if (fname == NULL)
20908 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020909
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020910 for (first = TRUE; ; first = FALSE)
20911 if (get_tagfname(&tn, first, fname) == FAIL
20912 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020913 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020914 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020915 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020916}
20917
20918/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020919 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020920 */
20921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020922f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020923{
20924 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020925
20926 tag_pattern = get_tv_string(&argvars[0]);
20927
20928 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020929 if (*tag_pattern == NUL)
20930 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020931
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020932 if (rettv_list_alloc(rettv) == OK)
20933 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020934}
20935
20936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 * "tempname()" function
20938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020940f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941{
20942 static int x = 'A';
20943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020945 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946
20947 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20948 * names. Skip 'I' and 'O', they are used for shell redirection. */
20949 do
20950 {
20951 if (x == 'Z')
20952 x = '0';
20953 else if (x == '9')
20954 x = 'A';
20955 else
20956 {
20957#ifdef EBCDIC
20958 if (x == 'I')
20959 x = 'J';
20960 else if (x == 'R')
20961 x = 'S';
20962 else
20963#endif
20964 ++x;
20965 }
20966 } while (x == 'I' || x == 'O');
20967}
20968
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020969#ifdef FEAT_FLOAT
20970/*
20971 * "tan()" function
20972 */
20973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020974f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020975{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020976 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020977
20978 rettv->v_type = VAR_FLOAT;
20979 if (get_float_arg(argvars, &f) == OK)
20980 rettv->vval.v_float = tan(f);
20981 else
20982 rettv->vval.v_float = 0.0;
20983}
20984
20985/*
20986 * "tanh()" function
20987 */
20988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020989f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020990{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020991 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020992
20993 rettv->v_type = VAR_FLOAT;
20994 if (get_float_arg(argvars, &f) == OK)
20995 rettv->vval.v_float = tanh(f);
20996 else
20997 rettv->vval.v_float = 0.0;
20998}
20999#endif
21000
Bram Moolenaar574860b2016-05-24 17:33:34 +020021001/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021002 * "test_alloc_fail(id, countdown, repeat)" function
21003 */
21004 static void
21005f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
21006{
21007 if (argvars[0].v_type != VAR_NUMBER
21008 || argvars[0].vval.v_number <= 0
21009 || argvars[1].v_type != VAR_NUMBER
21010 || argvars[1].vval.v_number < 0
21011 || argvars[2].v_type != VAR_NUMBER)
21012 EMSG(_(e_invarg));
21013 else
21014 {
21015 alloc_fail_id = argvars[0].vval.v_number;
21016 if (alloc_fail_id >= aid_last)
21017 EMSG(_(e_invarg));
21018 alloc_fail_countdown = argvars[1].vval.v_number;
21019 alloc_fail_repeat = argvars[2].vval.v_number;
21020 did_outofmem_msg = FALSE;
21021 }
21022}
21023
21024/*
21025 * "test_disable_char_avail({expr})" function
21026 */
21027 static void
21028f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
21029{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021030 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021031}
21032
21033/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020021034 * "test_garbagecollect_now()" function
21035 */
21036 static void
21037f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
21038{
21039 /* This is dangerous, any Lists and Dicts used internally may be freed
21040 * while still in use. */
21041 garbage_collect(TRUE);
21042}
21043
21044#ifdef FEAT_JOB_CHANNEL
21045 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021046f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021047{
21048 rettv->v_type = VAR_CHANNEL;
21049 rettv->vval.v_channel = NULL;
21050}
21051#endif
21052
21053 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021054f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021055{
21056 rettv->v_type = VAR_DICT;
21057 rettv->vval.v_dict = NULL;
21058}
21059
21060#ifdef FEAT_JOB_CHANNEL
21061 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021062f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021063{
21064 rettv->v_type = VAR_JOB;
21065 rettv->vval.v_job = NULL;
21066}
21067#endif
21068
21069 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021070f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021071{
21072 rettv->v_type = VAR_LIST;
21073 rettv->vval.v_list = NULL;
21074}
21075
21076 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021077f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021078{
21079 rettv->v_type = VAR_PARTIAL;
21080 rettv->vval.v_partial = NULL;
21081}
21082
21083 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021084f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021085{
21086 rettv->v_type = VAR_STRING;
21087 rettv->vval.v_string = NULL;
21088}
21089
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021090 static void
21091f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
21092{
21093 time_for_testing = (time_t)get_tv_number(&argvars[0]);
21094}
21095
Bram Moolenaar975b5272016-03-15 23:10:59 +010021096#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
21097/*
21098 * Get a callback from "arg". It can be a Funcref or a function name.
21099 * When "arg" is zero return an empty string.
21100 * Return NULL for an invalid argument.
21101 */
21102 char_u *
21103get_callback(typval_T *arg, partial_T **pp)
21104{
21105 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
21106 {
21107 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010021108 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021109 return (*pp)->pt_name;
21110 }
21111 *pp = NULL;
21112 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
21113 return arg->vval.v_string;
21114 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
21115 return (char_u *)"";
21116 EMSG(_("E921: Invalid callback argument"));
21117 return NULL;
21118}
21119#endif
21120
21121#ifdef FEAT_TIMERS
21122/*
21123 * "timer_start(time, callback [, options])" function
21124 */
21125 static void
21126f_timer_start(typval_T *argvars, typval_T *rettv)
21127{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021128 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010021129 timer_T *timer;
21130 int repeat = 0;
21131 char_u *callback;
21132 dict_T *dict;
21133
Bram Moolenaar38499922016-04-22 20:46:52 +020021134 if (check_secure())
21135 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021136 if (argvars[2].v_type != VAR_UNKNOWN)
21137 {
21138 if (argvars[2].v_type != VAR_DICT
21139 || (dict = argvars[2].vval.v_dict) == NULL)
21140 {
21141 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21142 return;
21143 }
21144 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21145 repeat = get_dict_number(dict, (char_u *)"repeat");
21146 }
21147
21148 timer = create_timer(msec, repeat);
21149 callback = get_callback(&argvars[1], &timer->tr_partial);
21150 if (callback == NULL)
21151 {
21152 stop_timer(timer);
21153 rettv->vval.v_number = -1;
21154 }
21155 else
21156 {
21157 timer->tr_callback = vim_strsave(callback);
21158 rettv->vval.v_number = timer->tr_id;
21159 }
21160}
21161
21162/*
21163 * "timer_stop(timer)" function
21164 */
21165 static void
21166f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21167{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021168 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021169
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021170 if (argvars[0].v_type != VAR_NUMBER)
21171 {
Bram Moolenaared59aa62016-07-09 17:41:12 +020021172 EMSG(_(e_number_exp));
21173 return;
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021174 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021175 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021176 if (timer != NULL)
21177 stop_timer(timer);
21178}
21179#endif
21180
Bram Moolenaard52d9742005-08-21 22:20:28 +000021181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 * "tolower(string)" function
21183 */
21184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021185f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186{
21187 char_u *p;
21188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021189 p = vim_strsave(get_tv_string(&argvars[0]));
21190 rettv->v_type = VAR_STRING;
21191 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192
21193 if (p != NULL)
21194 while (*p != NUL)
21195 {
21196#ifdef FEAT_MBYTE
21197 int l;
21198
21199 if (enc_utf8)
21200 {
21201 int c, lc;
21202
21203 c = utf_ptr2char(p);
21204 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021205 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 /* TODO: reallocate string when byte count changes. */
21207 if (utf_char2len(lc) == l)
21208 utf_char2bytes(lc, p);
21209 p += l;
21210 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021211 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 p += l; /* skip multi-byte character */
21213 else
21214#endif
21215 {
21216 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21217 ++p;
21218 }
21219 }
21220}
21221
21222/*
21223 * "toupper(string)" function
21224 */
21225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021226f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021228 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021229 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230}
21231
21232/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021233 * "tr(string, fromstr, tostr)" function
21234 */
21235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021236f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021237{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021238 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021239 char_u *fromstr;
21240 char_u *tostr;
21241 char_u *p;
21242#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021243 int inlen;
21244 int fromlen;
21245 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021246 int idx;
21247 char_u *cpstr;
21248 int cplen;
21249 int first = TRUE;
21250#endif
21251 char_u buf[NUMBUFLEN];
21252 char_u buf2[NUMBUFLEN];
21253 garray_T ga;
21254
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021255 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021256 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21257 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021258
21259 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021260 rettv->v_type = VAR_STRING;
21261 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021262 if (fromstr == NULL || tostr == NULL)
21263 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021264 ga_init2(&ga, (int)sizeof(char), 80);
21265
21266#ifdef FEAT_MBYTE
21267 if (!has_mbyte)
21268#endif
21269 /* not multi-byte: fromstr and tostr must be the same length */
21270 if (STRLEN(fromstr) != STRLEN(tostr))
21271 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021272#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021273error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021274#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021275 EMSG2(_(e_invarg2), fromstr);
21276 ga_clear(&ga);
21277 return;
21278 }
21279
21280 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021281 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021282 {
21283#ifdef FEAT_MBYTE
21284 if (has_mbyte)
21285 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021286 inlen = (*mb_ptr2len)(in_str);
21287 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021288 cplen = inlen;
21289 idx = 0;
21290 for (p = fromstr; *p != NUL; p += fromlen)
21291 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021292 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021293 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021294 {
21295 for (p = tostr; *p != NUL; p += tolen)
21296 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021297 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021298 if (idx-- == 0)
21299 {
21300 cplen = tolen;
21301 cpstr = p;
21302 break;
21303 }
21304 }
21305 if (*p == NUL) /* tostr is shorter than fromstr */
21306 goto error;
21307 break;
21308 }
21309 ++idx;
21310 }
21311
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021312 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021313 {
21314 /* Check that fromstr and tostr have the same number of
21315 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021316 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021317 first = FALSE;
21318 for (p = tostr; *p != NUL; p += tolen)
21319 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021320 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021321 --idx;
21322 }
21323 if (idx != 0)
21324 goto error;
21325 }
21326
Bram Moolenaarcde88542015-08-11 19:14:00 +020021327 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021328 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021329 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021330
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021331 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021332 }
21333 else
21334#endif
21335 {
21336 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021337 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021338 if (p != NULL)
21339 ga_append(&ga, tostr[p - fromstr]);
21340 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021341 ga_append(&ga, *in_str);
21342 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021343 }
21344 }
21345
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021346 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021347 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021348 ga_append(&ga, NUL);
21349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021350 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021351}
21352
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021353#ifdef FEAT_FLOAT
21354/*
21355 * "trunc({float})" function
21356 */
21357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021358f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021359{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021360 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021361
21362 rettv->v_type = VAR_FLOAT;
21363 if (get_float_arg(argvars, &f) == OK)
21364 /* trunc() is not in C90, use floor() or ceil() instead. */
21365 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21366 else
21367 rettv->vval.v_float = 0.0;
21368}
21369#endif
21370
Bram Moolenaar8299df92004-07-10 09:47:34 +000021371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 * "type(expr)" function
21373 */
21374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021375f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021377 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021378
21379 switch (argvars[0].v_type)
21380 {
21381 case VAR_NUMBER: n = 0; break;
21382 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021383 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021384 case VAR_FUNC: n = 2; break;
21385 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021386 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021387 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021388 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021389 if (argvars[0].vval.v_number == VVAL_FALSE
21390 || argvars[0].vval.v_number == VVAL_TRUE)
21391 n = 6;
21392 else
21393 n = 7;
21394 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021395 case VAR_JOB: n = 8; break;
21396 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021397 case VAR_UNKNOWN:
21398 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21399 n = -1;
21400 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021401 }
21402 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403}
21404
21405/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021406 * "undofile(name)" function
21407 */
21408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021409f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021410{
21411 rettv->v_type = VAR_STRING;
21412#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021413 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021414 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021415
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021416 if (*fname == NUL)
21417 {
21418 /* If there is no file name there will be no undo file. */
21419 rettv->vval.v_string = NULL;
21420 }
21421 else
21422 {
21423 char_u *ffname = FullName_save(fname, FALSE);
21424
21425 if (ffname != NULL)
21426 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21427 vim_free(ffname);
21428 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021429 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021430#else
21431 rettv->vval.v_string = NULL;
21432#endif
21433}
21434
21435/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021436 * "undotree()" function
21437 */
21438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021439f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021440{
21441 if (rettv_dict_alloc(rettv) == OK)
21442 {
21443 dict_T *dict = rettv->vval.v_dict;
21444 list_T *list;
21445
Bram Moolenaar730cde92010-06-27 05:18:54 +020021446 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021447 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021448 dict_add_nr_str(dict, "save_last",
21449 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021450 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21451 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021452 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021453
21454 list = list_alloc();
21455 if (list != NULL)
21456 {
21457 u_eval_tree(curbuf->b_u_oldhead, list);
21458 dict_add_list(dict, "entries", list);
21459 }
21460 }
21461}
21462
21463/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021464 * "values(dict)" function
21465 */
21466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021467f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021468{
21469 dict_list(argvars, rettv, 1);
21470}
21471
21472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473 * "virtcol(string)" function
21474 */
21475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021476f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477{
21478 colnr_T vcol = 0;
21479 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021480 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021482 fp = var2fpos(&argvars[0], FALSE, &fnum);
21483 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21484 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 {
21486 getvvcol(curwin, fp, NULL, NULL, &vcol);
21487 ++vcol;
21488 }
21489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021490 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491}
21492
21493/*
21494 * "visualmode()" function
21495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021497f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 char_u str[2];
21500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021501 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 str[0] = curbuf->b_visual_mode_eval;
21503 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021504 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505
21506 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021507 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509}
21510
21511/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021512 * "wildmenumode()" function
21513 */
21514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021515f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021516{
21517#ifdef FEAT_WILDMENU
21518 if (wild_menu_showing)
21519 rettv->vval.v_number = 1;
21520#endif
21521}
21522
21523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 * "winbufnr(nr)" function
21525 */
21526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021527f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528{
21529 win_T *wp;
21530
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021531 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021535 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536}
21537
21538/*
21539 * "wincol()" function
21540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021542f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543{
21544 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021545 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546}
21547
21548/*
21549 * "winheight(nr)" function
21550 */
21551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021552f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553{
21554 win_T *wp;
21555
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021556 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021560 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561}
21562
21563/*
21564 * "winline()" function
21565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021567f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568{
21569 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021570 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571}
21572
21573/*
21574 * "winnr()" function
21575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021577f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578{
21579 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021580
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021582 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021584 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585}
21586
21587/*
21588 * "winrestcmd()" function
21589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021591f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592{
21593#ifdef FEAT_WINDOWS
21594 win_T *wp;
21595 int winnr = 1;
21596 garray_T ga;
21597 char_u buf[50];
21598
21599 ga_init2(&ga, (int)sizeof(char), 70);
21600 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21601 {
21602 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21603 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21605 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 ++winnr;
21607 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021608 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021610 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021612 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021614 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615}
21616
21617/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021618 * "winrestview()" function
21619 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021621f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021622{
21623 dict_T *dict;
21624
21625 if (argvars[0].v_type != VAR_DICT
21626 || (dict = argvars[0].vval.v_dict) == NULL)
21627 EMSG(_(e_invarg));
21628 else
21629 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021630 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021631 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021632 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021633 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021634#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021635 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021636 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021637#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021638 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21639 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021640 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021641 curwin->w_set_curswant = FALSE;
21642 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021643
Bram Moolenaar82c25852014-05-28 16:47:16 +020021644 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021645 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021646#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021647 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021648 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021649#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021650 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021651 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021652 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021653 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021654
21655 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021656 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021657# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021658 win_new_width(curwin, W_WIDTH(curwin));
21659# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021660 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021661
Bram Moolenaarb851a962014-10-31 15:45:52 +010021662 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021663 curwin->w_topline = 1;
21664 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21665 curwin->w_topline = curbuf->b_ml.ml_line_count;
21666#ifdef FEAT_DIFF
21667 check_topfill(curwin, TRUE);
21668#endif
21669 }
21670}
21671
21672/*
21673 * "winsaveview()" function
21674 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021676f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021677{
21678 dict_T *dict;
21679
Bram Moolenaara800b422010-06-27 01:15:55 +020021680 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021681 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021682 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021683
21684 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21685 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21686#ifdef FEAT_VIRTUALEDIT
21687 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21688#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021689 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021690 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21691
21692 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21693#ifdef FEAT_DIFF
21694 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21695#endif
21696 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21697 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21698}
21699
21700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701 * "winwidth(nr)" function
21702 */
21703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021704f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705{
21706 win_T *wp;
21707
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021708 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021710 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021712#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021713 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021715 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716#endif
21717}
21718
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021720 * "wordcount()" function
21721 */
21722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021723f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021724{
21725 if (rettv_dict_alloc(rettv) == FAIL)
21726 return;
21727 cursor_pos_info(rettv->vval.v_dict);
21728}
21729
21730/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021731 * Write list of strings to file
21732 */
21733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021734write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021735{
21736 listitem_T *li;
21737 int c;
21738 int ret = OK;
21739 char_u *s;
21740
21741 for (li = list->lv_first; li != NULL; li = li->li_next)
21742 {
21743 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21744 {
21745 if (*s == '\n')
21746 c = putc(NUL, fd);
21747 else
21748 c = putc(*s, fd);
21749 if (c == EOF)
21750 {
21751 ret = FAIL;
21752 break;
21753 }
21754 }
21755 if (!binary || li->li_next != NULL)
21756 if (putc('\n', fd) == EOF)
21757 {
21758 ret = FAIL;
21759 break;
21760 }
21761 if (ret == FAIL)
21762 {
21763 EMSG(_(e_write));
21764 break;
21765 }
21766 }
21767 return ret;
21768}
21769
21770/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021771 * "writefile()" function
21772 */
21773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021774f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021775{
21776 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021777 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021778 char_u *fname;
21779 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021780 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021781
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021782 if (check_restricted() || check_secure())
21783 return;
21784
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021785 if (argvars[0].v_type != VAR_LIST)
21786 {
21787 EMSG2(_(e_listarg), "writefile()");
21788 return;
21789 }
21790 if (argvars[0].vval.v_list == NULL)
21791 return;
21792
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021793 if (argvars[2].v_type != VAR_UNKNOWN)
21794 {
21795 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21796 binary = TRUE;
21797 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21798 append = TRUE;
21799 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021800
21801 /* Always open the file in binary mode, library functions have a mind of
21802 * their own about CR-LF conversion. */
21803 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021804 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21805 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021806 {
21807 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21808 ret = -1;
21809 }
21810 else
21811 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021812 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21813 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021814 fclose(fd);
21815 }
21816
21817 rettv->vval.v_number = ret;
21818}
21819
21820/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021821 * "xor(expr, expr)" function
21822 */
21823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021824f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021825{
21826 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21827 ^ get_tv_number_chk(&argvars[1], NULL);
21828}
21829
21830
21831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021833 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834 */
21835 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021836var2fpos(
21837 typval_T *varp,
21838 int dollar_lnum, /* TRUE when $ is last line */
21839 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021841 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021843 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844
Bram Moolenaara5525202006-03-02 22:52:09 +000021845 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021846 if (varp->v_type == VAR_LIST)
21847 {
21848 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021849 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021850 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021851 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021852
21853 l = varp->vval.v_list;
21854 if (l == NULL)
21855 return NULL;
21856
21857 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021858 pos.lnum = list_find_nr(l, 0L, &error);
21859 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021860 return NULL; /* invalid line number */
21861
21862 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021863 pos.col = list_find_nr(l, 1L, &error);
21864 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021865 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021866 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021867
21868 /* We accept "$" for the column number: last column. */
21869 li = list_find(l, 1L);
21870 if (li != NULL && li->li_tv.v_type == VAR_STRING
21871 && li->li_tv.vval.v_string != NULL
21872 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21873 pos.col = len + 1;
21874
Bram Moolenaara5525202006-03-02 22:52:09 +000021875 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021876 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021877 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021878 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021879
Bram Moolenaara5525202006-03-02 22:52:09 +000021880#ifdef FEAT_VIRTUALEDIT
21881 /* Get the virtual offset. Defaults to zero. */
21882 pos.coladd = list_find_nr(l, 2L, &error);
21883 if (error)
21884 pos.coladd = 0;
21885#endif
21886
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021887 return &pos;
21888 }
21889
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021890 name = get_tv_string_chk(varp);
21891 if (name == NULL)
21892 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021893 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021895 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21896 {
21897 if (VIsual_active)
21898 return &VIsual;
21899 return &curwin->w_cursor;
21900 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021901 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021903 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21905 return NULL;
21906 return pp;
21907 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021908
21909#ifdef FEAT_VIRTUALEDIT
21910 pos.coladd = 0;
21911#endif
21912
Bram Moolenaar477933c2007-07-17 14:32:23 +000021913 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021914 {
21915 pos.col = 0;
21916 if (name[1] == '0') /* "w0": first visible line */
21917 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021918 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021919 pos.lnum = curwin->w_topline;
21920 return &pos;
21921 }
21922 else if (name[1] == '$') /* "w$": last visible line */
21923 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021924 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021925 pos.lnum = curwin->w_botline - 1;
21926 return &pos;
21927 }
21928 }
21929 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021931 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932 {
21933 pos.lnum = curbuf->b_ml.ml_line_count;
21934 pos.col = 0;
21935 }
21936 else
21937 {
21938 pos.lnum = curwin->w_cursor.lnum;
21939 pos.col = (colnr_T)STRLEN(ml_get_curline());
21940 }
21941 return &pos;
21942 }
21943 return NULL;
21944}
21945
21946/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021947 * Convert list in "arg" into a position and optional file number.
21948 * When "fnump" is NULL there is no file number, only 3 items.
21949 * Note that the column is passed on as-is, the caller may want to decrement
21950 * it to use 1 for the first column.
21951 * Return FAIL when conversion is not possible, doesn't check the position for
21952 * validity.
21953 */
21954 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021955list2fpos(
21956 typval_T *arg,
21957 pos_T *posp,
21958 int *fnump,
21959 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021960{
21961 list_T *l = arg->vval.v_list;
21962 long i = 0;
21963 long n;
21964
Bram Moolenaar493c1782014-05-28 14:34:46 +020021965 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21966 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021967 if (arg->v_type != VAR_LIST
21968 || l == NULL
21969 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021970 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021971 return FAIL;
21972
21973 if (fnump != NULL)
21974 {
21975 n = list_find_nr(l, i++, NULL); /* fnum */
21976 if (n < 0)
21977 return FAIL;
21978 if (n == 0)
21979 n = curbuf->b_fnum; /* current buffer */
21980 *fnump = n;
21981 }
21982
21983 n = list_find_nr(l, i++, NULL); /* lnum */
21984 if (n < 0)
21985 return FAIL;
21986 posp->lnum = n;
21987
21988 n = list_find_nr(l, i++, NULL); /* col */
21989 if (n < 0)
21990 return FAIL;
21991 posp->col = n;
21992
21993#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021994 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021995 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021996 posp->coladd = 0;
21997 else
21998 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021999#endif
22000
Bram Moolenaar493c1782014-05-28 14:34:46 +020022001 if (curswantp != NULL)
22002 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
22003
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022004 return OK;
22005}
22006
22007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 * Get the length of an environment variable name.
22009 * Advance "arg" to the first character after the name.
22010 * Return 0 for error.
22011 */
22012 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022013get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014{
22015 char_u *p;
22016 int len;
22017
22018 for (p = *arg; vim_isIDc(*p); ++p)
22019 ;
22020 if (p == *arg) /* no name found */
22021 return 0;
22022
22023 len = (int)(p - *arg);
22024 *arg = p;
22025 return len;
22026}
22027
22028/*
22029 * Get the length of the name of a function or internal variable.
22030 * "arg" is advanced to the first non-white character after the name.
22031 * Return 0 if something is wrong.
22032 */
22033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022034get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035{
22036 char_u *p;
22037 int len;
22038
22039 /* Find the end of the name. */
22040 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022041 {
22042 if (*p == ':')
22043 {
22044 /* "s:" is start of "s:var", but "n:" is not and can be used in
22045 * slice "[n:]". Also "xx:" is not a namespace. */
22046 len = (int)(p - *arg);
22047 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
22048 || len > 1)
22049 break;
22050 }
22051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 if (p == *arg) /* no name found */
22053 return 0;
22054
22055 len = (int)(p - *arg);
22056 *arg = skipwhite(p);
22057
22058 return len;
22059}
22060
22061/*
Bram Moolenaara7043832005-01-21 11:56:39 +000022062 * Get the length of the name of a variable or function.
22063 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022065 * Return -1 if curly braces expansion failed.
22066 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 * If the name contains 'magic' {}'s, expand them and return the
22068 * expanded name in an allocated string via 'alias' - caller must free.
22069 */
22070 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022071get_name_len(
22072 char_u **arg,
22073 char_u **alias,
22074 int evaluate,
22075 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076{
22077 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 char_u *p;
22079 char_u *expr_start;
22080 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081
22082 *alias = NULL; /* default to no alias */
22083
22084 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
22085 && (*arg)[2] == (int)KE_SNR)
22086 {
22087 /* hard coded <SNR>, already translated */
22088 *arg += 3;
22089 return get_id_len(arg) + 3;
22090 }
22091 len = eval_fname_script(*arg);
22092 if (len > 0)
22093 {
22094 /* literal "<SID>", "s:" or "<SNR>" */
22095 *arg += len;
22096 }
22097
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022099 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022101 p = find_name_end(*arg, &expr_start, &expr_end,
22102 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 if (expr_start != NULL)
22104 {
22105 char_u *temp_string;
22106
22107 if (!evaluate)
22108 {
22109 len += (int)(p - *arg);
22110 *arg = skipwhite(p);
22111 return len;
22112 }
22113
22114 /*
22115 * Include any <SID> etc in the expanded string:
22116 * Thus the -len here.
22117 */
22118 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
22119 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022120 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121 *alias = temp_string;
22122 *arg = skipwhite(p);
22123 return (int)STRLEN(temp_string);
22124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125
22126 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022127 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 EMSG2(_(e_invexpr2), *arg);
22129
22130 return len;
22131}
22132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022133/*
22134 * Find the end of a variable or function name, taking care of magic braces.
22135 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
22136 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022137 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022138 * Return a pointer to just after the name. Equal to "arg" if there is no
22139 * valid name.
22140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022142find_name_end(
22143 char_u *arg,
22144 char_u **expr_start,
22145 char_u **expr_end,
22146 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022148 int mb_nest = 0;
22149 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022151 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022153 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022155 *expr_start = NULL;
22156 *expr_end = NULL;
22157 }
22158
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022159 /* Quick check for valid starting character. */
22160 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22161 return arg;
22162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022163 for (p = arg; *p != NUL
22164 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022165 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022166 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022167 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022168 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022169 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022170 if (*p == '\'')
22171 {
22172 /* skip over 'string' to avoid counting [ and ] inside it. */
22173 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22174 ;
22175 if (*p == NUL)
22176 break;
22177 }
22178 else if (*p == '"')
22179 {
22180 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22181 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22182 if (*p == '\\' && p[1] != NUL)
22183 ++p;
22184 if (*p == NUL)
22185 break;
22186 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022187 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22188 {
22189 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022190 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022191 len = (int)(p - arg);
22192 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022193 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022194 break;
22195 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022197 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022199 if (*p == '[')
22200 ++br_nest;
22201 else if (*p == ']')
22202 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022205 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022207 if (*p == '{')
22208 {
22209 mb_nest++;
22210 if (expr_start != NULL && *expr_start == NULL)
22211 *expr_start = p;
22212 }
22213 else if (*p == '}')
22214 {
22215 mb_nest--;
22216 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22217 *expr_end = p;
22218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 }
22221
22222 return p;
22223}
22224
22225/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022226 * Expands out the 'magic' {}'s in a variable/function name.
22227 * Note that this can call itself recursively, to deal with
22228 * constructs like foo{bar}{baz}{bam}
22229 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22230 * "in_start" ^
22231 * "expr_start" ^
22232 * "expr_end" ^
22233 * "in_end" ^
22234 *
22235 * Returns a new allocated string, which the caller must free.
22236 * Returns NULL for failure.
22237 */
22238 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022239make_expanded_name(
22240 char_u *in_start,
22241 char_u *expr_start,
22242 char_u *expr_end,
22243 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022244{
22245 char_u c1;
22246 char_u *retval = NULL;
22247 char_u *temp_result;
22248 char_u *nextcmd = NULL;
22249
22250 if (expr_end == NULL || in_end == NULL)
22251 return NULL;
22252 *expr_start = NUL;
22253 *expr_end = NUL;
22254 c1 = *in_end;
22255 *in_end = NUL;
22256
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022257 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022258 if (temp_result != NULL && nextcmd == NULL)
22259 {
22260 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22261 + (in_end - expr_end) + 1));
22262 if (retval != NULL)
22263 {
22264 STRCPY(retval, in_start);
22265 STRCAT(retval, temp_result);
22266 STRCAT(retval, expr_end + 1);
22267 }
22268 }
22269 vim_free(temp_result);
22270
22271 *in_end = c1; /* put char back for error messages */
22272 *expr_start = '{';
22273 *expr_end = '}';
22274
22275 if (retval != NULL)
22276 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022277 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022278 if (expr_start != NULL)
22279 {
22280 /* Further expansion! */
22281 temp_result = make_expanded_name(retval, expr_start,
22282 expr_end, temp_result);
22283 vim_free(retval);
22284 retval = temp_result;
22285 }
22286 }
22287
22288 return retval;
22289}
22290
22291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022293 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 */
22295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022296eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022298 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22299}
22300
22301/*
22302 * Return TRUE if character "c" can be used as the first character in a
22303 * variable or function name (excluding '{' and '}').
22304 */
22305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022306eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022307{
22308 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309}
22310
22311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312 * Set number v: variable to "val".
22313 */
22314 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022315set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022317 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318}
22319
22320/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022321 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022323 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022324get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022326 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327}
22328
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022329/*
22330 * Get string v: variable value. Uses a static buffer, can only be used once.
22331 */
22332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022333get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022334{
22335 return get_tv_string(&vimvars[idx].vv_tv);
22336}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022337
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022339 * Get List v: variable value. Caller must take care of reference count when
22340 * needed.
22341 */
22342 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022343get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022344{
22345 return vimvars[idx].vv_list;
22346}
22347
22348/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022349 * Set v:char to character "c".
22350 */
22351 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022352set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022353{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022354 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022355
22356#ifdef FEAT_MBYTE
22357 if (has_mbyte)
22358 buf[(*mb_char2bytes)(c, buf)] = NUL;
22359 else
22360#endif
22361 {
22362 buf[0] = c;
22363 buf[1] = NUL;
22364 }
22365 set_vim_var_string(VV_CHAR, buf, -1);
22366}
22367
22368/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022369 * Set v:count to "count" and v:count1 to "count1".
22370 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371 */
22372 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022373set_vcount(
22374 long count,
22375 long count1,
22376 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022378 if (set_prevcount)
22379 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022380 vimvars[VV_COUNT].vv_nr = count;
22381 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382}
22383
22384/*
22385 * Set string v: variable to a copy of "val".
22386 */
22387 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022388set_vim_var_string(
22389 int idx,
22390 char_u *val,
22391 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392{
Bram Moolenaara542c682016-01-31 16:28:04 +010022393 clear_tv(&vimvars[idx].vv_di.di_tv);
22394 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022396 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022398 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022400 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401}
22402
22403/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022404 * Set List v: variable to "val".
22405 */
22406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022407set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022408{
Bram Moolenaara542c682016-01-31 16:28:04 +010022409 clear_tv(&vimvars[idx].vv_di.di_tv);
22410 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022411 vimvars[idx].vv_list = val;
22412 if (val != NULL)
22413 ++val->lv_refcount;
22414}
22415
22416/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022417 * Set Dictionary v: variable to "val".
22418 */
22419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022420set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022421{
22422 int todo;
22423 hashitem_T *hi;
22424
Bram Moolenaara542c682016-01-31 16:28:04 +010022425 clear_tv(&vimvars[idx].vv_di.di_tv);
22426 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022427 vimvars[idx].vv_dict = val;
22428 if (val != NULL)
22429 {
22430 ++val->dv_refcount;
22431
22432 /* Set readonly */
22433 todo = (int)val->dv_hashtab.ht_used;
22434 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22435 {
22436 if (HASHITEM_EMPTY(hi))
22437 continue;
22438 --todo;
22439 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22440 }
22441 }
22442}
22443
22444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 * Set v:register if needed.
22446 */
22447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022448set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449{
22450 char_u regname;
22451
22452 if (c == 0 || c == ' ')
22453 regname = '"';
22454 else
22455 regname = c;
22456 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022457 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 set_vim_var_string(VV_REG, &regname, 1);
22459}
22460
22461/*
22462 * Get or set v:exception. If "oldval" == NULL, return the current value.
22463 * Otherwise, restore the value to "oldval" and return NULL.
22464 * Must always be called in pairs to save and restore v:exception! Does not
22465 * take care of memory allocations.
22466 */
22467 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022468v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469{
22470 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022471 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472
Bram Moolenaare9a41262005-01-15 22:18:47 +000022473 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 return NULL;
22475}
22476
22477/*
22478 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22479 * Otherwise, restore the value to "oldval" and return NULL.
22480 * Must always be called in pairs to save and restore v:throwpoint! Does not
22481 * take care of memory allocations.
22482 */
22483 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022484v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485{
22486 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022487 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488
Bram Moolenaare9a41262005-01-15 22:18:47 +000022489 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490 return NULL;
22491}
22492
22493#if defined(FEAT_AUTOCMD) || defined(PROTO)
22494/*
22495 * Set v:cmdarg.
22496 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22497 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22498 * Must always be called in pairs!
22499 */
22500 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022501set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502{
22503 char_u *oldval;
22504 char_u *newval;
22505 unsigned len;
22506
Bram Moolenaare9a41262005-01-15 22:18:47 +000022507 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022508 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022510 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022511 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022512 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 }
22514
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022515 if (eap->force_bin == FORCE_BIN)
22516 len = 6;
22517 else if (eap->force_bin == FORCE_NOBIN)
22518 len = 8;
22519 else
22520 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022521
22522 if (eap->read_edit)
22523 len += 7;
22524
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022525 if (eap->force_ff != 0)
22526 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22527# ifdef FEAT_MBYTE
22528 if (eap->force_enc != 0)
22529 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022530 if (eap->bad_char != 0)
22531 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022532# endif
22533
22534 newval = alloc(len + 1);
22535 if (newval == NULL)
22536 return NULL;
22537
22538 if (eap->force_bin == FORCE_BIN)
22539 sprintf((char *)newval, " ++bin");
22540 else if (eap->force_bin == FORCE_NOBIN)
22541 sprintf((char *)newval, " ++nobin");
22542 else
22543 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022544
22545 if (eap->read_edit)
22546 STRCAT(newval, " ++edit");
22547
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022548 if (eap->force_ff != 0)
22549 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22550 eap->cmd + eap->force_ff);
22551# ifdef FEAT_MBYTE
22552 if (eap->force_enc != 0)
22553 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22554 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022555 if (eap->bad_char == BAD_KEEP)
22556 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22557 else if (eap->bad_char == BAD_DROP)
22558 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22559 else if (eap->bad_char != 0)
22560 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022561# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022562 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022563 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564}
22565#endif
22566
22567/*
22568 * Get the value of internal variable "name".
22569 * Return OK or FAIL.
22570 */
22571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022572get_var_tv(
22573 char_u *name,
22574 int len, /* length of "name" */
22575 typval_T *rettv, /* NULL when only checking existence */
22576 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22577 int verbose, /* may give error message */
22578 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579{
22580 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022581 typval_T *tv = NULL;
22582 typval_T atv;
22583 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585
22586 /* truncate the name, so that we can use strcmp() */
22587 cc = name[len];
22588 name[len] = NUL;
22589
22590 /*
22591 * Check for "b:changedtick".
22592 */
22593 if (STRCMP(name, "b:changedtick") == 0)
22594 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022595 atv.v_type = VAR_NUMBER;
22596 atv.vval.v_number = curbuf->b_changedtick;
22597 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 }
22599
22600 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 * Check for user-defined variables.
22602 */
22603 else
22604 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022605 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022607 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022608 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022609 if (dip != NULL)
22610 *dip = v;
22611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 }
22613
Bram Moolenaare9a41262005-01-15 22:18:47 +000022614 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022616 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022617 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 ret = FAIL;
22619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022620 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022621 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622
22623 name[len] = cc;
22624
22625 return ret;
22626}
22627
22628/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022629 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22630 * Also handle function call with Funcref variable: func(expr)
22631 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22632 */
22633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022634handle_subscript(
22635 char_u **arg,
22636 typval_T *rettv,
22637 int evaluate, /* do more than finding the end */
22638 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022639{
22640 int ret = OK;
22641 dict_T *selfdict = NULL;
22642 char_u *s;
22643 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022644 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022645
22646 while (ret == OK
22647 && (**arg == '['
22648 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022649 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22650 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022651 && !vim_iswhite(*(*arg - 1)))
22652 {
22653 if (**arg == '(')
22654 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022655 partial_T *pt = NULL;
22656
Bram Moolenaard9fba312005-06-26 22:34:35 +000022657 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022658 if (evaluate)
22659 {
22660 functv = *rettv;
22661 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022662
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022663 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022664 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022665 {
22666 pt = functv.vval.v_partial;
22667 s = pt->pt_name;
22668 }
22669 else
22670 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022671 }
22672 else
22673 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022674 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022675 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022676 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022677
22678 /* Clear the funcref afterwards, so that deleting it while
22679 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022680 if (evaluate)
22681 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022682
22683 /* Stop the expression evaluation when immediately aborting on
22684 * error, or when an interrupt occurred or an exception was thrown
22685 * but not caught. */
22686 if (aborting())
22687 {
22688 if (ret == OK)
22689 clear_tv(rettv);
22690 ret = FAIL;
22691 }
22692 dict_unref(selfdict);
22693 selfdict = NULL;
22694 }
22695 else /* **arg == '[' || **arg == '.' */
22696 {
22697 dict_unref(selfdict);
22698 if (rettv->v_type == VAR_DICT)
22699 {
22700 selfdict = rettv->vval.v_dict;
22701 if (selfdict != NULL)
22702 ++selfdict->dv_refcount;
22703 }
22704 else
22705 selfdict = NULL;
22706 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22707 {
22708 clear_tv(rettv);
22709 ret = FAIL;
22710 }
22711 }
22712 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022713
Bram Moolenaar1d429612016-05-24 15:44:17 +020022714 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22715 * Don't do this when "Func" is already a partial that was bound
22716 * explicitly (pt_auto is FALSE). */
22717 if (selfdict != NULL
22718 && (rettv->v_type == VAR_FUNC
22719 || (rettv->v_type == VAR_PARTIAL
22720 && (rettv->vval.v_partial->pt_auto
22721 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022722 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022723 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22724 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022725 char_u *tofree = NULL;
22726 ufunc_T *fp;
22727 char_u fname_buf[FLEN_FIXED + 1];
22728 int error;
22729
22730 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022731 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022732 fp = find_func(fname);
22733 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022734
Bram Moolenaar65639032016-03-16 21:40:30 +010022735 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022736 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022737 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22738
22739 if (pt != NULL)
22740 {
22741 pt->pt_refcount = 1;
22742 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022743 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022744 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022745 if (rettv->v_type == VAR_FUNC)
22746 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022747 /* Just a function: Take over the function name and use
22748 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022749 pt->pt_name = rettv->vval.v_string;
22750 }
22751 else
22752 {
22753 partial_T *ret_pt = rettv->vval.v_partial;
22754 int i;
22755
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022756 /* Partial: copy the function name, use selfdict and copy
22757 * args. Can't take over name or args, the partial might
22758 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022759 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022760 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022761 if (ret_pt->pt_argc > 0)
22762 {
22763 pt->pt_argv = (typval_T *)alloc(
22764 sizeof(typval_T) * ret_pt->pt_argc);
22765 if (pt->pt_argv == NULL)
22766 /* out of memory: drop the arguments */
22767 pt->pt_argc = 0;
22768 else
22769 {
22770 pt->pt_argc = ret_pt->pt_argc;
22771 for (i = 0; i < pt->pt_argc; i++)
22772 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22773 }
22774 }
22775 partial_unref(ret_pt);
22776 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022777 rettv->v_type = VAR_PARTIAL;
22778 rettv->vval.v_partial = pt;
22779 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022780 }
22781 }
22782
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022783 dict_unref(selfdict);
22784 return ret;
22785}
22786
22787/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022788 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022789 * value).
22790 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022791 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022792alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022793{
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022795}
22796
22797/*
22798 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 * The string "s" must have been allocated, it is consumed.
22800 * Return NULL for out of memory, the variable otherwise.
22801 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022802 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022803alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804{
Bram Moolenaar33570922005-01-25 22:26:29 +000022805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022807 rettv = alloc_tv();
22808 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022810 rettv->v_type = VAR_STRING;
22811 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 }
22813 else
22814 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022815 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816}
22817
22818/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022819 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022822free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823{
22824 if (varp != NULL)
22825 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022826 switch (varp->v_type)
22827 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022828 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022829 func_unref(varp->vval.v_string);
22830 /*FALLTHROUGH*/
22831 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022832 vim_free(varp->vval.v_string);
22833 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022834 case VAR_PARTIAL:
22835 partial_unref(varp->vval.v_partial);
22836 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022837 case VAR_LIST:
22838 list_unref(varp->vval.v_list);
22839 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022840 case VAR_DICT:
22841 dict_unref(varp->vval.v_dict);
22842 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022843 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022844#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022845 job_unref(varp->vval.v_job);
22846 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022847#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022848 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022849#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022850 channel_unref(varp->vval.v_channel);
22851 break;
22852#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022853 case VAR_NUMBER:
22854 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022855 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022856 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022857 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 vim_free(varp);
22860 }
22861}
22862
22863/*
22864 * Free the memory for a variable value and set the value to NULL or 0.
22865 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022866 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022867clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868{
22869 if (varp != NULL)
22870 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022871 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022873 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022874 func_unref(varp->vval.v_string);
22875 /*FALLTHROUGH*/
22876 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022877 vim_free(varp->vval.v_string);
22878 varp->vval.v_string = NULL;
22879 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022880 case VAR_PARTIAL:
22881 partial_unref(varp->vval.v_partial);
22882 varp->vval.v_partial = NULL;
22883 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022884 case VAR_LIST:
22885 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022886 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022887 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022888 case VAR_DICT:
22889 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022890 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022891 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022892 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022893 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022894 varp->vval.v_number = 0;
22895 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022896 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022897#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022898 varp->vval.v_float = 0.0;
22899 break;
22900#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022901 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022902#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022903 job_unref(varp->vval.v_job);
22904 varp->vval.v_job = NULL;
22905#endif
22906 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022907 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022908#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022909 channel_unref(varp->vval.v_channel);
22910 varp->vval.v_channel = NULL;
22911#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022912 case VAR_UNKNOWN:
22913 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022915 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 }
22917}
22918
22919/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022920 * Set the value of a variable to NULL without freeing items.
22921 */
22922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022923init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022924{
22925 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022926 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022927}
22928
22929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930 * Get the number value of a variable.
22931 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022932 * For incompatible types, return 0.
22933 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22934 * caller of incompatible types: it sets *denote to TRUE if "denote"
22935 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022937 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022938get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022940 int error = FALSE;
22941
22942 return get_tv_number_chk(varp, &error); /* return 0L on error */
22943}
22944
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022945 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022946get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022947{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022948 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022950 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022952 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022953 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022954 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022955#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022956 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022957 break;
22958#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022959 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022960 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022961 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022962 break;
22963 case VAR_STRING:
22964 if (varp->vval.v_string != NULL)
22965 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022966 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022967 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022968 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022969 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022970 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022971 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022972 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022973 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022974 case VAR_SPECIAL:
22975 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22976 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022977 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022978#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022979 EMSG(_("E910: Using a Job as a Number"));
22980 break;
22981#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022982 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022983#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022984 EMSG(_("E913: Using a Channel as a Number"));
22985 break;
22986#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022987 case VAR_UNKNOWN:
22988 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022991 if (denote == NULL) /* useful for values that must be unsigned */
22992 n = -1;
22993 else
22994 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022995 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996}
22997
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022998#ifdef FEAT_FLOAT
22999 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023000get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023001{
23002 switch (varp->v_type)
23003 {
23004 case VAR_NUMBER:
23005 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023006 case VAR_FLOAT:
23007 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023008 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023009 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023010 EMSG(_("E891: Using a Funcref as a Float"));
23011 break;
23012 case VAR_STRING:
23013 EMSG(_("E892: Using a String as a Float"));
23014 break;
23015 case VAR_LIST:
23016 EMSG(_("E893: Using a List as a Float"));
23017 break;
23018 case VAR_DICT:
23019 EMSG(_("E894: Using a Dictionary as a Float"));
23020 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023021 case VAR_SPECIAL:
23022 EMSG(_("E907: Using a special value as a Float"));
23023 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023024 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023025# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023026 EMSG(_("E911: Using a Job as a Float"));
23027 break;
23028# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023029 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023030# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023031 EMSG(_("E914: Using a Channel as a Float"));
23032 break;
23033# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010023034 case VAR_UNKNOWN:
23035 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023036 break;
23037 }
23038 return 0;
23039}
23040#endif
23041
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023043 * Get the lnum from the first argument.
23044 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023045 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 */
23047 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023048get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049{
Bram Moolenaar33570922005-01-25 22:26:29 +000023050 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051 linenr_T lnum;
23052
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023053 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 if (lnum == 0) /* no valid number, try using line() */
23055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023056 rettv.v_type = VAR_NUMBER;
23057 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023058 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023059 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 }
23061 return lnum;
23062}
23063
23064/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023065 * Get the lnum from the first argument.
23066 * Also accepts "$", then "buf" is used.
23067 * Returns 0 on error.
23068 */
23069 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023070get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000023071{
23072 if (argvars[0].v_type == VAR_STRING
23073 && argvars[0].vval.v_string != NULL
23074 && argvars[0].vval.v_string[0] == '$'
23075 && buf != NULL)
23076 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023077 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000023078}
23079
23080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 * Get the string value of a variable.
23082 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000023083 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23084 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 * If the String variable has never been set, return an empty string.
23086 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023087 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
23088 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023090 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023091get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092{
23093 static char_u mybuf[NUMBUFLEN];
23094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023095 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096}
23097
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023098 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023099get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023101 char_u *res = get_tv_string_buf_chk(varp, buf);
23102
23103 return res != NULL ? res : (char_u *)"";
23104}
23105
Bram Moolenaar7d647822014-04-05 21:28:56 +020023106/*
23107 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23108 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000023109 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023110get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023111{
23112 static char_u mybuf[NUMBUFLEN];
23113
23114 return get_tv_string_buf_chk(varp, mybuf);
23115}
23116
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023117 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023118get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023119{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023120 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023122 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023123 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
23124 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023125 return buf;
23126 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023127 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023128 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023129 break;
23130 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023131 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000023132 break;
23133 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023134 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023135 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023136 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023137#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020023138 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023139 break;
23140#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023141 case VAR_STRING:
23142 if (varp->vval.v_string != NULL)
23143 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023144 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023145 case VAR_SPECIAL:
23146 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23147 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023148 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023149#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023150 {
23151 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023152 char *status;
23153
23154 if (job == NULL)
23155 return (char_u *)"no process";
23156 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023157 : job->jv_status == JOB_ENDED ? "dead"
23158 : "run";
23159# ifdef UNIX
23160 vim_snprintf((char *)buf, NUMBUFLEN,
23161 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023162# elif defined(WIN32)
23163 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023164 "process %ld %s",
23165 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023166 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023167# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023168 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023169 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23170# endif
23171 return buf;
23172 }
23173#endif
23174 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023175 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023176#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023177 {
23178 channel_T *channel = varp->vval.v_channel;
23179 char *status = channel_status(channel);
23180
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023181 if (channel == NULL)
23182 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23183 else
23184 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023185 "channel %d %s", channel->ch_id, status);
23186 return buf;
23187 }
23188#endif
23189 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023190 case VAR_UNKNOWN:
23191 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023192 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023194 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195}
23196
23197/*
23198 * Find variable "name" in the list of variables.
23199 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023200 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023201 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023202 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023204 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023205find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023208 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209
Bram Moolenaara7043832005-01-21 11:56:39 +000023210 ht = find_var_ht(name, &varname);
23211 if (htp != NULL)
23212 *htp = ht;
23213 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023215 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216}
23217
23218/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023219 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023220 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023222 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023223find_var_in_ht(
23224 hashtab_T *ht,
23225 int htname,
23226 char_u *varname,
23227 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023228{
Bram Moolenaar33570922005-01-25 22:26:29 +000023229 hashitem_T *hi;
23230
23231 if (*varname == NUL)
23232 {
23233 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023234 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023235 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023236 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023237 case 'g': return &globvars_var;
23238 case 'v': return &vimvars_var;
23239 case 'b': return &curbuf->b_bufvar;
23240 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023241#ifdef FEAT_WINDOWS
23242 case 't': return &curtab->tp_winvar;
23243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023244 case 'l': return current_funccal == NULL
23245 ? NULL : &current_funccal->l_vars_var;
23246 case 'a': return current_funccal == NULL
23247 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023248 }
23249 return NULL;
23250 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023251
23252 hi = hash_find(ht, varname);
23253 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023254 {
23255 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023256 * worked find the variable again. Don't auto-load a script if it was
23257 * loaded already, otherwise it would be loaded every time when
23258 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023259 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023260 {
23261 /* Note: script_autoload() may make "hi" invalid. It must either
23262 * be obtained again or not used. */
23263 if (!script_autoload(varname, FALSE) || aborting())
23264 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023265 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023266 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023267 if (HASHITEM_EMPTY(hi))
23268 return NULL;
23269 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023270 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023271}
23272
23273/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023274 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023275 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023276 * Set "varname" to the start of name without ':'.
23277 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023278 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023279find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023281 hashitem_T *hi;
23282
Bram Moolenaar73627d02015-08-11 15:46:09 +020023283 if (name[0] == NUL)
23284 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285 if (name[1] != ':')
23286 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023287 /* The name must not start with a colon or #. */
23288 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 return NULL;
23290 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023291
23292 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023293 hi = hash_find(&compat_hashtab, name);
23294 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023295 return &compat_hashtab;
23296
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023298 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023299 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300 }
23301 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023302 if (*name == 'g') /* global variable */
23303 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023304 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23305 */
23306 if (vim_strchr(name + 2, ':') != NULL
23307 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023308 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023310 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023312 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023313#ifdef FEAT_WINDOWS
23314 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023315 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023316#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023317 if (*name == 'v') /* v: variable */
23318 return &vimvarht;
23319 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023320 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023321 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023322 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 if (*name == 's' /* script variable */
23324 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23325 return &SCRIPT_VARS(current_SID);
23326 return NULL;
23327}
23328
23329/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023330 * Get function call environment based on bactrace debug level
23331 */
23332 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023333get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023334{
23335 int i;
23336 funccall_T *funccal;
23337 funccall_T *temp_funccal;
23338
23339 funccal = current_funccal;
23340 if (debug_backtrace_level > 0)
23341 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023342 for (i = 0; i < debug_backtrace_level; i++)
23343 {
23344 temp_funccal = funccal->caller;
23345 if (temp_funccal)
23346 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023347 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023348 /* backtrace level overflow. reset to max */
23349 debug_backtrace_level = i;
23350 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023351 }
23352 return funccal;
23353}
23354
23355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023357 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 * Returns NULL when it doesn't exist.
23359 */
23360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023361get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362{
Bram Moolenaar33570922005-01-25 22:26:29 +000023363 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023365 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 if (v == NULL)
23367 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023368 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369}
23370
23371/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023372 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373 * sourcing this script and when executing functions defined in the script.
23374 */
23375 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023376new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377{
Bram Moolenaara7043832005-01-21 11:56:39 +000023378 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023379 hashtab_T *ht;
23380 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023381
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23383 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023384 /* Re-allocating ga_data means that an ht_array pointing to
23385 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023386 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023387 for (i = 1; i <= ga_scripts.ga_len; ++i)
23388 {
23389 ht = &SCRIPT_VARS(i);
23390 if (ht->ht_mask == HT_INIT_SIZE - 1)
23391 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023392 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023393 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023394 }
23395
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 while (ga_scripts.ga_len < id)
23397 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023398 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023399 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023400 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 }
23403 }
23404}
23405
23406/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023407 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23408 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409 */
23410 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023411init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412{
Bram Moolenaar33570922005-01-25 22:26:29 +000023413 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023414 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023415 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023416 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023417 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023418 dict_var->di_tv.vval.v_dict = dict;
23419 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023420 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023421 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23422 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423}
23424
23425/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023426 * Unreference a dictionary initialized by init_var_dict().
23427 */
23428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023429unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023430{
23431 /* Now the dict needs to be freed if no one else is using it, go back to
23432 * normal reference counting. */
23433 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23434 dict_unref(dict);
23435}
23436
23437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023439 * Frees all allocated variables and the value they contain.
23440 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 */
23442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023443vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023444{
23445 vars_clear_ext(ht, TRUE);
23446}
23447
23448/*
23449 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23450 */
23451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023452vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453{
Bram Moolenaara7043832005-01-21 11:56:39 +000023454 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023455 hashitem_T *hi;
23456 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457
Bram Moolenaar33570922005-01-25 22:26:29 +000023458 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023459 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023460 for (hi = ht->ht_array; todo > 0; ++hi)
23461 {
23462 if (!HASHITEM_EMPTY(hi))
23463 {
23464 --todo;
23465
Bram Moolenaar33570922005-01-25 22:26:29 +000023466 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023467 * ht_array might change then. hash_clear() takes care of it
23468 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023469 v = HI2DI(hi);
23470 if (free_val)
23471 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023472 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023473 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023474 }
23475 }
23476 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023477 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478}
23479
Bram Moolenaara7043832005-01-21 11:56:39 +000023480/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023481 * Delete a variable from hashtab "ht" at item "hi".
23482 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023485delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486{
Bram Moolenaar33570922005-01-25 22:26:29 +000023487 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023488
23489 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023490 clear_tv(&di->di_tv);
23491 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492}
23493
23494/*
23495 * List the value of one internal variable.
23496 */
23497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023498list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023500 char_u *tofree;
23501 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023502 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023503
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023504 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023505 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023506 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023507 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508}
23509
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023511list_one_var_a(
23512 char_u *prefix,
23513 char_u *name,
23514 int type,
23515 char_u *string,
23516 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517{
Bram Moolenaar31859182007-08-14 20:41:13 +000023518 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23519 msg_start();
23520 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 if (name != NULL) /* "a:" vars don't have a name stored */
23522 msg_puts(name);
23523 msg_putchar(' ');
23524 msg_advance(22);
23525 if (type == VAR_NUMBER)
23526 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023527 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023528 msg_putchar('*');
23529 else if (type == VAR_LIST)
23530 {
23531 msg_putchar('[');
23532 if (*string == '[')
23533 ++string;
23534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023535 else if (type == VAR_DICT)
23536 {
23537 msg_putchar('{');
23538 if (*string == '{')
23539 ++string;
23540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 else
23542 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023543
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023545
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023546 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023547 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023548 if (*first)
23549 {
23550 msg_clr_eos();
23551 *first = FALSE;
23552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553}
23554
23555/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023556 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 * If the variable already exists, the value is updated.
23558 * Otherwise the variable is created.
23559 */
23560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023561set_var(
23562 char_u *name,
23563 typval_T *tv,
23564 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565{
Bram Moolenaar33570922005-01-25 22:26:29 +000023566 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023568 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023570 ht = find_var_ht(name, &varname);
23571 if (ht == NULL || *varname == NUL)
23572 {
23573 EMSG2(_(e_illvar), name);
23574 return;
23575 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023576 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023577
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023578 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23579 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023580 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023581
Bram Moolenaar33570922005-01-25 22:26:29 +000023582 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023584 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023585 if (var_check_ro(v->di_flags, name, FALSE)
23586 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023587 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023588
23589 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023590 * Handle setting internal v: variables separately where needed to
23591 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023592 */
23593 if (ht == &vimvarht)
23594 {
23595 if (v->di_tv.v_type == VAR_STRING)
23596 {
23597 vim_free(v->di_tv.vval.v_string);
23598 if (copy || tv->v_type != VAR_STRING)
23599 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23600 else
23601 {
23602 /* Take over the string to avoid an extra alloc/free. */
23603 v->di_tv.vval.v_string = tv->vval.v_string;
23604 tv->vval.v_string = NULL;
23605 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023606 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023607 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023608 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023609 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023610 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023611 if (STRCMP(varname, "searchforward") == 0)
23612 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023613#ifdef FEAT_SEARCH_EXTRA
23614 else if (STRCMP(varname, "hlsearch") == 0)
23615 {
23616 no_hlsearch = !v->di_tv.vval.v_number;
23617 redraw_all_later(SOME_VALID);
23618 }
23619#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023620 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023621 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023622 else if (v->di_tv.v_type != tv->v_type)
23623 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023624 }
23625
23626 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 }
23628 else /* add a new variable */
23629 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023630 /* Can't add "v:" variable. */
23631 if (ht == &vimvarht)
23632 {
23633 EMSG2(_(e_illvar), name);
23634 return;
23635 }
23636
Bram Moolenaar92124a32005-06-17 22:03:40 +000023637 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023638 if (!valid_varname(varname))
23639 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023640
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023641 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23642 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023643 if (v == NULL)
23644 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023645 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023646 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023648 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 return;
23650 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023651 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023653
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023654 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023655 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023656 else
23657 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023658 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023659 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023660 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662}
23663
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023664/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023665 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023666 * Also give an error message.
23667 */
23668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023669var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023670{
23671 if (flags & DI_FLAGS_RO)
23672 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023673 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023674 return TRUE;
23675 }
23676 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23677 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023678 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023679 return TRUE;
23680 }
23681 return FALSE;
23682}
23683
23684/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023685 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23686 * Also give an error message.
23687 */
23688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023689var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023690{
23691 if (flags & DI_FLAGS_FIX)
23692 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023693 EMSG2(_("E795: Cannot delete variable %s"),
23694 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023695 return TRUE;
23696 }
23697 return FALSE;
23698}
23699
23700/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023701 * Check if a funcref is assigned to a valid variable name.
23702 * Return TRUE and give an error if not.
23703 */
23704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023705var_check_func_name(
23706 char_u *name, /* points to start of variable name */
23707 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023708{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023709 /* Allow for w: b: s: and t:. */
23710 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023711 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23712 ? name[2] : name[0]))
23713 {
23714 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23715 name);
23716 return TRUE;
23717 }
23718 /* Don't allow hiding a function. When "v" is not NULL we might be
23719 * assigning another function to the same var, the type is checked
23720 * below. */
23721 if (new_var && function_exists(name))
23722 {
23723 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23724 name);
23725 return TRUE;
23726 }
23727 return FALSE;
23728}
23729
23730/*
23731 * Check if a variable name is valid.
23732 * Return FALSE and give an error if not.
23733 */
23734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023735valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023736{
23737 char_u *p;
23738
23739 for (p = varname; *p != NUL; ++p)
23740 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23741 && *p != AUTOLOAD_CHAR)
23742 {
23743 EMSG2(_(e_illvar), varname);
23744 return FALSE;
23745 }
23746 return TRUE;
23747}
23748
23749/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023750 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023751 * Also give an error message, using "name" or _("name") when use_gettext is
23752 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023753 */
23754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023755tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023756{
23757 if (lock & VAR_LOCKED)
23758 {
23759 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023760 name == NULL ? (char_u *)_("Unknown")
23761 : use_gettext ? (char_u *)_(name)
23762 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023763 return TRUE;
23764 }
23765 if (lock & VAR_FIXED)
23766 {
23767 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023768 name == NULL ? (char_u *)_("Unknown")
23769 : use_gettext ? (char_u *)_(name)
23770 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023771 return TRUE;
23772 }
23773 return FALSE;
23774}
23775
23776/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023777 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023778 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023779 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023780 * It is OK for "from" and "to" to point to the same item. This is used to
23781 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023782 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023784copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023786 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023787 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023788 switch (from->v_type)
23789 {
23790 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023791 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023792 to->vval.v_number = from->vval.v_number;
23793 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023794 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023795#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023796 to->vval.v_float = from->vval.v_float;
23797 break;
23798#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023799 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023800#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023801 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023802 if (to->vval.v_job != NULL)
23803 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023804 break;
23805#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023806 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023807#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023808 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023809 if (to->vval.v_channel != NULL)
23810 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023811 break;
23812#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023813 case VAR_STRING:
23814 case VAR_FUNC:
23815 if (from->vval.v_string == NULL)
23816 to->vval.v_string = NULL;
23817 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023818 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023819 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023820 if (from->v_type == VAR_FUNC)
23821 func_ref(to->vval.v_string);
23822 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023823 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023824 case VAR_PARTIAL:
23825 if (from->vval.v_partial == NULL)
23826 to->vval.v_partial = NULL;
23827 else
23828 {
23829 to->vval.v_partial = from->vval.v_partial;
23830 ++to->vval.v_partial->pt_refcount;
23831 }
23832 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023833 case VAR_LIST:
23834 if (from->vval.v_list == NULL)
23835 to->vval.v_list = NULL;
23836 else
23837 {
23838 to->vval.v_list = from->vval.v_list;
23839 ++to->vval.v_list->lv_refcount;
23840 }
23841 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023842 case VAR_DICT:
23843 if (from->vval.v_dict == NULL)
23844 to->vval.v_dict = NULL;
23845 else
23846 {
23847 to->vval.v_dict = from->vval.v_dict;
23848 ++to->vval.v_dict->dv_refcount;
23849 }
23850 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023851 case VAR_UNKNOWN:
23852 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023853 break;
23854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855}
23856
23857/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023858 * Make a copy of an item.
23859 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023860 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23861 * reference to an already copied list/dict can be used.
23862 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023863 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023865item_copy(
23866 typval_T *from,
23867 typval_T *to,
23868 int deep,
23869 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023870{
23871 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023872 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023873
Bram Moolenaar33570922005-01-25 22:26:29 +000023874 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023875 {
23876 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023877 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023878 }
23879 ++recurse;
23880
23881 switch (from->v_type)
23882 {
23883 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023884 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023885 case VAR_STRING:
23886 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023887 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023888 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023889 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023890 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023891 copy_tv(from, to);
23892 break;
23893 case VAR_LIST:
23894 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023895 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023896 if (from->vval.v_list == NULL)
23897 to->vval.v_list = NULL;
23898 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23899 {
23900 /* use the copy made earlier */
23901 to->vval.v_list = from->vval.v_list->lv_copylist;
23902 ++to->vval.v_list->lv_refcount;
23903 }
23904 else
23905 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23906 if (to->vval.v_list == NULL)
23907 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023908 break;
23909 case VAR_DICT:
23910 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023911 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023912 if (from->vval.v_dict == NULL)
23913 to->vval.v_dict = NULL;
23914 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23915 {
23916 /* use the copy made earlier */
23917 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23918 ++to->vval.v_dict->dv_refcount;
23919 }
23920 else
23921 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23922 if (to->vval.v_dict == NULL)
23923 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023924 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023925 case VAR_UNKNOWN:
23926 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023927 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023928 }
23929 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023930 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023931}
23932
23933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934 * ":echo expr1 ..." print each argument separated with a space, add a
23935 * newline at the end.
23936 * ":echon expr1 ..." print each argument plain.
23937 */
23938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023939ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940{
23941 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023942 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023943 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 char_u *p;
23945 int needclr = TRUE;
23946 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023947 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948
23949 if (eap->skip)
23950 ++emsg_skip;
23951 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23952 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023953 /* If eval1() causes an error message the text from the command may
23954 * still need to be cleared. E.g., "echo 22,44". */
23955 need_clr_eos = needclr;
23956
Bram Moolenaar071d4272004-06-13 20:20:40 +000023957 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023958 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 {
23960 /*
23961 * Report the invalid expression unless the expression evaluation
23962 * has been cancelled due to an aborting error, an interrupt, or an
23963 * exception.
23964 */
23965 if (!aborting())
23966 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023967 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 break;
23969 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023970 need_clr_eos = FALSE;
23971
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 if (!eap->skip)
23973 {
23974 if (atstart)
23975 {
23976 atstart = FALSE;
23977 /* Call msg_start() after eval1(), evaluating the expression
23978 * may cause a message to appear. */
23979 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023980 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023981 /* Mark the saved text as finishing the line, so that what
23982 * follows is displayed on a new line when scrolling back
23983 * at the more prompt. */
23984 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 }
23988 else if (eap->cmdidx == CMD_echo)
23989 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023990 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023991 if (p != NULL)
23992 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023994 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023996 if (*p != TAB && needclr)
23997 {
23998 /* remove any text still there from the command */
23999 msg_clr_eos();
24000 needclr = FALSE;
24001 }
24002 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 }
24004 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024005 {
24006#ifdef FEAT_MBYTE
24007 if (has_mbyte)
24008 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000024009 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024010
24011 (void)msg_outtrans_len_attr(p, i, echo_attr);
24012 p += i - 1;
24013 }
24014 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024016 (void)msg_outtrans_len_attr(p, 1, echo_attr);
24017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024019 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024021 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 arg = skipwhite(arg);
24023 }
24024 eap->nextcmd = check_nextcmd(arg);
24025
24026 if (eap->skip)
24027 --emsg_skip;
24028 else
24029 {
24030 /* remove text that may still be there from the command */
24031 if (needclr)
24032 msg_clr_eos();
24033 if (eap->cmdidx == CMD_echo)
24034 msg_end();
24035 }
24036}
24037
24038/*
24039 * ":echohl {name}".
24040 */
24041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024042ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024043{
24044 int id;
24045
24046 id = syn_name2id(eap->arg);
24047 if (id == 0)
24048 echo_attr = 0;
24049 else
24050 echo_attr = syn_id2attr(id);
24051}
24052
24053/*
24054 * ":execute expr1 ..." execute the result of an expression.
24055 * ":echomsg expr1 ..." Print a message
24056 * ":echoerr expr1 ..." Print an error
24057 * Each gets spaces around each argument and a newline at the end for
24058 * echo commands
24059 */
24060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024061ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062{
24063 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024064 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024065 int ret = OK;
24066 char_u *p;
24067 garray_T ga;
24068 int len;
24069 int save_did_emsg;
24070
24071 ga_init2(&ga, 1, 80);
24072
24073 if (eap->skip)
24074 ++emsg_skip;
24075 while (*arg != NUL && *arg != '|' && *arg != '\n')
24076 {
24077 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024078 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 {
24080 /*
24081 * Report the invalid expression unless the expression evaluation
24082 * has been cancelled due to an aborting error, an interrupt, or an
24083 * exception.
24084 */
24085 if (!aborting())
24086 EMSG2(_(e_invexpr2), p);
24087 ret = FAIL;
24088 break;
24089 }
24090
24091 if (!eap->skip)
24092 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024093 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 len = (int)STRLEN(p);
24095 if (ga_grow(&ga, len + 2) == FAIL)
24096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024097 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098 ret = FAIL;
24099 break;
24100 }
24101 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024102 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104 ga.ga_len += len;
24105 }
24106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024107 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108 arg = skipwhite(arg);
24109 }
24110
24111 if (ret != FAIL && ga.ga_data != NULL)
24112 {
24113 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000024114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000024116 out_flush();
24117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 else if (eap->cmdidx == CMD_echoerr)
24119 {
24120 /* We don't want to abort following commands, restore did_emsg. */
24121 save_did_emsg = did_emsg;
24122 EMSG((char_u *)ga.ga_data);
24123 if (!force_abort)
24124 did_emsg = save_did_emsg;
24125 }
24126 else if (eap->cmdidx == CMD_execute)
24127 do_cmdline((char_u *)ga.ga_data,
24128 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
24129 }
24130
24131 ga_clear(&ga);
24132
24133 if (eap->skip)
24134 --emsg_skip;
24135
24136 eap->nextcmd = check_nextcmd(arg);
24137}
24138
24139/*
24140 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24141 * "arg" points to the "&" or '+' when called, to "option" when returning.
24142 * Returns NULL when no option name found. Otherwise pointer to the char
24143 * after the option name.
24144 */
24145 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024146find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147{
24148 char_u *p = *arg;
24149
24150 ++p;
24151 if (*p == 'g' && p[1] == ':')
24152 {
24153 *opt_flags = OPT_GLOBAL;
24154 p += 2;
24155 }
24156 else if (*p == 'l' && p[1] == ':')
24157 {
24158 *opt_flags = OPT_LOCAL;
24159 p += 2;
24160 }
24161 else
24162 *opt_flags = 0;
24163
24164 if (!ASCII_ISALPHA(*p))
24165 return NULL;
24166 *arg = p;
24167
24168 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24169 p += 4; /* termcap option */
24170 else
24171 while (ASCII_ISALPHA(*p))
24172 ++p;
24173 return p;
24174}
24175
24176/*
24177 * ":function"
24178 */
24179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024180ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181{
24182 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024183 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 int j;
24185 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024187 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 char_u *name = NULL;
24189 char_u *p;
24190 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024191 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192 garray_T newargs;
24193 garray_T newlines;
24194 int varargs = FALSE;
24195 int mustend = FALSE;
24196 int flags = 0;
24197 ufunc_T *fp;
24198 int indent;
24199 int nesting;
24200 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024201 dictitem_T *v;
24202 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024203 static int func_nr = 0; /* number for nameless function */
24204 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024205 hashtab_T *ht;
24206 int todo;
24207 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024208 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209
24210 /*
24211 * ":function" without argument: list functions.
24212 */
24213 if (ends_excmd(*eap->arg))
24214 {
24215 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024216 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024217 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024218 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024219 {
24220 if (!HASHITEM_EMPTY(hi))
24221 {
24222 --todo;
24223 fp = HI2UF(hi);
24224 if (!isdigit(*fp->uf_name))
24225 list_func_head(fp, FALSE);
24226 }
24227 }
24228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 eap->nextcmd = check_nextcmd(eap->arg);
24230 return;
24231 }
24232
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024233 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024234 * ":function /pat": list functions matching pattern.
24235 */
24236 if (*eap->arg == '/')
24237 {
24238 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24239 if (!eap->skip)
24240 {
24241 regmatch_T regmatch;
24242
24243 c = *p;
24244 *p = NUL;
24245 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24246 *p = c;
24247 if (regmatch.regprog != NULL)
24248 {
24249 regmatch.rm_ic = p_ic;
24250
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024251 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024252 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24253 {
24254 if (!HASHITEM_EMPTY(hi))
24255 {
24256 --todo;
24257 fp = HI2UF(hi);
24258 if (!isdigit(*fp->uf_name)
24259 && vim_regexec(&regmatch, fp->uf_name, 0))
24260 list_func_head(fp, FALSE);
24261 }
24262 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024263 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024264 }
24265 }
24266 if (*p == '/')
24267 ++p;
24268 eap->nextcmd = check_nextcmd(p);
24269 return;
24270 }
24271
24272 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024273 * Get the function name. There are these situations:
24274 * func normal function name
24275 * "name" == func, "fudi.fd_dict" == NULL
24276 * dict.func new dictionary entry
24277 * "name" == NULL, "fudi.fd_dict" set,
24278 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24279 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024280 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024281 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24282 * dict.func existing dict entry that's not a Funcref
24283 * "name" == NULL, "fudi.fd_dict" set,
24284 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024285 * s:func script-local function name
24286 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024289 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024290 paren = (vim_strchr(p, '(') != NULL);
24291 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292 {
24293 /*
24294 * Return on an invalid expression in braces, unless the expression
24295 * evaluation has been cancelled due to an aborting error, an
24296 * interrupt, or an exception.
24297 */
24298 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024300 if (!eap->skip && fudi.fd_newkey != NULL)
24301 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024302 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305 else
24306 eap->skip = TRUE;
24307 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024308
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309 /* An error in a function call during evaluation of an expression in magic
24310 * braces should not cause the function not to be defined. */
24311 saved_did_emsg = did_emsg;
24312 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313
24314 /*
24315 * ":function func" with only function name: list function.
24316 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024317 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 {
24319 if (!ends_excmd(*skipwhite(p)))
24320 {
24321 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024322 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323 }
24324 eap->nextcmd = check_nextcmd(p);
24325 if (eap->nextcmd != NULL)
24326 *p = NUL;
24327 if (!eap->skip && !got_int)
24328 {
24329 fp = find_func(name);
24330 if (fp != NULL)
24331 {
24332 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024333 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024335 if (FUNCLINE(fp, j) == NULL)
24336 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337 msg_putchar('\n');
24338 msg_outnum((long)(j + 1));
24339 if (j < 9)
24340 msg_putchar(' ');
24341 if (j < 99)
24342 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024343 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344 out_flush(); /* show a line at a time */
24345 ui_breakcheck();
24346 }
24347 if (!got_int)
24348 {
24349 msg_putchar('\n');
24350 msg_puts((char_u *)" endfunction");
24351 }
24352 }
24353 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024354 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024356 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 }
24358
24359 /*
24360 * ":function name(arg1, arg2)" Define function.
24361 */
24362 p = skipwhite(p);
24363 if (*p != '(')
24364 {
24365 if (!eap->skip)
24366 {
24367 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024368 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369 }
24370 /* attempt to continue by skipping some text */
24371 if (vim_strchr(p, '(') != NULL)
24372 p = vim_strchr(p, '(');
24373 }
24374 p = skipwhite(p + 1);
24375
24376 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24377 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24378
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024379 if (!eap->skip)
24380 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024381 /* Check the name of the function. Unless it's a dictionary function
24382 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024383 if (name != NULL)
24384 arg = name;
24385 else
24386 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024387 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024388 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24389 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024390 {
24391 if (*arg == K_SPECIAL)
24392 j = 3;
24393 else
24394 j = 0;
24395 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24396 : eval_isnamec(arg[j])))
24397 ++j;
24398 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024399 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024400 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024401 /* Disallow using the g: dict. */
24402 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24403 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024404 }
24405
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 /*
24407 * Isolate the arguments: "arg1, arg2, ...)"
24408 */
24409 while (*p != ')')
24410 {
24411 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24412 {
24413 varargs = TRUE;
24414 p += 3;
24415 mustend = TRUE;
24416 }
24417 else
24418 {
24419 arg = p;
24420 while (ASCII_ISALNUM(*p) || *p == '_')
24421 ++p;
24422 if (arg == p || isdigit(*arg)
24423 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24424 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24425 {
24426 if (!eap->skip)
24427 EMSG2(_("E125: Illegal argument: %s"), arg);
24428 break;
24429 }
24430 if (ga_grow(&newargs, 1) == FAIL)
24431 goto erret;
24432 c = *p;
24433 *p = NUL;
24434 arg = vim_strsave(arg);
24435 if (arg == NULL)
24436 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024437
24438 /* Check for duplicate argument name. */
24439 for (i = 0; i < newargs.ga_len; ++i)
24440 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24441 {
24442 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024443 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024444 goto erret;
24445 }
24446
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24448 *p = c;
24449 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024450 if (*p == ',')
24451 ++p;
24452 else
24453 mustend = TRUE;
24454 }
24455 p = skipwhite(p);
24456 if (mustend && *p != ')')
24457 {
24458 if (!eap->skip)
24459 EMSG2(_(e_invarg2), eap->arg);
24460 break;
24461 }
24462 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024463 if (*p != ')')
24464 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024465 ++p; /* skip the ')' */
24466
Bram Moolenaare9a41262005-01-15 22:18:47 +000024467 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468 for (;;)
24469 {
24470 p = skipwhite(p);
24471 if (STRNCMP(p, "range", 5) == 0)
24472 {
24473 flags |= FC_RANGE;
24474 p += 5;
24475 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024476 else if (STRNCMP(p, "dict", 4) == 0)
24477 {
24478 flags |= FC_DICT;
24479 p += 4;
24480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 else if (STRNCMP(p, "abort", 5) == 0)
24482 {
24483 flags |= FC_ABORT;
24484 p += 5;
24485 }
24486 else
24487 break;
24488 }
24489
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024490 /* When there is a line break use what follows for the function body.
24491 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24492 if (*p == '\n')
24493 line_arg = p + 1;
24494 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495 EMSG(_(e_trailing));
24496
24497 /*
24498 * Read the body of the function, until ":endfunction" is found.
24499 */
24500 if (KeyTyped)
24501 {
24502 /* Check if the function already exists, don't let the user type the
24503 * whole function before telling him it doesn't work! For a script we
24504 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024505 if (!eap->skip && !eap->forceit)
24506 {
24507 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24508 EMSG(_(e_funcdict));
24509 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024510 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024513 if (!eap->skip && did_emsg)
24514 goto erret;
24515
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516 msg_putchar('\n'); /* don't overwrite the function name */
24517 cmdline_row = msg_row;
24518 }
24519
24520 indent = 2;
24521 nesting = 0;
24522 for (;;)
24523 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024524 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024525 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024526 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024527 saved_wait_return = FALSE;
24528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024530 sourcing_lnum_off = sourcing_lnum;
24531
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024532 if (line_arg != NULL)
24533 {
24534 /* Use eap->arg, split up in parts by line breaks. */
24535 theline = line_arg;
24536 p = vim_strchr(theline, '\n');
24537 if (p == NULL)
24538 line_arg += STRLEN(line_arg);
24539 else
24540 {
24541 *p = NUL;
24542 line_arg = p + 1;
24543 }
24544 }
24545 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 theline = getcmdline(':', 0L, indent);
24547 else
24548 theline = eap->getline(':', eap->cookie, indent);
24549 if (KeyTyped)
24550 lines_left = Rows - 1;
24551 if (theline == NULL)
24552 {
24553 EMSG(_("E126: Missing :endfunction"));
24554 goto erret;
24555 }
24556
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024557 /* Detect line continuation: sourcing_lnum increased more than one. */
24558 if (sourcing_lnum > sourcing_lnum_off + 1)
24559 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24560 else
24561 sourcing_lnum_off = 0;
24562
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 if (skip_until != NULL)
24564 {
24565 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24566 * don't check for ":endfunc". */
24567 if (STRCMP(theline, skip_until) == 0)
24568 {
24569 vim_free(skip_until);
24570 skip_until = NULL;
24571 }
24572 }
24573 else
24574 {
24575 /* skip ':' and blanks*/
24576 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24577 ;
24578
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024579 /* Check for "endfunction". */
24580 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024581 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024582 if (line_arg == NULL)
24583 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 break;
24585 }
24586
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024587 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588 * at "end". */
24589 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24590 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024591 else if (STRNCMP(p, "if", 2) == 0
24592 || STRNCMP(p, "wh", 2) == 0
24593 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594 || STRNCMP(p, "try", 3) == 0)
24595 indent += 2;
24596
24597 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024598 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024600 if (*p == '!')
24601 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024602 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024603 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024604 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024606 ++nesting;
24607 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608 }
24609 }
24610
24611 /* Check for ":append" or ":insert". */
24612 p = skip_range(p, NULL);
24613 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24614 || (p[0] == 'i'
24615 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24616 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24617 skip_until = vim_strsave((char_u *)".");
24618
24619 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24620 arg = skipwhite(skiptowhite(p));
24621 if (arg[0] == '<' && arg[1] =='<'
24622 && ((p[0] == 'p' && p[1] == 'y'
24623 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24624 || (p[0] == 'p' && p[1] == 'e'
24625 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24626 || (p[0] == 't' && p[1] == 'c'
24627 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024628 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24629 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24631 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024632 || (p[0] == 'm' && p[1] == 'z'
24633 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 ))
24635 {
24636 /* ":python <<" continues until a dot, like ":append" */
24637 p = skipwhite(arg + 2);
24638 if (*p == NUL)
24639 skip_until = vim_strsave((char_u *)".");
24640 else
24641 skip_until = vim_strsave(p);
24642 }
24643 }
24644
24645 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024646 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024647 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024648 if (line_arg == NULL)
24649 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024651 }
24652
24653 /* Copy the line to newly allocated memory. get_one_sourceline()
24654 * allocates 250 bytes per line, this saves 80% on average. The cost
24655 * is an extra alloc/free. */
24656 p = vim_strsave(theline);
24657 if (p != NULL)
24658 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024659 if (line_arg == NULL)
24660 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024661 theline = p;
24662 }
24663
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024664 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24665
24666 /* Add NULL lines for continuation lines, so that the line count is
24667 * equal to the index in the growarray. */
24668 while (sourcing_lnum_off-- > 0)
24669 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024670
24671 /* Check for end of eap->arg. */
24672 if (line_arg != NULL && *line_arg == NUL)
24673 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674 }
24675
24676 /* Don't define the function when skipping commands or when an error was
24677 * detected. */
24678 if (eap->skip || did_emsg)
24679 goto erret;
24680
24681 /*
24682 * If there are no errors, add the function
24683 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024684 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024685 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024686 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024687 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024688 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024689 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024690 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024691 goto erret;
24692 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024693
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024694 fp = find_func(name);
24695 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024697 if (!eap->forceit)
24698 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024699 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024700 goto erret;
24701 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024702 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024703 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024704 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024705 name);
24706 goto erret;
24707 }
24708 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024709 ga_clear_strings(&(fp->uf_args));
24710 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024711 vim_free(name);
24712 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714 }
24715 else
24716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024717 char numbuf[20];
24718
24719 fp = NULL;
24720 if (fudi.fd_newkey == NULL && !eap->forceit)
24721 {
24722 EMSG(_(e_funcdict));
24723 goto erret;
24724 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024725 if (fudi.fd_di == NULL)
24726 {
24727 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024728 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024729 goto erret;
24730 }
24731 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024732 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024733 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024734
24735 /* Give the function a sequential number. Can only be used with a
24736 * Funcref! */
24737 vim_free(name);
24738 sprintf(numbuf, "%d", ++func_nr);
24739 name = vim_strsave((char_u *)numbuf);
24740 if (name == NULL)
24741 goto erret;
24742 }
24743
24744 if (fp == NULL)
24745 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024746 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024747 {
24748 int slen, plen;
24749 char_u *scriptname;
24750
24751 /* Check that the autoload name matches the script name. */
24752 j = FAIL;
24753 if (sourcing_name != NULL)
24754 {
24755 scriptname = autoload_name(name);
24756 if (scriptname != NULL)
24757 {
24758 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024759 plen = (int)STRLEN(p);
24760 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024761 if (slen > plen && fnamecmp(p,
24762 sourcing_name + slen - plen) == 0)
24763 j = OK;
24764 vim_free(scriptname);
24765 }
24766 }
24767 if (j == FAIL)
24768 {
24769 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24770 goto erret;
24771 }
24772 }
24773
24774 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775 if (fp == NULL)
24776 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024777
24778 if (fudi.fd_dict != NULL)
24779 {
24780 if (fudi.fd_di == NULL)
24781 {
24782 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024783 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024784 if (fudi.fd_di == NULL)
24785 {
24786 vim_free(fp);
24787 goto erret;
24788 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024789 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24790 {
24791 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024792 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024793 goto erret;
24794 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024795 }
24796 else
24797 /* overwrite existing dict entry */
24798 clear_tv(&fudi.fd_di->di_tv);
24799 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024800 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024801 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024802 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024803
24804 /* behave like "dict" was used */
24805 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024806 }
24807
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024809 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024810 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24811 {
24812 vim_free(fp);
24813 goto erret;
24814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024816 fp->uf_args = newargs;
24817 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024818#ifdef FEAT_PROFILE
24819 fp->uf_tml_count = NULL;
24820 fp->uf_tml_total = NULL;
24821 fp->uf_tml_self = NULL;
24822 fp->uf_profiling = FALSE;
24823 if (prof_def_func())
24824 func_do_profile(fp);
24825#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024826 fp->uf_varargs = varargs;
24827 fp->uf_flags = flags;
24828 fp->uf_calls = 0;
24829 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024830 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831
24832erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024833 ga_clear_strings(&newargs);
24834 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024835ret_free:
24836 vim_free(skip_until);
24837 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024840 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841}
24842
24843/*
24844 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024845 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024847 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024848 * TFN_INT: internal function name OK
24849 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024850 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 * Advances "pp" to just after the function name (if no error).
24852 */
24853 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024854trans_function_name(
24855 char_u **pp,
24856 int skip, /* only find the end, don't evaluate */
24857 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024858 funcdict_T *fdp, /* return: info about dictionary used */
24859 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024861 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862 char_u *start;
24863 char_u *end;
24864 int lead;
24865 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024867 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024868
24869 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024870 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024872
24873 /* Check for hard coded <SNR>: already translated function ID (from a user
24874 * command). */
24875 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24876 && (*pp)[2] == (int)KE_SNR)
24877 {
24878 *pp += 3;
24879 len = get_id_len(pp) + 3;
24880 return vim_strnsave(start, len);
24881 }
24882
24883 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24884 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024886 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024888
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024889 /* Note that TFN_ flags use the same values as GLV_ flags. */
24890 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024891 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024892 if (end == start)
24893 {
24894 if (!skip)
24895 EMSG(_("E129: Function name required"));
24896 goto theend;
24897 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024898 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024899 {
24900 /*
24901 * Report an invalid expression in braces, unless the expression
24902 * evaluation has been cancelled due to an aborting error, an
24903 * interrupt, or an exception.
24904 */
24905 if (!aborting())
24906 {
24907 if (end != NULL)
24908 EMSG2(_(e_invarg2), start);
24909 }
24910 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024911 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024912 goto theend;
24913 }
24914
24915 if (lv.ll_tv != NULL)
24916 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024917 if (fdp != NULL)
24918 {
24919 fdp->fd_dict = lv.ll_dict;
24920 fdp->fd_newkey = lv.ll_newkey;
24921 lv.ll_newkey = NULL;
24922 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024924 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24925 {
24926 name = vim_strsave(lv.ll_tv->vval.v_string);
24927 *pp = end;
24928 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024929 else if (lv.ll_tv->v_type == VAR_PARTIAL
24930 && lv.ll_tv->vval.v_partial != NULL)
24931 {
24932 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24933 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024934 if (partial != NULL)
24935 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024936 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024937 else
24938 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024939 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24940 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024941 EMSG(_(e_funcref));
24942 else
24943 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024944 name = NULL;
24945 }
24946 goto theend;
24947 }
24948
24949 if (lv.ll_name == NULL)
24950 {
24951 /* Error found, but continue after the function name. */
24952 *pp = end;
24953 goto theend;
24954 }
24955
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024956 /* Check if the name is a Funcref. If so, use the value. */
24957 if (lv.ll_exp_name != NULL)
24958 {
24959 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024960 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024961 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024962 if (name == lv.ll_exp_name)
24963 name = NULL;
24964 }
24965 else
24966 {
24967 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024968 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024969 if (name == *pp)
24970 name = NULL;
24971 }
24972 if (name != NULL)
24973 {
24974 name = vim_strsave(name);
24975 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024976 if (STRNCMP(name, "<SNR>", 5) == 0)
24977 {
24978 /* Change "<SNR>" to the byte sequence. */
24979 name[0] = K_SPECIAL;
24980 name[1] = KS_EXTRA;
24981 name[2] = (int)KE_SNR;
24982 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24983 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024984 goto theend;
24985 }
24986
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024987 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024988 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024989 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024990 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24991 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24992 {
24993 /* When there was "s:" already or the name expanded to get a
24994 * leading "s:" then remove it. */
24995 lv.ll_name += 2;
24996 len -= 2;
24997 lead = 2;
24998 }
24999 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025000 else
Bram Moolenaara7043832005-01-21 11:56:39 +000025001 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025002 /* skip over "s:" and "g:" */
25003 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000025004 lv.ll_name += 2;
25005 len = (int)(end - lv.ll_name);
25006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025007
25008 /*
25009 * Copy the function name to allocated memory.
25010 * Accept <SID>name() inside a script, translate into <SNR>123_name().
25011 * Accept <SNR>123_name() outside a script.
25012 */
25013 if (skip)
25014 lead = 0; /* do nothing */
25015 else if (lead > 0)
25016 {
25017 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000025018 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
25019 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025020 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000025021 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025022 if (current_SID <= 0)
25023 {
25024 EMSG(_(e_usingsid));
25025 goto theend;
25026 }
25027 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
25028 lead += (int)STRLEN(sid_buf);
25029 }
25030 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025031 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025032 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025033 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025034 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025035 goto theend;
25036 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025037 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025038 {
25039 char_u *cp = vim_strchr(lv.ll_name, ':');
25040
25041 if (cp != NULL && cp < end)
25042 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025043 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025044 goto theend;
25045 }
25046 }
25047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025048 name = alloc((unsigned)(len + lead + 1));
25049 if (name != NULL)
25050 {
25051 if (lead > 0)
25052 {
25053 name[0] = K_SPECIAL;
25054 name[1] = KS_EXTRA;
25055 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000025056 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025057 STRCPY(name + 3, sid_buf);
25058 }
25059 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025060 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025061 }
25062 *pp = end;
25063
25064theend:
25065 clear_lval(&lv);
25066 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067}
25068
25069/*
25070 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
25071 * Return 2 if "p" starts with "s:".
25072 * Return 0 otherwise.
25073 */
25074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025075eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010025077 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
25078 * the standard library function. */
25079 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
25080 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025081 return 5;
25082 if (p[0] == 's' && p[1] == ':')
25083 return 2;
25084 return 0;
25085}
25086
25087/*
25088 * Return TRUE if "p" starts with "<SID>" or "s:".
25089 * Only works if eval_fname_script() returned non-zero for "p"!
25090 */
25091 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025092eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093{
25094 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
25095}
25096
25097/*
25098 * List the head of the function: "name(arg1, arg2)".
25099 */
25100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025101list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025102{
25103 int j;
25104
25105 msg_start();
25106 if (indent)
25107 MSG_PUTS(" ");
25108 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025109 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 {
25111 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025112 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113 }
25114 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025115 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025117 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025118 {
25119 if (j)
25120 MSG_PUTS(", ");
25121 msg_puts(FUNCARG(fp, j));
25122 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025123 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124 {
25125 if (j)
25126 MSG_PUTS(", ");
25127 MSG_PUTS("...");
25128 }
25129 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020025130 if (fp->uf_flags & FC_ABORT)
25131 MSG_PUTS(" abort");
25132 if (fp->uf_flags & FC_RANGE)
25133 MSG_PUTS(" range");
25134 if (fp->uf_flags & FC_DICT)
25135 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025136 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000025137 if (p_verbose > 0)
25138 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139}
25140
25141/*
25142 * Find a function by name, return pointer to it in ufuncs.
25143 * Return NULL for unknown function.
25144 */
25145 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025146find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025147{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025148 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025150 hi = hash_find(&func_hashtab, name);
25151 if (!HASHITEM_EMPTY(hi))
25152 return HI2UF(hi);
25153 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025154}
25155
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025156#if defined(EXITFREE) || defined(PROTO)
25157 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025158free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025159{
25160 hashitem_T *hi;
25161
25162 /* Need to start all over every time, because func_free() may change the
25163 * hash table. */
25164 while (func_hashtab.ht_used > 0)
25165 for (hi = func_hashtab.ht_array; ; ++hi)
25166 if (!HASHITEM_EMPTY(hi))
25167 {
25168 func_free(HI2UF(hi));
25169 break;
25170 }
25171}
25172#endif
25173
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025174 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025175translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025176{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025177 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025178 return find_internal_func(name) >= 0;
25179 return find_func(name) != NULL;
25180}
25181
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025182/*
25183 * Return TRUE if a function "name" exists.
25184 */
25185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025186function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025187{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025188 char_u *nm = name;
25189 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025190 int n = FALSE;
25191
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025192 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025193 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025194 nm = skipwhite(nm);
25195
25196 /* Only accept "funcname", "funcname ", "funcname (..." and
25197 * "funcname(...", not "funcname!...". */
25198 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025199 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025200 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025201 return n;
25202}
25203
Bram Moolenaara1544c02013-05-30 12:35:52 +020025204 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025205get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025206{
25207 char_u *nm = name;
25208 char_u *p;
25209
Bram Moolenaar65639032016-03-16 21:40:30 +010025210 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025211
25212 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025213 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025214 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025215
Bram Moolenaara1544c02013-05-30 12:35:52 +020025216 vim_free(p);
25217 return NULL;
25218}
25219
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025220/*
25221 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025222 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25223 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025224 */
25225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025226builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025227{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025228 char_u *p;
25229
25230 if (!ASCII_ISLOWER(name[0]))
25231 return FALSE;
25232 p = vim_strchr(name, AUTOLOAD_CHAR);
25233 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025234}
25235
Bram Moolenaar05159a02005-02-26 23:04:13 +000025236#if defined(FEAT_PROFILE) || defined(PROTO)
25237/*
25238 * Start profiling function "fp".
25239 */
25240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025241func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025242{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025243 int len = fp->uf_lines.ga_len;
25244
25245 if (len == 0)
25246 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025247 fp->uf_tm_count = 0;
25248 profile_zero(&fp->uf_tm_self);
25249 profile_zero(&fp->uf_tm_total);
25250 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025251 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025252 if (fp->uf_tml_total == NULL)
25253 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025254 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025255 if (fp->uf_tml_self == NULL)
25256 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025257 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025258 fp->uf_tml_idx = -1;
25259 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25260 || fp->uf_tml_self == NULL)
25261 return; /* out of memory */
25262
25263 fp->uf_profiling = TRUE;
25264}
25265
25266/*
25267 * Dump the profiling results for all functions in file "fd".
25268 */
25269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025270func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025271{
25272 hashitem_T *hi;
25273 int todo;
25274 ufunc_T *fp;
25275 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025276 ufunc_T **sorttab;
25277 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025279 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025280 if (todo == 0)
25281 return; /* nothing to dump */
25282
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025283 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025284
Bram Moolenaar05159a02005-02-26 23:04:13 +000025285 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25286 {
25287 if (!HASHITEM_EMPTY(hi))
25288 {
25289 --todo;
25290 fp = HI2UF(hi);
25291 if (fp->uf_profiling)
25292 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025293 if (sorttab != NULL)
25294 sorttab[st_len++] = fp;
25295
Bram Moolenaar05159a02005-02-26 23:04:13 +000025296 if (fp->uf_name[0] == K_SPECIAL)
25297 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25298 else
25299 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25300 if (fp->uf_tm_count == 1)
25301 fprintf(fd, "Called 1 time\n");
25302 else
25303 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25304 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25305 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25306 fprintf(fd, "\n");
25307 fprintf(fd, "count total (s) self (s)\n");
25308
25309 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25310 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025311 if (FUNCLINE(fp, i) == NULL)
25312 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025313 prof_func_line(fd, fp->uf_tml_count[i],
25314 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025315 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25316 }
25317 fprintf(fd, "\n");
25318 }
25319 }
25320 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025321
25322 if (sorttab != NULL && st_len > 0)
25323 {
25324 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25325 prof_total_cmp);
25326 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25327 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25328 prof_self_cmp);
25329 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25330 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025331
25332 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025333}
Bram Moolenaar73830342005-02-28 22:48:19 +000025334
25335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025336prof_sort_list(
25337 FILE *fd,
25338 ufunc_T **sorttab,
25339 int st_len,
25340 char *title,
25341 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025342{
25343 int i;
25344 ufunc_T *fp;
25345
25346 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25347 fprintf(fd, "count total (s) self (s) function\n");
25348 for (i = 0; i < 20 && i < st_len; ++i)
25349 {
25350 fp = sorttab[i];
25351 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25352 prefer_self);
25353 if (fp->uf_name[0] == K_SPECIAL)
25354 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25355 else
25356 fprintf(fd, " %s()\n", fp->uf_name);
25357 }
25358 fprintf(fd, "\n");
25359}
25360
25361/*
25362 * Print the count and times for one function or function line.
25363 */
25364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025365prof_func_line(
25366 FILE *fd,
25367 int count,
25368 proftime_T *total,
25369 proftime_T *self,
25370 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025371{
25372 if (count > 0)
25373 {
25374 fprintf(fd, "%5d ", count);
25375 if (prefer_self && profile_equal(total, self))
25376 fprintf(fd, " ");
25377 else
25378 fprintf(fd, "%s ", profile_msg(total));
25379 if (!prefer_self && profile_equal(total, self))
25380 fprintf(fd, " ");
25381 else
25382 fprintf(fd, "%s ", profile_msg(self));
25383 }
25384 else
25385 fprintf(fd, " ");
25386}
25387
25388/*
25389 * Compare function for total time sorting.
25390 */
25391 static int
25392#ifdef __BORLANDC__
25393_RTLENTRYF
25394#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025395prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025396{
25397 ufunc_T *p1, *p2;
25398
25399 p1 = *(ufunc_T **)s1;
25400 p2 = *(ufunc_T **)s2;
25401 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25402}
25403
25404/*
25405 * Compare function for self time sorting.
25406 */
25407 static int
25408#ifdef __BORLANDC__
25409_RTLENTRYF
25410#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025411prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025412{
25413 ufunc_T *p1, *p2;
25414
25415 p1 = *(ufunc_T **)s1;
25416 p2 = *(ufunc_T **)s2;
25417 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25418}
25419
Bram Moolenaar05159a02005-02-26 23:04:13 +000025420#endif
25421
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025422/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025423 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025424 * Return TRUE if a package was loaded.
25425 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025426 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025427script_autoload(
25428 char_u *name,
25429 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025430{
25431 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025432 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025433 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025434 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025435
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025436 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025437 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025438 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025439 return FALSE;
25440
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025441 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025442
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025443 /* Find the name in the list of previously loaded package names. Skip
25444 * "autoload/", it's always the same. */
25445 for (i = 0; i < ga_loaded.ga_len; ++i)
25446 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25447 break;
25448 if (!reload && i < ga_loaded.ga_len)
25449 ret = FALSE; /* was loaded already */
25450 else
25451 {
25452 /* Remember the name if it wasn't loaded already. */
25453 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25454 {
25455 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25456 tofree = NULL;
25457 }
25458
25459 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025460 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025461 ret = TRUE;
25462 }
25463
25464 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025465 return ret;
25466}
25467
25468/*
25469 * Return the autoload script name for a function or variable name.
25470 * Returns NULL when out of memory.
25471 */
25472 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025473autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025474{
25475 char_u *p;
25476 char_u *scriptname;
25477
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025478 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025479 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25480 if (scriptname == NULL)
25481 return FALSE;
25482 STRCPY(scriptname, "autoload/");
25483 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025484 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025485 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025486 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025487 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025488 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025489}
25490
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25492
25493/*
25494 * Function given to ExpandGeneric() to obtain the list of user defined
25495 * function names.
25496 */
25497 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025498get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025500 static long_u done;
25501 static hashitem_T *hi;
25502 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503
25504 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025506 done = 0;
25507 hi = func_hashtab.ht_array;
25508 }
25509 if (done < func_hashtab.ht_used)
25510 {
25511 if (done++ > 0)
25512 ++hi;
25513 while (HASHITEM_EMPTY(hi))
25514 ++hi;
25515 fp = HI2UF(hi);
25516
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025517 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025518 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025519
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025520 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25521 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522
25523 cat_func_name(IObuff, fp);
25524 if (xp->xp_context != EXPAND_USER_FUNC)
25525 {
25526 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025527 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025528 STRCAT(IObuff, ")");
25529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 return IObuff;
25531 }
25532 return NULL;
25533}
25534
25535#endif /* FEAT_CMDL_COMPL */
25536
25537/*
25538 * Copy the function name of "fp" to buffer "buf".
25539 * "buf" must be able to hold the function name plus three bytes.
25540 * Takes care of script-local function names.
25541 */
25542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025543cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025545 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546 {
25547 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025548 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549 }
25550 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025551 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552}
25553
25554/*
25555 * ":delfunction {name}"
25556 */
25557 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025558ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025560 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561 char_u *p;
25562 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025563 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025564
25565 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025566 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025567 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025569 {
25570 if (fudi.fd_dict != NULL && !eap->skip)
25571 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025572 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 if (!ends_excmd(*skipwhite(p)))
25575 {
25576 vim_free(name);
25577 EMSG(_(e_trailing));
25578 return;
25579 }
25580 eap->nextcmd = check_nextcmd(p);
25581 if (eap->nextcmd != NULL)
25582 *p = NUL;
25583
25584 if (!eap->skip)
25585 fp = find_func(name);
25586 vim_free(name);
25587
25588 if (!eap->skip)
25589 {
25590 if (fp == NULL)
25591 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025592 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593 return;
25594 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025595 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596 {
25597 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25598 return;
25599 }
25600
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025601 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025603 /* Delete the dict item that refers to the function, it will
25604 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025605 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025607 else
25608 func_free(fp);
25609 }
25610}
25611
25612/*
25613 * Free a function and remove it from the list of functions.
25614 */
25615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025616func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025617{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025618 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025619
25620 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025621 ga_clear_strings(&(fp->uf_args));
25622 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025623#ifdef FEAT_PROFILE
25624 vim_free(fp->uf_tml_count);
25625 vim_free(fp->uf_tml_total);
25626 vim_free(fp->uf_tml_self);
25627#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025628
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025629 /* remove the function from the function hashtable */
25630 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25631 if (HASHITEM_EMPTY(hi))
25632 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025633 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025634 hash_remove(&func_hashtab, hi);
25635
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025636 vim_free(fp);
25637}
25638
25639/*
25640 * Unreference a Function: decrement the reference count and free it when it
25641 * becomes zero. Only for numbered functions.
25642 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025644func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025645{
25646 ufunc_T *fp;
25647
25648 if (name != NULL && isdigit(*name))
25649 {
25650 fp = find_func(name);
25651 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025652 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025653#ifdef EXITFREE
25654 if (!entered_free_all_mem)
25655#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025656 EMSG2(_(e_intern2), "func_unref()");
25657 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025658 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025659 {
25660 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025661 * when "uf_calls" becomes zero. */
25662 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025663 func_free(fp);
25664 }
25665 }
25666}
25667
25668/*
25669 * Count a reference to a Function.
25670 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025672func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025673{
25674 ufunc_T *fp;
25675
25676 if (name != NULL && isdigit(*name))
25677 {
25678 fp = find_func(name);
25679 if (fp == NULL)
25680 EMSG2(_(e_intern2), "func_ref()");
25681 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025682 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025683 }
25684}
25685
25686/*
25687 * Call a user function.
25688 */
25689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025690call_user_func(
25691 ufunc_T *fp, /* pointer to function */
25692 int argcount, /* nr of args */
25693 typval_T *argvars, /* arguments */
25694 typval_T *rettv, /* return value */
25695 linenr_T firstline, /* first line of range */
25696 linenr_T lastline, /* last line of range */
25697 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025698{
Bram Moolenaar33570922005-01-25 22:26:29 +000025699 char_u *save_sourcing_name;
25700 linenr_T save_sourcing_lnum;
25701 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025702 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025703 int save_did_emsg;
25704 static int depth = 0;
25705 dictitem_T *v;
25706 int fixvar_idx = 0; /* index in fixvar[] */
25707 int i;
25708 int ai;
25709 char_u numbuf[NUMBUFLEN];
25710 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025711 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025712#ifdef FEAT_PROFILE
25713 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025714 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716
25717 /* If depth of calling is getting too high, don't execute the function */
25718 if (depth >= p_mfd)
25719 {
25720 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025721 rettv->v_type = VAR_NUMBER;
25722 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025723 return;
25724 }
25725 ++depth;
25726
25727 line_breakcheck(); /* check for CTRL-C hit */
25728
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025729 fc = (funccall_T *)alloc(sizeof(funccall_T));
25730 fc->caller = current_funccal;
25731 current_funccal = fc;
25732 fc->func = fp;
25733 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025734 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025735 fc->linenr = 0;
25736 fc->returned = FALSE;
25737 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025739 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25740 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741
Bram Moolenaar33570922005-01-25 22:26:29 +000025742 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025743 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025744 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25745 * each argument variable and saves a lot of time.
25746 */
25747 /*
25748 * Init l: variables.
25749 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025750 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025751 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025752 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025753 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25754 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025755 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025756 name = v->di_key;
25757 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025758 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025759 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025760 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025761 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025762 v->di_tv.vval.v_dict = selfdict;
25763 ++selfdict->dv_refcount;
25764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025765
Bram Moolenaar33570922005-01-25 22:26:29 +000025766 /*
25767 * Init a: variables.
25768 * Set a:0 to "argcount".
25769 * Set a:000 to a list with room for the "..." arguments.
25770 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025771 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025772 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025773 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025774 /* Use "name" to avoid a warning from some compiler that checks the
25775 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025776 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025777 name = v->di_key;
25778 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025779 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025780 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025781 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025782 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025783 v->di_tv.vval.v_list = &fc->l_varlist;
25784 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25785 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25786 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025787
25788 /*
25789 * Set a:firstline to "firstline" and a:lastline to "lastline".
25790 * Set a:name to named arguments.
25791 * Set a:N to the "..." arguments.
25792 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025793 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025794 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025795 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025796 (varnumber_T)lastline);
25797 for (i = 0; i < argcount; ++i)
25798 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025799 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025800 if (ai < 0)
25801 /* named argument a:name */
25802 name = FUNCARG(fp, i);
25803 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025804 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025805 /* "..." argument a:1, a:2, etc. */
25806 sprintf((char *)numbuf, "%d", ai + 1);
25807 name = numbuf;
25808 }
25809 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25810 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025811 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025812 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25813 }
25814 else
25815 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025816 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25817 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025818 if (v == NULL)
25819 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025820 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025821 }
25822 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025823 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025824
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025825 /* Note: the values are copied directly to avoid alloc/free.
25826 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025827 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025828 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025829
25830 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25831 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025832 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25833 fc->l_listitems[ai].li_tv = argvars[i];
25834 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025835 }
25836 }
25837
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 /* Don't redraw while executing the function. */
25839 ++RedrawingDisabled;
25840 save_sourcing_name = sourcing_name;
25841 save_sourcing_lnum = sourcing_lnum;
25842 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025843 /* need space for function name + ("function " + 3) or "[number]" */
25844 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25845 + STRLEN(fp->uf_name) + 20;
25846 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025847 if (sourcing_name != NULL)
25848 {
25849 if (save_sourcing_name != NULL
25850 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025851 sprintf((char *)sourcing_name, "%s[%d]..",
25852 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025853 else
25854 STRCPY(sourcing_name, "function ");
25855 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25856
25857 if (p_verbose >= 12)
25858 {
25859 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025860 verbose_enter_scroll();
25861
Bram Moolenaar555b2802005-05-19 21:08:39 +000025862 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025863 if (p_verbose >= 14)
25864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025865 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025866 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025867 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025868 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025869
25870 msg_puts((char_u *)"(");
25871 for (i = 0; i < argcount; ++i)
25872 {
25873 if (i > 0)
25874 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025875 if (argvars[i].v_type == VAR_NUMBER)
25876 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025877 else
25878 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025879 /* Do not want errors such as E724 here. */
25880 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025881 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025882 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025883 if (s != NULL)
25884 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025885 if (vim_strsize(s) > MSG_BUF_CLEN)
25886 {
25887 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25888 s = buf;
25889 }
25890 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025891 vim_free(tofree);
25892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893 }
25894 }
25895 msg_puts((char_u *)")");
25896 }
25897 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025898
25899 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900 --no_wait_return;
25901 }
25902 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025903#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025904 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025905 {
25906 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25907 func_do_profile(fp);
25908 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025909 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025910 {
25911 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025912 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025913 profile_zero(&fp->uf_tm_children);
25914 }
25915 script_prof_save(&wait_start);
25916 }
25917#endif
25918
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025920 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921 save_did_emsg = did_emsg;
25922 did_emsg = FALSE;
25923
25924 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025925 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25927
25928 --RedrawingDisabled;
25929
25930 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025931 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025933 clear_tv(rettv);
25934 rettv->v_type = VAR_NUMBER;
25935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936 }
25937
Bram Moolenaar05159a02005-02-26 23:04:13 +000025938#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025939 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025940 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025941 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025942 profile_end(&call_start);
25943 profile_sub_wait(&wait_start, &call_start);
25944 profile_add(&fp->uf_tm_total, &call_start);
25945 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025946 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025947 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025948 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25949 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025950 }
25951 }
25952#endif
25953
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954 /* when being verbose, mention the return value */
25955 if (p_verbose >= 12)
25956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025957 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025958 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025961 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025962 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025963 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025964 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025965 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025967 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025968 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025969 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025970 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025971
Bram Moolenaar555b2802005-05-19 21:08:39 +000025972 /* The value may be very long. Skip the middle part, so that we
25973 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025974 * truncate it at the end. Don't want errors such as E724 here. */
25975 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025976 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025977 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025978 if (s != NULL)
25979 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025980 if (vim_strsize(s) > MSG_BUF_CLEN)
25981 {
25982 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25983 s = buf;
25984 }
25985 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025986 vim_free(tofree);
25987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025988 }
25989 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025990
25991 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025992 --no_wait_return;
25993 }
25994
25995 vim_free(sourcing_name);
25996 sourcing_name = save_sourcing_name;
25997 sourcing_lnum = save_sourcing_lnum;
25998 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025999#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026000 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026001 script_prof_restore(&wait_start);
26002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026003
26004 if (p_verbose >= 12 && sourcing_name != NULL)
26005 {
26006 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026007 verbose_enter_scroll();
26008
Bram Moolenaar555b2802005-05-19 21:08:39 +000026009 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026010 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026011
26012 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013 --no_wait_return;
26014 }
26015
26016 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026017 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026019
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026020 /* If the a:000 list and the l: and a: dicts are not referenced we can
26021 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026022 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
26023 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
26024 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
26025 {
26026 free_funccal(fc, FALSE);
26027 }
26028 else
26029 {
26030 hashitem_T *hi;
26031 listitem_T *li;
26032 int todo;
26033
26034 /* "fc" is still in use. This can happen when returning "a:000" or
26035 * assigning "l:" to a global variable.
26036 * Link "fc" in the list for garbage collection later. */
26037 fc->caller = previous_funccal;
26038 previous_funccal = fc;
26039
26040 /* Make a copy of the a: variables, since we didn't do that above. */
26041 todo = (int)fc->l_avars.dv_hashtab.ht_used;
26042 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
26043 {
26044 if (!HASHITEM_EMPTY(hi))
26045 {
26046 --todo;
26047 v = HI2DI(hi);
26048 copy_tv(&v->di_tv, &v->di_tv);
26049 }
26050 }
26051
26052 /* Make a copy of the a:000 items, since we didn't do that above. */
26053 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26054 copy_tv(&li->li_tv, &li->li_tv);
26055 }
26056}
26057
26058/*
26059 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026060 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026061 */
26062 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026063can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026064{
26065 return (fc->l_varlist.lv_copyID != copyID
26066 && fc->l_vars.dv_copyID != copyID
26067 && fc->l_avars.dv_copyID != copyID);
26068}
26069
26070/*
26071 * Free "fc" and what it contains.
26072 */
26073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026074free_funccal(
26075 funccall_T *fc,
26076 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026077{
26078 listitem_T *li;
26079
26080 /* The a: variables typevals may not have been allocated, only free the
26081 * allocated variables. */
26082 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
26083
26084 /* free all l: variables */
26085 vars_clear(&fc->l_vars.dv_hashtab);
26086
26087 /* Free the a:000 variables if they were allocated. */
26088 if (free_val)
26089 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26090 clear_tv(&li->li_tv);
26091
26092 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026093}
26094
26095/*
Bram Moolenaar33570922005-01-25 22:26:29 +000026096 * Add a number variable "name" to dict "dp" with value "nr".
26097 */
26098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026099add_nr_var(
26100 dict_T *dp,
26101 dictitem_T *v,
26102 char *name,
26103 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000026104{
26105 STRCPY(v->di_key, name);
26106 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
26107 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
26108 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000026109 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000026110 v->di_tv.vval.v_number = nr;
26111}
26112
26113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000026114 * ":return [expr]"
26115 */
26116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026117ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118{
26119 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000026120 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121 int returning = FALSE;
26122
26123 if (current_funccal == NULL)
26124 {
26125 EMSG(_("E133: :return not inside a function"));
26126 return;
26127 }
26128
26129 if (eap->skip)
26130 ++emsg_skip;
26131
26132 eap->nextcmd = NULL;
26133 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026134 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135 {
26136 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026137 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026139 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026140 }
26141 /* It's safer to return also on error. */
26142 else if (!eap->skip)
26143 {
26144 /*
26145 * Return unless the expression evaluation has been cancelled due to an
26146 * aborting error, an interrupt, or an exception.
26147 */
26148 if (!aborting())
26149 returning = do_return(eap, FALSE, TRUE, NULL);
26150 }
26151
26152 /* When skipping or the return gets pending, advance to the next command
26153 * in this line (!returning). Otherwise, ignore the rest of the line.
26154 * Following lines will be ignored by get_func_line(). */
26155 if (returning)
26156 eap->nextcmd = NULL;
26157 else if (eap->nextcmd == NULL) /* no argument */
26158 eap->nextcmd = check_nextcmd(arg);
26159
26160 if (eap->skip)
26161 --emsg_skip;
26162}
26163
26164/*
26165 * Return from a function. Possibly makes the return pending. Also called
26166 * for a pending return at the ":endtry" or after returning from an extra
26167 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026168 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026169 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026170 * FALSE when the return gets pending.
26171 */
26172 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026173do_return(
26174 exarg_T *eap,
26175 int reanimate,
26176 int is_cmd,
26177 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026178{
26179 int idx;
26180 struct condstack *cstack = eap->cstack;
26181
26182 if (reanimate)
26183 /* Undo the return. */
26184 current_funccal->returned = FALSE;
26185
26186 /*
26187 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26188 * not in its finally clause (which then is to be executed next) is found.
26189 * In this case, make the ":return" pending for execution at the ":endtry".
26190 * Otherwise, return normally.
26191 */
26192 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26193 if (idx >= 0)
26194 {
26195 cstack->cs_pending[idx] = CSTP_RETURN;
26196
26197 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026198 /* A pending return again gets pending. "rettv" points to an
26199 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026201 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026202 else
26203 {
26204 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026205 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026207 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026209 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026210 {
26211 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026212 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026213 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026214 else
26215 EMSG(_(e_outofmem));
26216 }
26217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026218 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026219
26220 if (reanimate)
26221 {
26222 /* The pending return value could be overwritten by a ":return"
26223 * without argument in a finally clause; reset the default
26224 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026225 current_funccal->rettv->v_type = VAR_NUMBER;
26226 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227 }
26228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026229 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230 }
26231 else
26232 {
26233 current_funccal->returned = TRUE;
26234
26235 /* If the return is carried out now, store the return value. For
26236 * a return immediately after reanimation, the value is already
26237 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026238 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026240 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026241 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026242 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026243 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026244 }
26245 }
26246
26247 return idx < 0;
26248}
26249
26250/*
26251 * Free the variable with a pending return value.
26252 */
26253 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026254discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255{
Bram Moolenaar33570922005-01-25 22:26:29 +000026256 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026257}
26258
26259/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026260 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026261 * is an allocated string. Used by report_pending() for verbose messages.
26262 */
26263 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026264get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026265{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026266 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026267 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026268 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026270 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026271 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026272 if (s == NULL)
26273 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026274
26275 STRCPY(IObuff, ":return ");
26276 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26277 if (STRLEN(s) + 8 >= IOSIZE)
26278 STRCPY(IObuff + IOSIZE - 4, "...");
26279 vim_free(tofree);
26280 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026281}
26282
26283/*
26284 * Get next function line.
26285 * Called by do_cmdline() to get the next line.
26286 * Returns allocated string, or NULL for end of function.
26287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026288 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026289get_func_line(
26290 int c UNUSED,
26291 void *cookie,
26292 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293{
Bram Moolenaar33570922005-01-25 22:26:29 +000026294 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026295 ufunc_T *fp = fcp->func;
26296 char_u *retval;
26297 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026298
26299 /* If breakpoints have been added/deleted need to check for it. */
26300 if (fcp->dbg_tick != debug_tick)
26301 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026302 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026303 sourcing_lnum);
26304 fcp->dbg_tick = debug_tick;
26305 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026306#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026307 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026308 func_line_end(cookie);
26309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310
Bram Moolenaar05159a02005-02-26 23:04:13 +000026311 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026312 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26313 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026314 retval = NULL;
26315 else
26316 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026317 /* Skip NULL lines (continuation lines). */
26318 while (fcp->linenr < gap->ga_len
26319 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26320 ++fcp->linenr;
26321 if (fcp->linenr >= gap->ga_len)
26322 retval = NULL;
26323 else
26324 {
26325 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26326 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026327#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026328 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026329 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026330#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026332 }
26333
26334 /* Did we encounter a breakpoint? */
26335 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26336 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026337 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026338 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026339 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026340 sourcing_lnum);
26341 fcp->dbg_tick = debug_tick;
26342 }
26343
26344 return retval;
26345}
26346
Bram Moolenaar05159a02005-02-26 23:04:13 +000026347#if defined(FEAT_PROFILE) || defined(PROTO)
26348/*
26349 * Called when starting to read a function line.
26350 * "sourcing_lnum" must be correct!
26351 * When skipping lines it may not actually be executed, but we won't find out
26352 * until later and we need to store the time now.
26353 */
26354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026355func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026356{
26357 funccall_T *fcp = (funccall_T *)cookie;
26358 ufunc_T *fp = fcp->func;
26359
26360 if (fp->uf_profiling && sourcing_lnum >= 1
26361 && sourcing_lnum <= fp->uf_lines.ga_len)
26362 {
26363 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026364 /* Skip continuation lines. */
26365 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26366 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026367 fp->uf_tml_execed = FALSE;
26368 profile_start(&fp->uf_tml_start);
26369 profile_zero(&fp->uf_tml_children);
26370 profile_get_wait(&fp->uf_tml_wait);
26371 }
26372}
26373
26374/*
26375 * Called when actually executing a function line.
26376 */
26377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026378func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026379{
26380 funccall_T *fcp = (funccall_T *)cookie;
26381 ufunc_T *fp = fcp->func;
26382
26383 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26384 fp->uf_tml_execed = TRUE;
26385}
26386
26387/*
26388 * Called when done with a function line.
26389 */
26390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026391func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026392{
26393 funccall_T *fcp = (funccall_T *)cookie;
26394 ufunc_T *fp = fcp->func;
26395
26396 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26397 {
26398 if (fp->uf_tml_execed)
26399 {
26400 ++fp->uf_tml_count[fp->uf_tml_idx];
26401 profile_end(&fp->uf_tml_start);
26402 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026403 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026404 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26405 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026406 }
26407 fp->uf_tml_idx = -1;
26408 }
26409}
26410#endif
26411
Bram Moolenaar071d4272004-06-13 20:20:40 +000026412/*
26413 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026414 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026415 */
26416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026417func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418{
Bram Moolenaar33570922005-01-25 22:26:29 +000026419 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026420
26421 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26422 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026423 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026424 || fcp->returned);
26425}
26426
26427/*
26428 * return TRUE if cookie indicates a function which "abort"s on errors.
26429 */
26430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026431func_has_abort(
26432 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026433{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026434 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026435}
26436
26437#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26438typedef enum
26439{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026440 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26441 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26442 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026443} var_flavour_T;
26444
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026445static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446
26447 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026448var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026449{
26450 char_u *p = varname;
26451
26452 if (ASCII_ISUPPER(*p))
26453 {
26454 while (*(++p))
26455 if (ASCII_ISLOWER(*p))
26456 return VAR_FLAVOUR_SESSION;
26457 return VAR_FLAVOUR_VIMINFO;
26458 }
26459 else
26460 return VAR_FLAVOUR_DEFAULT;
26461}
26462#endif
26463
26464#if defined(FEAT_VIMINFO) || defined(PROTO)
26465/*
26466 * Restore global vars that start with a capital from the viminfo file
26467 */
26468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026469read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026470{
26471 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026472 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026473 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026474 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026475
26476 if (!writing && (find_viminfo_parameter('!') != NULL))
26477 {
26478 tab = vim_strchr(virp->vir_line + 1, '\t');
26479 if (tab != NULL)
26480 {
26481 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026482 switch (*tab)
26483 {
26484 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026485#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026486 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026487#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026488 case 'D': type = VAR_DICT; break;
26489 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026490 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026492
26493 tab = vim_strchr(tab, '\t');
26494 if (tab != NULL)
26495 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026496 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026497 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026498 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026499 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026500#ifdef FEAT_FLOAT
26501 else if (type == VAR_FLOAT)
26502 (void)string2float(tab + 1, &tv.vval.v_float);
26503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026504 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026505 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026506 if (type == VAR_DICT || type == VAR_LIST)
26507 {
26508 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26509
26510 if (etv == NULL)
26511 /* Failed to parse back the dict or list, use it as a
26512 * string. */
26513 tv.v_type = VAR_STRING;
26514 else
26515 {
26516 vim_free(tv.vval.v_string);
26517 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026518 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026519 }
26520 }
26521
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026522 /* when in a function use global variables */
26523 save_funccal = current_funccal;
26524 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026525 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026526 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026527
26528 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026529 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026530 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26531 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532 }
26533 }
26534 }
26535
26536 return viminfo_readline(virp);
26537}
26538
26539/*
26540 * Write global vars that start with a capital to the viminfo file
26541 */
26542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026543write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026544{
Bram Moolenaar33570922005-01-25 22:26:29 +000026545 hashitem_T *hi;
26546 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026547 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026548 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026549 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026550 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026551 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026552
26553 if (find_viminfo_parameter('!') == NULL)
26554 return;
26555
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026556 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026557
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026558 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026559 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026560 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026561 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026563 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026564 this_var = HI2DI(hi);
26565 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026566 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026567 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026568 {
26569 case VAR_STRING: s = "STR"; break;
26570 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026571 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026572 case VAR_DICT: s = "DIC"; break;
26573 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026574 case VAR_SPECIAL: s = "XPL"; break;
26575
26576 case VAR_UNKNOWN:
26577 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026578 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026579 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026580 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026581 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026582 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026583 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026584 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026585 if (p != NULL)
26586 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026587 vim_free(tofree);
26588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026589 }
26590 }
26591}
26592#endif
26593
26594#if defined(FEAT_SESSION) || defined(PROTO)
26595 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026596store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026597{
Bram Moolenaar33570922005-01-25 22:26:29 +000026598 hashitem_T *hi;
26599 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026600 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026601 char_u *p, *t;
26602
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026603 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026604 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026605 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026606 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026607 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026608 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026609 this_var = HI2DI(hi);
26610 if ((this_var->di_tv.v_type == VAR_NUMBER
26611 || this_var->di_tv.v_type == VAR_STRING)
26612 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026613 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026614 /* Escape special characters with a backslash. Turn a LF and
26615 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026616 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026617 (char_u *)"\\\"\n\r");
26618 if (p == NULL) /* out of memory */
26619 break;
26620 for (t = p; *t != NUL; ++t)
26621 if (*t == '\n')
26622 *t = 'n';
26623 else if (*t == '\r')
26624 *t = 'r';
26625 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026626 this_var->di_key,
26627 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26628 : ' ',
26629 p,
26630 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26631 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026632 || put_eol(fd) == FAIL)
26633 {
26634 vim_free(p);
26635 return FAIL;
26636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026637 vim_free(p);
26638 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026639#ifdef FEAT_FLOAT
26640 else if (this_var->di_tv.v_type == VAR_FLOAT
26641 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26642 {
26643 float_T f = this_var->di_tv.vval.v_float;
26644 int sign = ' ';
26645
26646 if (f < 0)
26647 {
26648 f = -f;
26649 sign = '-';
26650 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026651 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026652 this_var->di_key, sign, f) < 0)
26653 || put_eol(fd) == FAIL)
26654 return FAIL;
26655 }
26656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026657 }
26658 }
26659 return OK;
26660}
26661#endif
26662
Bram Moolenaar661b1822005-07-28 22:36:45 +000026663/*
26664 * Display script name where an item was last set.
26665 * Should only be invoked when 'verbose' is non-zero.
26666 */
26667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026668last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026669{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026670 char_u *p;
26671
Bram Moolenaar661b1822005-07-28 22:36:45 +000026672 if (scriptID != 0)
26673 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026674 p = home_replace_save(NULL, get_scriptname(scriptID));
26675 if (p != NULL)
26676 {
26677 verbose_enter();
26678 MSG_PUTS(_("\n\tLast set from "));
26679 MSG_PUTS(p);
26680 vim_free(p);
26681 verbose_leave();
26682 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026683 }
26684}
26685
Bram Moolenaard812df62008-11-09 12:46:09 +000026686/*
26687 * List v:oldfiles in a nice way.
26688 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026689 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026690ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026691{
26692 list_T *l = vimvars[VV_OLDFILES].vv_list;
26693 listitem_T *li;
26694 int nr = 0;
26695
26696 if (l == NULL)
26697 msg((char_u *)_("No old files"));
26698 else
26699 {
26700 msg_start();
26701 msg_scroll = TRUE;
26702 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26703 {
26704 msg_outnum((long)++nr);
26705 MSG_PUTS(": ");
26706 msg_outtrans(get_tv_string(&li->li_tv));
26707 msg_putchar('\n');
26708 out_flush(); /* output one line at a time */
26709 ui_breakcheck();
26710 }
26711 /* Assume "got_int" was set to truncate the listing. */
26712 got_int = FALSE;
26713
26714#ifdef FEAT_BROWSE_CMD
26715 if (cmdmod.browse)
26716 {
26717 quit_more = FALSE;
26718 nr = prompt_for_number(FALSE);
26719 msg_starthere();
26720 if (nr > 0)
26721 {
26722 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26723 (long)nr);
26724
26725 if (p != NULL)
26726 {
26727 p = expand_env_save(p);
26728 eap->arg = p;
26729 eap->cmdidx = CMD_edit;
26730 cmdmod.browse = FALSE;
26731 do_exedit(eap, NULL);
26732 vim_free(p);
26733 }
26734 }
26735 }
26736#endif
26737 }
26738}
26739
Bram Moolenaar53744302015-07-17 17:38:22 +020026740/* reset v:option_new, v:option_old and v:option_type */
26741 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026742reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026743{
26744 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26745 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26746 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26747}
26748
26749
Bram Moolenaar071d4272004-06-13 20:20:40 +000026750#endif /* FEAT_EVAL */
26751
Bram Moolenaar071d4272004-06-13 20:20:40 +000026752
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026753#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026754
26755#ifdef WIN3264
26756/*
26757 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26758 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026759static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26760static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26761static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026762
26763/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026764 * Get the short path (8.3) for the filename in "fnamep".
26765 * Only works for a valid file name.
26766 * When the path gets longer "fnamep" is changed and the allocated buffer
26767 * is put in "bufp".
26768 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26769 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026770 */
26771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026772get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026773{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026774 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026775 char_u *newbuf;
26776
26777 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026778 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026779 if (l > len - 1)
26780 {
26781 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026782 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026783 newbuf = vim_strnsave(*fnamep, l);
26784 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026785 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026786
26787 vim_free(*bufp);
26788 *fnamep = *bufp = newbuf;
26789
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026790 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026791 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026792 }
26793
26794 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026795 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026796}
26797
26798/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026799 * Get the short path (8.3) for the filename in "fname". The converted
26800 * path is returned in "bufp".
26801 *
26802 * Some of the directories specified in "fname" may not exist. This function
26803 * will shorten the existing directories at the beginning of the path and then
26804 * append the remaining non-existing path.
26805 *
26806 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026807 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026808 * bufp - Pointer to an allocated buffer for the filename.
26809 * fnamelen - Length of the filename pointed to by fname
26810 *
26811 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026812 */
26813 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026814shortpath_for_invalid_fname(
26815 char_u **fname,
26816 char_u **bufp,
26817 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026818{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026819 char_u *short_fname, *save_fname, *pbuf_unused;
26820 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026821 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026822 int old_len, len;
26823 int new_len, sfx_len;
26824 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026825
26826 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026827 old_len = *fnamelen;
26828 save_fname = vim_strnsave(*fname, old_len);
26829 pbuf_unused = NULL;
26830 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026831
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026832 endp = save_fname + old_len - 1; /* Find the end of the copy */
26833 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026834
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026835 /*
26836 * Try shortening the supplied path till it succeeds by removing one
26837 * directory at a time from the tail of the path.
26838 */
26839 len = 0;
26840 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026841 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026842 /* go back one path-separator */
26843 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26844 --endp;
26845 if (endp <= save_fname)
26846 break; /* processed the complete path */
26847
26848 /*
26849 * Replace the path separator with a NUL and try to shorten the
26850 * resulting path.
26851 */
26852 ch = *endp;
26853 *endp = 0;
26854 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026855 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026856 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26857 {
26858 retval = FAIL;
26859 goto theend;
26860 }
26861 *endp = ch; /* preserve the string */
26862
26863 if (len > 0)
26864 break; /* successfully shortened the path */
26865
26866 /* failed to shorten the path. Skip the path separator */
26867 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026868 }
26869
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026870 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026871 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026872 /*
26873 * Succeeded in shortening the path. Now concatenate the shortened
26874 * path with the remaining path at the tail.
26875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026876
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026877 /* Compute the length of the new path. */
26878 sfx_len = (int)(save_endp - endp) + 1;
26879 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026880
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026881 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026882 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026883 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026884 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026885 /* There is not enough space in the currently allocated string,
26886 * copy it to a buffer big enough. */
26887 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026888 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026889 {
26890 retval = FAIL;
26891 goto theend;
26892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026893 }
26894 else
26895 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026896 /* Transfer short_fname to the main buffer (it's big enough),
26897 * unless get_short_pathname() did its work in-place. */
26898 *fname = *bufp = save_fname;
26899 if (short_fname != save_fname)
26900 vim_strncpy(save_fname, short_fname, len);
26901 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026902 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026903
26904 /* concat the not-shortened part of the path */
26905 vim_strncpy(*fname + len, endp, sfx_len);
26906 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026907 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026908
26909theend:
26910 vim_free(pbuf_unused);
26911 vim_free(save_fname);
26912
26913 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026914}
26915
26916/*
26917 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026918 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026919 */
26920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026921shortpath_for_partial(
26922 char_u **fnamep,
26923 char_u **bufp,
26924 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026925{
26926 int sepcount, len, tflen;
26927 char_u *p;
26928 char_u *pbuf, *tfname;
26929 int hasTilde;
26930
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026931 /* Count up the path separators from the RHS.. so we know which part
26932 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026933 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026934 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026935 if (vim_ispathsep(*p))
26936 ++sepcount;
26937
26938 /* Need full path first (use expand_env() to remove a "~/") */
26939 hasTilde = (**fnamep == '~');
26940 if (hasTilde)
26941 pbuf = tfname = expand_env_save(*fnamep);
26942 else
26943 pbuf = tfname = FullName_save(*fnamep, FALSE);
26944
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026945 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026946
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026947 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26948 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026949
26950 if (len == 0)
26951 {
26952 /* Don't have a valid filename, so shorten the rest of the
26953 * path if we can. This CAN give us invalid 8.3 filenames, but
26954 * there's not a lot of point in guessing what it might be.
26955 */
26956 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026957 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26958 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026959 }
26960
26961 /* Count the paths backward to find the beginning of the desired string. */
26962 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026963 {
26964#ifdef FEAT_MBYTE
26965 if (has_mbyte)
26966 p -= mb_head_off(tfname, p);
26967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026968 if (vim_ispathsep(*p))
26969 {
26970 if (sepcount == 0 || (hasTilde && sepcount == 1))
26971 break;
26972 else
26973 sepcount --;
26974 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026976 if (hasTilde)
26977 {
26978 --p;
26979 if (p >= tfname)
26980 *p = '~';
26981 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026982 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026983 }
26984 else
26985 ++p;
26986
26987 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26988 vim_free(*bufp);
26989 *fnamelen = (int)STRLEN(p);
26990 *bufp = pbuf;
26991 *fnamep = p;
26992
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026993 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026994}
26995#endif /* WIN3264 */
26996
26997/*
26998 * Adjust a filename, according to a string of modifiers.
26999 * *fnamep must be NUL terminated when called. When returning, the length is
27000 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027001 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027002 * When there is an error, *fnamep is set to NULL.
27003 */
27004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010027005modify_fname(
27006 char_u *src, /* string with modifiers */
27007 int *usedlen, /* characters after src that are used */
27008 char_u **fnamep, /* file name so far */
27009 char_u **bufp, /* buffer for allocated file name or NULL */
27010 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027011{
27012 int valid = 0;
27013 char_u *tail;
27014 char_u *s, *p, *pbuf;
27015 char_u dirname[MAXPATHL];
27016 int c;
27017 int has_fullname = 0;
27018#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027019 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027020 int has_shortname = 0;
27021#endif
27022
27023repeat:
27024 /* ":p" - full path/file_name */
27025 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
27026 {
27027 has_fullname = 1;
27028
27029 valid |= VALID_PATH;
27030 *usedlen += 2;
27031
27032 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
27033 if ((*fnamep)[0] == '~'
27034#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
27035 && ((*fnamep)[1] == '/'
27036# ifdef BACKSLASH_IN_FILENAME
27037 || (*fnamep)[1] == '\\'
27038# endif
27039 || (*fnamep)[1] == NUL)
27040
27041#endif
27042 )
27043 {
27044 *fnamep = expand_env_save(*fnamep);
27045 vim_free(*bufp); /* free any allocated file name */
27046 *bufp = *fnamep;
27047 if (*fnamep == NULL)
27048 return -1;
27049 }
27050
27051 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027052 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000027053 {
27054 if (vim_ispathsep(*p)
27055 && p[1] == '.'
27056 && (p[2] == NUL
27057 || vim_ispathsep(p[2])
27058 || (p[2] == '.'
27059 && (p[3] == NUL || vim_ispathsep(p[3])))))
27060 break;
27061 }
27062
27063 /* FullName_save() is slow, don't use it when not needed. */
27064 if (*p != NUL || !vim_isAbsName(*fnamep))
27065 {
27066 *fnamep = FullName_save(*fnamep, *p != NUL);
27067 vim_free(*bufp); /* free any allocated file name */
27068 *bufp = *fnamep;
27069 if (*fnamep == NULL)
27070 return -1;
27071 }
27072
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027073#ifdef WIN3264
27074# if _WIN32_WINNT >= 0x0500
27075 if (vim_strchr(*fnamep, '~') != NULL)
27076 {
27077 /* Expand 8.3 filename to full path. Needed to make sure the same
27078 * file does not have two different names.
27079 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
27080 p = alloc(_MAX_PATH + 1);
27081 if (p != NULL)
27082 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010027083 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027084 {
27085 vim_free(*bufp);
27086 *bufp = *fnamep = p;
27087 }
27088 else
27089 vim_free(p);
27090 }
27091 }
27092# endif
27093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027094 /* Append a path separator to a directory. */
27095 if (mch_isdir(*fnamep))
27096 {
27097 /* Make room for one or two extra characters. */
27098 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
27099 vim_free(*bufp); /* free any allocated file name */
27100 *bufp = *fnamep;
27101 if (*fnamep == NULL)
27102 return -1;
27103 add_pathsep(*fnamep);
27104 }
27105 }
27106
27107 /* ":." - path relative to the current directory */
27108 /* ":~" - path relative to the home directory */
27109 /* ":8" - shortname path - postponed till after */
27110 while (src[*usedlen] == ':'
27111 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
27112 {
27113 *usedlen += 2;
27114 if (c == '8')
27115 {
27116#ifdef WIN3264
27117 has_shortname = 1; /* Postpone this. */
27118#endif
27119 continue;
27120 }
27121 pbuf = NULL;
27122 /* Need full path first (use expand_env() to remove a "~/") */
27123 if (!has_fullname)
27124 {
27125 if (c == '.' && **fnamep == '~')
27126 p = pbuf = expand_env_save(*fnamep);
27127 else
27128 p = pbuf = FullName_save(*fnamep, FALSE);
27129 }
27130 else
27131 p = *fnamep;
27132
27133 has_fullname = 0;
27134
27135 if (p != NULL)
27136 {
27137 if (c == '.')
27138 {
27139 mch_dirname(dirname, MAXPATHL);
27140 s = shorten_fname(p, dirname);
27141 if (s != NULL)
27142 {
27143 *fnamep = s;
27144 if (pbuf != NULL)
27145 {
27146 vim_free(*bufp); /* free any allocated file name */
27147 *bufp = pbuf;
27148 pbuf = NULL;
27149 }
27150 }
27151 }
27152 else
27153 {
27154 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27155 /* Only replace it when it starts with '~' */
27156 if (*dirname == '~')
27157 {
27158 s = vim_strsave(dirname);
27159 if (s != NULL)
27160 {
27161 *fnamep = s;
27162 vim_free(*bufp);
27163 *bufp = s;
27164 }
27165 }
27166 }
27167 vim_free(pbuf);
27168 }
27169 }
27170
27171 tail = gettail(*fnamep);
27172 *fnamelen = (int)STRLEN(*fnamep);
27173
27174 /* ":h" - head, remove "/file_name", can be repeated */
27175 /* Don't remove the first "/" or "c:\" */
27176 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27177 {
27178 valid |= VALID_HEAD;
27179 *usedlen += 2;
27180 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027181 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027182 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027183 *fnamelen = (int)(tail - *fnamep);
27184#ifdef VMS
27185 if (*fnamelen > 0)
27186 *fnamelen += 1; /* the path separator is part of the path */
27187#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027188 if (*fnamelen == 0)
27189 {
27190 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27191 p = vim_strsave((char_u *)".");
27192 if (p == NULL)
27193 return -1;
27194 vim_free(*bufp);
27195 *bufp = *fnamep = tail = p;
27196 *fnamelen = 1;
27197 }
27198 else
27199 {
27200 while (tail > s && !after_pathsep(s, tail))
27201 mb_ptr_back(*fnamep, tail);
27202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027203 }
27204
27205 /* ":8" - shortname */
27206 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27207 {
27208 *usedlen += 2;
27209#ifdef WIN3264
27210 has_shortname = 1;
27211#endif
27212 }
27213
27214#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027215 /*
27216 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027217 */
27218 if (has_shortname)
27219 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027220 /* Copy the string if it is shortened by :h and when it wasn't copied
27221 * yet, because we are going to change it in place. Avoids changing
27222 * the buffer name for "%:8". */
27223 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027224 {
27225 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027226 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027227 return -1;
27228 vim_free(*bufp);
27229 *bufp = *fnamep = p;
27230 }
27231
27232 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027233 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027234 if (!has_fullname && !vim_isAbsName(*fnamep))
27235 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027236 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027237 return -1;
27238 }
27239 else
27240 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027241 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027242
Bram Moolenaardc935552011-08-17 15:23:23 +020027243 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027244 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027245 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027246 return -1;
27247
27248 if (l == 0)
27249 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027250 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027251 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027252 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027253 return -1;
27254 }
27255 *fnamelen = l;
27256 }
27257 }
27258#endif /* WIN3264 */
27259
27260 /* ":t" - tail, just the basename */
27261 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27262 {
27263 *usedlen += 2;
27264 *fnamelen -= (int)(tail - *fnamep);
27265 *fnamep = tail;
27266 }
27267
27268 /* ":e" - extension, can be repeated */
27269 /* ":r" - root, without extension, can be repeated */
27270 while (src[*usedlen] == ':'
27271 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27272 {
27273 /* find a '.' in the tail:
27274 * - for second :e: before the current fname
27275 * - otherwise: The last '.'
27276 */
27277 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27278 s = *fnamep - 2;
27279 else
27280 s = *fnamep + *fnamelen - 1;
27281 for ( ; s > tail; --s)
27282 if (s[0] == '.')
27283 break;
27284 if (src[*usedlen + 1] == 'e') /* :e */
27285 {
27286 if (s > tail)
27287 {
27288 *fnamelen += (int)(*fnamep - (s + 1));
27289 *fnamep = s + 1;
27290#ifdef VMS
27291 /* cut version from the extension */
27292 s = *fnamep + *fnamelen - 1;
27293 for ( ; s > *fnamep; --s)
27294 if (s[0] == ';')
27295 break;
27296 if (s > *fnamep)
27297 *fnamelen = s - *fnamep;
27298#endif
27299 }
27300 else if (*fnamep <= tail)
27301 *fnamelen = 0;
27302 }
27303 else /* :r */
27304 {
27305 if (s > tail) /* remove one extension */
27306 *fnamelen = (int)(s - *fnamep);
27307 }
27308 *usedlen += 2;
27309 }
27310
27311 /* ":s?pat?foo?" - substitute */
27312 /* ":gs?pat?foo?" - global substitute */
27313 if (src[*usedlen] == ':'
27314 && (src[*usedlen + 1] == 's'
27315 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27316 {
27317 char_u *str;
27318 char_u *pat;
27319 char_u *sub;
27320 int sep;
27321 char_u *flags;
27322 int didit = FALSE;
27323
27324 flags = (char_u *)"";
27325 s = src + *usedlen + 2;
27326 if (src[*usedlen + 1] == 'g')
27327 {
27328 flags = (char_u *)"g";
27329 ++s;
27330 }
27331
27332 sep = *s++;
27333 if (sep)
27334 {
27335 /* find end of pattern */
27336 p = vim_strchr(s, sep);
27337 if (p != NULL)
27338 {
27339 pat = vim_strnsave(s, (int)(p - s));
27340 if (pat != NULL)
27341 {
27342 s = p + 1;
27343 /* find end of substitution */
27344 p = vim_strchr(s, sep);
27345 if (p != NULL)
27346 {
27347 sub = vim_strnsave(s, (int)(p - s));
27348 str = vim_strnsave(*fnamep, *fnamelen);
27349 if (sub != NULL && str != NULL)
27350 {
27351 *usedlen = (int)(p + 1 - src);
27352 s = do_string_sub(str, pat, sub, flags);
27353 if (s != NULL)
27354 {
27355 *fnamep = s;
27356 *fnamelen = (int)STRLEN(s);
27357 vim_free(*bufp);
27358 *bufp = s;
27359 didit = TRUE;
27360 }
27361 }
27362 vim_free(sub);
27363 vim_free(str);
27364 }
27365 vim_free(pat);
27366 }
27367 }
27368 /* after using ":s", repeat all the modifiers */
27369 if (didit)
27370 goto repeat;
27371 }
27372 }
27373
Bram Moolenaar26df0922014-02-23 23:39:13 +010027374 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27375 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027376 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027377 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027378 if (c != NUL)
27379 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027380 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027381 if (c != NUL)
27382 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027383 if (p == NULL)
27384 return -1;
27385 vim_free(*bufp);
27386 *bufp = *fnamep = p;
27387 *fnamelen = (int)STRLEN(p);
27388 *usedlen += 2;
27389 }
27390
Bram Moolenaar071d4272004-06-13 20:20:40 +000027391 return valid;
27392}
27393
27394/*
27395 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27396 * "flags" can be "g" to do a global substitute.
27397 * Returns an allocated string, NULL for error.
27398 */
27399 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027400do_string_sub(
27401 char_u *str,
27402 char_u *pat,
27403 char_u *sub,
27404 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027405{
27406 int sublen;
27407 regmatch_T regmatch;
27408 int i;
27409 int do_all;
27410 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027411 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027412 garray_T ga;
27413 char_u *ret;
27414 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027415 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027416
27417 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27418 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027419 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027420
27421 ga_init2(&ga, 1, 200);
27422
27423 do_all = (flags[0] == 'g');
27424
27425 regmatch.rm_ic = p_ic;
27426 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27427 if (regmatch.regprog != NULL)
27428 {
27429 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027430 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027431 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27432 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027433 /* Skip empty match except for first match. */
27434 if (regmatch.startp[0] == regmatch.endp[0])
27435 {
27436 if (zero_width == regmatch.startp[0])
27437 {
27438 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027439 i = MB_PTR2LEN(tail);
27440 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27441 (size_t)i);
27442 ga.ga_len += i;
27443 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027444 continue;
27445 }
27446 zero_width = regmatch.startp[0];
27447 }
27448
Bram Moolenaar071d4272004-06-13 20:20:40 +000027449 /*
27450 * Get some space for a temporary buffer to do the substitution
27451 * into. It will contain:
27452 * - The text up to where the match is.
27453 * - The substituted text.
27454 * - The text after the match.
27455 */
27456 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027457 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027458 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27459 {
27460 ga_clear(&ga);
27461 break;
27462 }
27463
27464 /* copy the text up to where the match is */
27465 i = (int)(regmatch.startp[0] - tail);
27466 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27467 /* add the substituted text */
27468 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27469 + ga.ga_len + i, TRUE, TRUE, FALSE);
27470 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027471 tail = regmatch.endp[0];
27472 if (*tail == NUL)
27473 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027474 if (!do_all)
27475 break;
27476 }
27477
27478 if (ga.ga_data != NULL)
27479 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27480
Bram Moolenaar473de612013-06-08 18:19:48 +020027481 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027482 }
27483
27484 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27485 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027486 if (p_cpo == empty_option)
27487 p_cpo = save_cpo;
27488 else
27489 /* Darn, evaluating {sub} expression changed the value. */
27490 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027491
27492 return ret;
27493}
27494
27495#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */