blob: a90dd0b732be6b8460c360d1b967291395b3c962 [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 Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
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 Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100454#ifdef FEAT_JOB
455static void job_free(job_T *job);
456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
458static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
462static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
463static 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, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
483static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_asin(typval_T *argvars, typval_T *rettv);
486static void f_atan(typval_T *argvars, typval_T *rettv);
487static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_browse(typval_T *argvars, typval_T *rettv);
490static void f_browsedir(typval_T *argvars, typval_T *rettv);
491static void f_bufexists(typval_T *argvars, typval_T *rettv);
492static void f_buflisted(typval_T *argvars, typval_T *rettv);
493static void f_bufloaded(typval_T *argvars, typval_T *rettv);
494static void f_bufname(typval_T *argvars, typval_T *rettv);
495static void f_bufnr(typval_T *argvars, typval_T *rettv);
496static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
497static void f_byte2line(typval_T *argvars, typval_T *rettv);
498static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
499static void f_byteidx(typval_T *argvars, typval_T *rettv);
500static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
501static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100505#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100506static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100507static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
508static void f_ch_open(typval_T *argvars, typval_T *rettv);
509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100512static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_changenr(typval_T *argvars, typval_T *rettv);
515static void f_char2nr(typval_T *argvars, typval_T *rettv);
516static void f_cindent(typval_T *argvars, typval_T *rettv);
517static void f_clearmatches(typval_T *argvars, typval_T *rettv);
518static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000519#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_complete(typval_T *argvars, typval_T *rettv);
521static void f_complete_add(typval_T *argvars, typval_T *rettv);
522static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_confirm(typval_T *argvars, typval_T *rettv);
525static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_cos(typval_T *argvars, typval_T *rettv);
528static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_count(typval_T *argvars, typval_T *rettv);
531static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
532static void f_cursor(typval_T *argsvars, typval_T *rettv);
533static void f_deepcopy(typval_T *argvars, typval_T *rettv);
534static void f_delete(typval_T *argvars, typval_T *rettv);
535static void f_did_filetype(typval_T *argvars, typval_T *rettv);
536static void f_diff_filler(typval_T *argvars, typval_T *rettv);
537static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100538static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_empty(typval_T *argvars, typval_T *rettv);
540static void f_escape(typval_T *argvars, typval_T *rettv);
541static void f_eval(typval_T *argvars, typval_T *rettv);
542static void f_eventhandler(typval_T *argvars, typval_T *rettv);
543static void f_executable(typval_T *argvars, typval_T *rettv);
544static void f_exepath(typval_T *argvars, typval_T *rettv);
545static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_expand(typval_T *argvars, typval_T *rettv);
550static void f_extend(typval_T *argvars, typval_T *rettv);
551static void f_feedkeys(typval_T *argvars, typval_T *rettv);
552static void f_filereadable(typval_T *argvars, typval_T *rettv);
553static void f_filewritable(typval_T *argvars, typval_T *rettv);
554static void f_filter(typval_T *argvars, typval_T *rettv);
555static void f_finddir(typval_T *argvars, typval_T *rettv);
556static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_float2nr(typval_T *argvars, typval_T *rettv);
559static void f_floor(typval_T *argvars, typval_T *rettv);
560static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_fnameescape(typval_T *argvars, typval_T *rettv);
563static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
564static void f_foldclosed(typval_T *argvars, typval_T *rettv);
565static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
566static void f_foldlevel(typval_T *argvars, typval_T *rettv);
567static void f_foldtext(typval_T *argvars, typval_T *rettv);
568static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
569static void f_foreground(typval_T *argvars, typval_T *rettv);
570static void f_function(typval_T *argvars, typval_T *rettv);
571static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
572static void f_get(typval_T *argvars, typval_T *rettv);
573static void f_getbufline(typval_T *argvars, typval_T *rettv);
574static void f_getbufvar(typval_T *argvars, typval_T *rettv);
575static void f_getchar(typval_T *argvars, typval_T *rettv);
576static void f_getcharmod(typval_T *argvars, typval_T *rettv);
577static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
578static void f_getcmdline(typval_T *argvars, typval_T *rettv);
579static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
580static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
581static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
582static void f_getcwd(typval_T *argvars, typval_T *rettv);
583static void f_getfontname(typval_T *argvars, typval_T *rettv);
584static void f_getfperm(typval_T *argvars, typval_T *rettv);
585static void f_getfsize(typval_T *argvars, typval_T *rettv);
586static void f_getftime(typval_T *argvars, typval_T *rettv);
587static void f_getftype(typval_T *argvars, typval_T *rettv);
588static void f_getline(typval_T *argvars, typval_T *rettv);
589static void f_getmatches(typval_T *argvars, typval_T *rettv);
590static void f_getpid(typval_T *argvars, typval_T *rettv);
591static void f_getcurpos(typval_T *argvars, typval_T *rettv);
592static void f_getpos(typval_T *argvars, typval_T *rettv);
593static void f_getqflist(typval_T *argvars, typval_T *rettv);
594static void f_getreg(typval_T *argvars, typval_T *rettv);
595static void f_getregtype(typval_T *argvars, typval_T *rettv);
596static void f_gettabvar(typval_T *argvars, typval_T *rettv);
597static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
598static void f_getwinposx(typval_T *argvars, typval_T *rettv);
599static void f_getwinposy(typval_T *argvars, typval_T *rettv);
600static void f_getwinvar(typval_T *argvars, typval_T *rettv);
601static void f_glob(typval_T *argvars, typval_T *rettv);
602static void f_globpath(typval_T *argvars, typval_T *rettv);
603static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
604static void f_has(typval_T *argvars, typval_T *rettv);
605static void f_has_key(typval_T *argvars, typval_T *rettv);
606static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
607static void f_hasmapto(typval_T *argvars, typval_T *rettv);
608static void f_histadd(typval_T *argvars, typval_T *rettv);
609static void f_histdel(typval_T *argvars, typval_T *rettv);
610static void f_histget(typval_T *argvars, typval_T *rettv);
611static void f_histnr(typval_T *argvars, typval_T *rettv);
612static void f_hlID(typval_T *argvars, typval_T *rettv);
613static void f_hlexists(typval_T *argvars, typval_T *rettv);
614static void f_hostname(typval_T *argvars, typval_T *rettv);
615static void f_iconv(typval_T *argvars, typval_T *rettv);
616static void f_indent(typval_T *argvars, typval_T *rettv);
617static void f_index(typval_T *argvars, typval_T *rettv);
618static void f_input(typval_T *argvars, typval_T *rettv);
619static void f_inputdialog(typval_T *argvars, typval_T *rettv);
620static void f_inputlist(typval_T *argvars, typval_T *rettv);
621static void f_inputrestore(typval_T *argvars, typval_T *rettv);
622static void f_inputsave(typval_T *argvars, typval_T *rettv);
623static void f_inputsecret(typval_T *argvars, typval_T *rettv);
624static void f_insert(typval_T *argvars, typval_T *rettv);
625static void f_invert(typval_T *argvars, typval_T *rettv);
626static void f_isdirectory(typval_T *argvars, typval_T *rettv);
627static void f_islocked(typval_T *argvars, typval_T *rettv);
628static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100629#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100630# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100631static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100632# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +0100633static void f_job_start(typval_T *argvars, typval_T *rettv);
634static void f_job_stop(typval_T *argvars, typval_T *rettv);
635static void f_job_status(typval_T *argvars, typval_T *rettv);
636#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100637static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100638static void f_js_decode(typval_T *argvars, typval_T *rettv);
639static void f_js_encode(typval_T *argvars, typval_T *rettv);
640static void f_json_decode(typval_T *argvars, typval_T *rettv);
641static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100642static void f_keys(typval_T *argvars, typval_T *rettv);
643static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
644static void f_len(typval_T *argvars, typval_T *rettv);
645static void f_libcall(typval_T *argvars, typval_T *rettv);
646static void f_libcallnr(typval_T *argvars, typval_T *rettv);
647static void f_line(typval_T *argvars, typval_T *rettv);
648static void f_line2byte(typval_T *argvars, typval_T *rettv);
649static void f_lispindent(typval_T *argvars, typval_T *rettv);
650static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100652static void f_log(typval_T *argvars, typval_T *rettv);
653static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200655#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200657#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_map(typval_T *argvars, typval_T *rettv);
659static void f_maparg(typval_T *argvars, typval_T *rettv);
660static void f_mapcheck(typval_T *argvars, typval_T *rettv);
661static void f_match(typval_T *argvars, typval_T *rettv);
662static void f_matchadd(typval_T *argvars, typval_T *rettv);
663static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
664static void f_matcharg(typval_T *argvars, typval_T *rettv);
665static void f_matchdelete(typval_T *argvars, typval_T *rettv);
666static void f_matchend(typval_T *argvars, typval_T *rettv);
667static void f_matchlist(typval_T *argvars, typval_T *rettv);
668static void f_matchstr(typval_T *argvars, typval_T *rettv);
669static void f_max(typval_T *argvars, typval_T *rettv);
670static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000671#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000673#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100675#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
679static void f_nr2char(typval_T *argvars, typval_T *rettv);
680static void f_or(typval_T *argvars, typval_T *rettv);
681static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100682#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100684#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000685#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
689static void f_printf(typval_T *argvars, typval_T *rettv);
690static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200691#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200693#endif
694#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_range(typval_T *argvars, typval_T *rettv);
698static void f_readfile(typval_T *argvars, typval_T *rettv);
699static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100700#ifdef FEAT_FLOAT
701static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
702#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_reltimestr(typval_T *argvars, typval_T *rettv);
704static void f_remote_expr(typval_T *argvars, typval_T *rettv);
705static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
706static void f_remote_peek(typval_T *argvars, typval_T *rettv);
707static void f_remote_read(typval_T *argvars, typval_T *rettv);
708static void f_remote_send(typval_T *argvars, typval_T *rettv);
709static void f_remove(typval_T *argvars, typval_T *rettv);
710static void f_rename(typval_T *argvars, typval_T *rettv);
711static void f_repeat(typval_T *argvars, typval_T *rettv);
712static void f_resolve(typval_T *argvars, typval_T *rettv);
713static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000714#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100717static void f_screenattr(typval_T *argvars, typval_T *rettv);
718static void f_screenchar(typval_T *argvars, typval_T *rettv);
719static void f_screencol(typval_T *argvars, typval_T *rettv);
720static void f_screenrow(typval_T *argvars, typval_T *rettv);
721static void f_search(typval_T *argvars, typval_T *rettv);
722static void f_searchdecl(typval_T *argvars, typval_T *rettv);
723static void f_searchpair(typval_T *argvars, typval_T *rettv);
724static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
725static void f_searchpos(typval_T *argvars, typval_T *rettv);
726static void f_server2client(typval_T *argvars, typval_T *rettv);
727static void f_serverlist(typval_T *argvars, typval_T *rettv);
728static void f_setbufvar(typval_T *argvars, typval_T *rettv);
729static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
730static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
731static void f_setline(typval_T *argvars, typval_T *rettv);
732static void f_setloclist(typval_T *argvars, typval_T *rettv);
733static void f_setmatches(typval_T *argvars, typval_T *rettv);
734static void f_setpos(typval_T *argvars, typval_T *rettv);
735static void f_setqflist(typval_T *argvars, typval_T *rettv);
736static void f_setreg(typval_T *argvars, typval_T *rettv);
737static void f_settabvar(typval_T *argvars, typval_T *rettv);
738static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
739static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100740#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100742#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_shellescape(typval_T *argvars, typval_T *rettv);
744static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
745static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000746#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_sin(typval_T *argvars, typval_T *rettv);
748static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000749#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_sort(typval_T *argvars, typval_T *rettv);
751static void f_soundfold(typval_T *argvars, typval_T *rettv);
752static void f_spellbadword(typval_T *argvars, typval_T *rettv);
753static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
754static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000755#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_sqrt(typval_T *argvars, typval_T *rettv);
757static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000758#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_str2nr(typval_T *argvars, typval_T *rettv);
760static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000763#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_stridx(typval_T *argvars, typval_T *rettv);
765static void f_string(typval_T *argvars, typval_T *rettv);
766static void f_strlen(typval_T *argvars, typval_T *rettv);
767static void f_strpart(typval_T *argvars, typval_T *rettv);
768static void f_strridx(typval_T *argvars, typval_T *rettv);
769static void f_strtrans(typval_T *argvars, typval_T *rettv);
770static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
771static void f_strwidth(typval_T *argvars, typval_T *rettv);
772static void f_submatch(typval_T *argvars, typval_T *rettv);
773static void f_substitute(typval_T *argvars, typval_T *rettv);
774static void f_synID(typval_T *argvars, typval_T *rettv);
775static void f_synIDattr(typval_T *argvars, typval_T *rettv);
776static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
777static void f_synstack(typval_T *argvars, typval_T *rettv);
778static void f_synconcealed(typval_T *argvars, typval_T *rettv);
779static void f_system(typval_T *argvars, typval_T *rettv);
780static void f_systemlist(typval_T *argvars, typval_T *rettv);
781static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
782static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
783static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
784static void f_taglist(typval_T *argvars, typval_T *rettv);
785static void f_tagfiles(typval_T *argvars, typval_T *rettv);
786static void f_tempname(typval_T *argvars, typval_T *rettv);
787static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200788#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_tan(typval_T *argvars, typval_T *rettv);
790static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200791#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100792static void f_tolower(typval_T *argvars, typval_T *rettv);
793static void f_toupper(typval_T *argvars, typval_T *rettv);
794static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000795#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000797#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100798static void f_type(typval_T *argvars, typval_T *rettv);
799static void f_undofile(typval_T *argvars, typval_T *rettv);
800static void f_undotree(typval_T *argvars, typval_T *rettv);
801static void f_uniq(typval_T *argvars, typval_T *rettv);
802static void f_values(typval_T *argvars, typval_T *rettv);
803static void f_virtcol(typval_T *argvars, typval_T *rettv);
804static void f_visualmode(typval_T *argvars, typval_T *rettv);
805static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
806static void f_winbufnr(typval_T *argvars, typval_T *rettv);
807static void f_wincol(typval_T *argvars, typval_T *rettv);
808static void f_winheight(typval_T *argvars, typval_T *rettv);
809static void f_winline(typval_T *argvars, typval_T *rettv);
810static void f_winnr(typval_T *argvars, typval_T *rettv);
811static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
812static void f_winrestview(typval_T *argvars, typval_T *rettv);
813static void f_winsaveview(typval_T *argvars, typval_T *rettv);
814static void f_winwidth(typval_T *argvars, typval_T *rettv);
815static void f_writefile(typval_T *argvars, typval_T *rettv);
816static void f_wordcount(typval_T *argvars, typval_T *rettv);
817static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000818
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
820static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
821static int get_env_len(char_u **arg);
822static int get_id_len(char_u **arg);
823static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
824static 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 +0000825#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
826#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
827 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100828static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
829static int eval_isnamec(int c);
830static int eval_isnamec1(int c);
831static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
832static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static typval_T *alloc_string_tv(char_u *string);
834static void init_tv(typval_T *varp);
835static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100836#ifdef FEAT_FLOAT
837static float_T get_tv_float(typval_T *varp);
838#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static linenr_T get_tv_lnum(typval_T *argvars);
840static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
841static char_u *get_tv_string(typval_T *varp);
842static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
843static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
844static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
845static hashtab_T *find_var_ht(char_u *name, char_u **varname);
846static funccall_T *get_funccal(void);
847static void vars_clear_ext(hashtab_T *ht, int free_val);
848static void delete_var(hashtab_T *ht, hashitem_T *hi);
849static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
850static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
851static void set_var(char_u *name, typval_T *varp, int copy);
852static int var_check_ro(int flags, char_u *name, int use_gettext);
853static int var_check_fixed(int flags, char_u *name, int use_gettext);
854static int var_check_func_name(char_u *name, int new_var);
855static int valid_varname(char_u *varname);
856static int tv_check_lock(int lock, char_u *name, int use_gettext);
857static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
858static char_u *find_option_end(char_u **arg, int *opt_flags);
859static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
860static int eval_fname_script(char_u *p);
861static int eval_fname_sid(char_u *p);
862static void list_func_head(ufunc_T *fp, int indent);
863static ufunc_T *find_func(char_u *name);
864static int function_exists(char_u *name);
865static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000866#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100867static void func_do_profile(ufunc_T *fp);
868static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
869static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000870static int
871# ifdef __BORLANDC__
872 _RTLENTRYF
873# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000875static int
876# ifdef __BORLANDC__
877 _RTLENTRYF
878# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000880#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881static int script_autoload(char_u *name, int reload);
882static char_u *autoload_name(char_u *name);
883static void cat_func_name(char_u *buf, ufunc_T *fp);
884static void func_free(ufunc_T *fp);
885static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
886static int can_free_funccal(funccall_T *fc, int copyID) ;
887static void free_funccal(funccall_T *fc, int free_val);
888static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
889static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
890static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
891static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
892static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
893static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
894static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
895static int write_list(FILE *fd, list_T *list, int binary);
896static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000897
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200898
899#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100900static int compare_func_name(const void *s1, const void *s2);
901static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200902#endif
903
Bram Moolenaar33570922005-01-25 22:26:29 +0000904/*
905 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000906 */
907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100908eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000909{
Bram Moolenaar33570922005-01-25 22:26:29 +0000910 int i;
911 struct vimvar *p;
912
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200913 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
914 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200915 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000916 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000918
919 for (i = 0; i < VV_LEN; ++i)
920 {
921 p = &vimvars[i];
922 STRCPY(p->vv_di.di_key, p->vv_name);
923 if (p->vv_flags & VV_RO)
924 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
925 else if (p->vv_flags & VV_RO_SBX)
926 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
927 else
928 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000929
930 /* add to v: scope dict, unless the value is not always available */
931 if (p->vv_type != VAR_UNKNOWN)
932 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000933 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000934 /* add to compat scope dict */
935 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000936 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100937 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
938
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000939 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100940 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200941 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100942 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100943
944 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
945 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
946 set_vim_var_nr(VV_NONE, VVAL_NONE);
947 set_vim_var_nr(VV_NULL, VVAL_NULL);
948
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200949 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200950
951#ifdef EBCDIC
952 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100953 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200954 */
955 sortFunctions();
956#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000957}
958
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000959#if defined(EXITFREE) || defined(PROTO)
960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100961eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962{
963 int i;
964 struct vimvar *p;
965
966 for (i = 0; i < VV_LEN; ++i)
967 {
968 p = &vimvars[i];
969 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000970 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000971 vim_free(p->vv_str);
972 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000973 }
974 else if (p->vv_di.di_tv.v_type == VAR_LIST)
975 {
976 list_unref(p->vv_list);
977 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000978 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000979 }
980 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000981 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000982 hash_clear(&compat_hashtab);
983
Bram Moolenaard9fba312005-06-26 22:34:35 +0000984 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100985# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200986 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100987# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000988
989 /* global variables */
990 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000992 /* autoloaded script names */
993 ga_clear_strings(&ga_loaded);
994
Bram Moolenaarcca74132013-09-25 21:00:28 +0200995 /* Script-local variables. First clear all the variables and in a second
996 * loop free the scriptvar_T, because a variable in one script might hold
997 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200998 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200999 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001000 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001001 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001002 ga_clear(&ga_scripts);
1003
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001004 /* unreferenced lists and dicts */
1005 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001006
1007 /* functions */
1008 free_all_functions();
1009 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010}
1011#endif
1012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 * Return the name of the executed function.
1015 */
1016 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001017func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020}
1021
1022/*
1023 * Return the address holding the next breakpoint line for a funccall cookie.
1024 */
1025 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001026func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
Bram Moolenaar33570922005-01-25 22:26:29 +00001028 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029}
1030
1031/*
1032 * Return the address holding the debug tick for a funccall cookie.
1033 */
1034 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001035func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
Bram Moolenaar33570922005-01-25 22:26:29 +00001037 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038}
1039
1040/*
1041 * Return the nesting level for a funccall cookie.
1042 */
1043 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001044func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
Bram Moolenaar33570922005-01-25 22:26:29 +00001046 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047}
1048
1049/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001050funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001052/* pointer to list of previously used funccal, still around because some
1053 * item in it is still being used. */
1054funccall_T *previous_funccal = NULL;
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056/*
1057 * Return TRUE when a function was ended by a ":return" command.
1058 */
1059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001060current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
1062 return current_funccal->returned;
1063}
1064
1065
1066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 * Set an internal variable to a string value. Creates the variable if it does
1068 * not already exist.
1069 */
1070 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001071set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001073 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001074 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 val = vim_strsave(value);
1077 if (val != NULL)
1078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001079 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001080 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001082 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001083 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 }
1086}
1087
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001089static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090static char_u *redir_endp = NULL;
1091static char_u *redir_varname = NULL;
1092
1093/*
1094 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001095 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 * Returns OK if successfully completed the setup. FAIL otherwise.
1097 */
1098 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001099var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100{
1101 int save_emsg;
1102 int err;
1103 typval_T tv;
1104
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001105 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001106 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 {
1108 EMSG(_(e_invarg));
1109 return FAIL;
1110 }
1111
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001112 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 redir_varname = vim_strsave(name);
1114 if (redir_varname == NULL)
1115 return FAIL;
1116
1117 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1118 if (redir_lval == NULL)
1119 {
1120 var_redir_stop();
1121 return FAIL;
1122 }
1123
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 /* The output is stored in growarray "redir_ga" until redirection ends. */
1125 ga_init2(&redir_ga, (int)sizeof(char), 500);
1126
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001128 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001129 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1131 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001132 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 if (redir_endp != NULL && *redir_endp != NUL)
1134 /* Trailing characters are present after the variable name */
1135 EMSG(_(e_trailing));
1136 else
1137 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 var_redir_stop();
1140 return FAIL;
1141 }
1142
1143 /* check if we can write to the variable: set it to or append an empty
1144 * string */
1145 save_emsg = did_emsg;
1146 did_emsg = FALSE;
1147 tv.v_type = VAR_STRING;
1148 tv.vval.v_string = (char_u *)"";
1149 if (append)
1150 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1151 else
1152 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001153 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001155 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 if (err)
1157 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 var_redir_stop();
1160 return FAIL;
1161 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162
1163 return OK;
1164}
1165
1166/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 * Append "value[value_len]" to the variable set by var_redir_start().
1168 * The actual appending is postponed until redirection ends, because the value
1169 * appended may in fact be the string we write to, changing it may cause freed
1170 * memory to be used:
1171 * :redir => foo
1172 * :let foo
1173 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 */
1175 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001176var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001178 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179
1180 if (redir_lval == NULL)
1181 return;
1182
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001183 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001184 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 {
1190 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001192 }
1193 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195}
1196
1197/*
1198 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001199 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 */
1201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001202var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001204 typval_T tv;
1205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 if (redir_lval != NULL)
1207 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001208 /* If there was no error: assign the text to the variable. */
1209 if (redir_endp != NULL)
1210 {
1211 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1212 tv.v_type = VAR_STRING;
1213 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001214 /* Call get_lval() again, if it's inside a Dict or List it may
1215 * have changed. */
1216 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001217 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001218 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1219 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1220 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001221 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001222
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001223 /* free the collected output */
1224 vim_free(redir_ga.ga_data);
1225 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 vim_free(redir_lval);
1228 redir_lval = NULL;
1229 }
1230 vim_free(redir_varname);
1231 redir_varname = NULL;
1232}
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234# if defined(FEAT_MBYTE) || defined(PROTO)
1235 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001236eval_charconvert(
1237 char_u *enc_from,
1238 char_u *enc_to,
1239 char_u *fname_from,
1240 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241{
1242 int err = FALSE;
1243
1244 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1245 set_vim_var_string(VV_CC_TO, enc_to, -1);
1246 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1247 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1248 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1249 err = TRUE;
1250 set_vim_var_string(VV_CC_FROM, NULL, -1);
1251 set_vim_var_string(VV_CC_TO, NULL, -1);
1252 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1253 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1254
1255 if (err)
1256 return FAIL;
1257 return OK;
1258}
1259# endif
1260
1261# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001263eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 int err = FALSE;
1266
1267 set_vim_var_string(VV_FNAME_IN, fname, -1);
1268 set_vim_var_string(VV_CMDARG, args, -1);
1269 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1270 err = TRUE;
1271 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1272 set_vim_var_string(VV_CMDARG, NULL, -1);
1273
1274 if (err)
1275 {
1276 mch_remove(fname);
1277 return FAIL;
1278 }
1279 return OK;
1280}
1281# endif
1282
1283# if defined(FEAT_DIFF) || defined(PROTO)
1284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001285eval_diff(
1286 char_u *origfile,
1287 char_u *newfile,
1288 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 int err = FALSE;
1291
1292 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1293 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1294 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1295 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1296 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1297 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1298 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1299}
1300
1301 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001302eval_patch(
1303 char_u *origfile,
1304 char_u *difffile,
1305 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 int err;
1308
1309 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1310 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1311 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1312 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1313 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1314 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1315 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1316}
1317# endif
1318
1319/*
1320 * Top level evaluation function, returning a boolean.
1321 * Sets "error" to TRUE if there was an error.
1322 * Return TRUE or FALSE.
1323 */
1324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001325eval_to_bool(
1326 char_u *arg,
1327 int *error,
1328 char_u **nextcmd,
1329 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
Bram Moolenaar33570922005-01-25 22:26:29 +00001331 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 int retval = FALSE;
1333
1334 if (skip)
1335 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 else
1339 {
1340 *error = FALSE;
1341 if (!skip)
1342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001343 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 }
1347 if (skip)
1348 --emsg_skip;
1349
1350 return retval;
1351}
1352
1353/*
1354 * Top level evaluation function, returning a string. If "skip" is TRUE,
1355 * only parsing to "nextcmd" is done, without reporting errors. Return
1356 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1357 */
1358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001359eval_to_string_skip(
1360 char_u *arg,
1361 char_u **nextcmd,
1362 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar33570922005-01-25 22:26:29 +00001364 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 char_u *retval;
1366
1367 if (skip)
1368 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 retval = NULL;
1371 else
1372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 retval = vim_strsave(get_tv_string(&tv));
1374 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376 if (skip)
1377 --emsg_skip;
1378
1379 return retval;
1380}
1381
1382/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001383 * Skip over an expression at "*pp".
1384 * Return FAIL for an error, OK otherwise.
1385 */
1386 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001387skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001388{
Bram Moolenaar33570922005-01-25 22:26:29 +00001389 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390
1391 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001393}
1394
1395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001397 * When "convert" is TRUE convert a List into a sequence of lines and convert
1398 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 * Return pointer to allocated memory, or NULL for failure.
1400 */
1401 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402eval_to_string(
1403 char_u *arg,
1404 char_u **nextcmd,
1405 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001410#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001411 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 retval = NULL;
1416 else
1417 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001418 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001419 {
1420 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001421 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001422 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001423 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001424 if (tv.vval.v_list->lv_len > 0)
1425 ga_append(&ga, NL);
1426 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001427 ga_append(&ga, NUL);
1428 retval = (char_u *)ga.ga_data;
1429 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001430#ifdef FEAT_FLOAT
1431 else if (convert && tv.v_type == VAR_FLOAT)
1432 {
1433 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1434 retval = vim_strsave(numbuf);
1435 }
1436#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001437 else
1438 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
1441
1442 return retval;
1443}
1444
1445/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001446 * Call eval_to_string() without using current local variables and using
1447 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 */
1449 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001450eval_to_string_safe(
1451 char_u *arg,
1452 char_u **nextcmd,
1453 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 char_u *retval;
1456 void *save_funccalp;
1457
1458 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001459 if (use_sandbox)
1460 ++sandbox;
1461 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001462 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001463 if (use_sandbox)
1464 --sandbox;
1465 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 restore_funccal(save_funccalp);
1467 return retval;
1468}
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470/*
1471 * Top level evaluation function, returning a number.
1472 * Evaluates "expr" silently.
1473 * Returns -1 for an error.
1474 */
1475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001476eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
Bram Moolenaar33570922005-01-25 22:26:29 +00001478 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001480 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
1482 ++emsg_off;
1483
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 retval = -1;
1486 else
1487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001488 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 --emsg_off;
1492
1493 return retval;
1494}
1495
Bram Moolenaara40058a2005-07-11 22:42:07 +00001496/*
1497 * Prepare v: variable "idx" to be used.
1498 * Save the current typeval in "save_tv".
1499 * When not used yet add the variable to the v: hashtable.
1500 */
1501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503{
1504 *save_tv = vimvars[idx].vv_tv;
1505 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1506 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1507}
1508
1509/*
1510 * Restore v: variable "idx" to typeval "save_tv".
1511 * When no longer defined, remove the variable from the v: hashtable.
1512 */
1513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001514restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001515{
1516 hashitem_T *hi;
1517
Bram Moolenaara40058a2005-07-11 22:42:07 +00001518 vimvars[idx].vv_tv = *save_tv;
1519 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1520 {
1521 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1522 if (HASHITEM_EMPTY(hi))
1523 EMSG2(_(e_intern2), "restore_vimvar()");
1524 else
1525 hash_remove(&vimvarht, hi);
1526 }
1527}
1528
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001529#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001530/*
1531 * Evaluate an expression to a list with suggestions.
1532 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001533 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001534 */
1535 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537{
1538 typval_T save_val;
1539 typval_T rettv;
1540 list_T *list = NULL;
1541 char_u *p = skipwhite(expr);
1542
1543 /* Set "v:val" to the bad word. */
1544 prepare_vimvar(VV_VAL, &save_val);
1545 vimvars[VV_VAL].vv_type = VAR_STRING;
1546 vimvars[VV_VAL].vv_str = badword;
1547 if (p_verbose == 0)
1548 ++emsg_off;
1549
1550 if (eval1(&p, &rettv, TRUE) == OK)
1551 {
1552 if (rettv.v_type != VAR_LIST)
1553 clear_tv(&rettv);
1554 else
1555 list = rettv.vval.v_list;
1556 }
1557
1558 if (p_verbose == 0)
1559 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001560 restore_vimvar(VV_VAL, &save_val);
1561
1562 return list;
1563}
1564
1565/*
1566 * "list" is supposed to contain two items: a word and a number. Return the
1567 * word in "pp" and the number as the return value.
1568 * Return -1 if anything isn't right.
1569 * Used to get the good word and score from the eval_spell_expr() result.
1570 */
1571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001573{
1574 listitem_T *li;
1575
1576 li = list->lv_first;
1577 if (li == NULL)
1578 return -1;
1579 *pp = get_tv_string(&li->li_tv);
1580
1581 li = li->li_next;
1582 if (li == NULL)
1583 return -1;
1584 return get_tv_number(&li->li_tv);
1585}
1586#endif
1587
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001588/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001589 * Top level evaluation function.
1590 * Returns an allocated typval_T with the result.
1591 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001592 */
1593 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001595{
1596 typval_T *tv;
1597
1598 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001599 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600 {
1601 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001602 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603 }
1604
1605 return tv;
1606}
1607
1608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001611 * Uses argv[argc] for the function arguments. Only Number and String
1612 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616call_vim_function(
1617 char_u *func,
1618 int argc,
1619 char_u **argv,
1620 int safe, /* use the sandbox */
1621 int str_arg_only, /* all arguments are strings */
1622 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 long n;
1626 int len;
1627 int i;
1628 int doesrange;
1629 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001632 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636 for (i = 0; i < argc; i++)
1637 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001638 /* Pass a NULL or empty argument as an empty string */
1639 if (argv[i] == NULL || *argv[i] == NUL)
1640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001641 argvars[i].v_type = VAR_STRING;
1642 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001643 continue;
1644 }
1645
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001646 if (str_arg_only)
1647 len = 0;
1648 else
1649 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001650 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (len != 0 && len == (int)STRLEN(argv[i]))
1652 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001653 argvars[i].v_type = VAR_NUMBER;
1654 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 }
1656 else
1657 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001658 argvars[i].v_type = VAR_STRING;
1659 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 }
1662
1663 if (safe)
1664 {
1665 save_funccalp = save_funccal();
1666 ++sandbox;
1667 }
1668
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1670 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (safe)
1674 {
1675 --sandbox;
1676 restore_funccal(save_funccalp);
1677 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678 vim_free(argvars);
1679
1680 if (ret == FAIL)
1681 clear_tv(rettv);
1682
1683 return ret;
1684}
1685
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001686/*
1687 * Call vimL function "func" and return the result as a number.
1688 * Returns -1 when calling the function fails.
1689 * Uses argv[argc] for the function arguments.
1690 */
1691 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001692call_func_retnr(
1693 char_u *func,
1694 int argc,
1695 char_u **argv,
1696 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001697{
1698 typval_T rettv;
1699 long retval;
1700
1701 /* All arguments are passed as strings, no conversion to number. */
1702 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1703 return -1;
1704
1705 retval = get_tv_number_chk(&rettv, NULL);
1706 clear_tv(&rettv);
1707 return retval;
1708}
1709
1710#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1711 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1712
Bram Moolenaar4f688582007-07-24 12:34:30 +00001713# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001715 * Call vimL function "func" and return the result as a string.
1716 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717 * Uses argv[argc] for the function arguments.
1718 */
1719 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001720call_func_retstr(
1721 char_u *func,
1722 int argc,
1723 char_u **argv,
1724 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725{
1726 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001727 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001729 /* All arguments are passed as strings, no conversion to number. */
1730 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731 return NULL;
1732
1733 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 return retval;
1736}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001737# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001739/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001740 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001742 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743 */
1744 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001745call_func_retlist(
1746 char_u *func,
1747 int argc,
1748 char_u **argv,
1749 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750{
1751 typval_T rettv;
1752
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001753 /* All arguments are passed as strings, no conversion to number. */
1754 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755 return NULL;
1756
1757 if (rettv.v_type != VAR_LIST)
1758 {
1759 clear_tv(&rettv);
1760 return NULL;
1761 }
1762
1763 return rettv.vval.v_list;
1764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
1766
1767/*
1768 * Save the current function call pointer, and set it to NULL.
1769 * Used when executing autocommands and for ":source".
1770 */
1771 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001772save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001774 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 current_funccal = NULL;
1777 return (void *)fc;
1778}
1779
1780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001783 funccall_T *fc = (funccall_T *)vfc;
1784
1785 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786}
1787
Bram Moolenaar05159a02005-02-26 23:04:13 +00001788#if defined(FEAT_PROFILE) || defined(PROTO)
1789/*
1790 * Prepare profiling for entering a child or something else that is not
1791 * counted for the script/function itself.
1792 * Should always be called in pair with prof_child_exit().
1793 */
1794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001795prof_child_enter(
1796 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001797{
1798 funccall_T *fc = current_funccal;
1799
1800 if (fc != NULL && fc->func->uf_profiling)
1801 profile_start(&fc->prof_child);
1802 script_prof_save(tm);
1803}
1804
1805/*
1806 * Take care of time spent in a child.
1807 * Should always be called after prof_child_enter().
1808 */
1809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001810prof_child_exit(
1811 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001812{
1813 funccall_T *fc = current_funccal;
1814
1815 if (fc != NULL && fc->func->uf_profiling)
1816 {
1817 profile_end(&fc->prof_child);
1818 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1819 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1820 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1821 }
1822 script_prof_restore(tm);
1823}
1824#endif
1825
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#ifdef FEAT_FOLDING
1828/*
1829 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1830 * it in "*cp". Doesn't give error messages.
1831 */
1832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
Bram Moolenaar33570922005-01-25 22:26:29 +00001835 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 int retval;
1837 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001838 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1839 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001842 if (use_sandbox)
1843 ++sandbox;
1844 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 retval = 0;
1848 else
1849 {
1850 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 if (tv.v_type == VAR_NUMBER)
1852 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001853 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 retval = 0;
1855 else
1856 {
1857 /* If the result is a string, check if there is a non-digit before
1858 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (!VIM_ISDIGIT(*s) && *s != '-')
1861 *cp = *s++;
1862 retval = atol((char *)s);
1863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001867 if (use_sandbox)
1868 --sandbox;
1869 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
1871 return retval;
1872}
1873#endif
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 * ":let" list all variable values
1877 * ":let var1 var2" list variable values
1878 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 * ":let var += expr" assignment command.
1880 * ":let var -= expr" assignment command.
1881 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 */
1884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001885ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886{
1887 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 int var_count = 0;
1892 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001894 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001895 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
Bram Moolenaardb552d602006-03-23 22:59:57 +00001897 argend = skip_var_list(arg, &var_count, &semicolon);
1898 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001900 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1901 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 expr = skipwhite(argend);
1903 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1904 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001906 /*
1907 * ":let" without "=": list variables
1908 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 if (*arg == '[')
1910 EMSG(_(e_invarg));
1911 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001913 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001917 list_glob_vars(&first);
1918 list_buf_vars(&first);
1919 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001920#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001922#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 list_script_vars(&first);
1924 list_func_vars(&first);
1925 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 eap->nextcmd = check_nextcmd(arg);
1928 }
1929 else
1930 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 op[0] = '=';
1932 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001933 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1936 op[0] = *expr; /* +=, -= or .= */
1937 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001939 else
1940 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (eap->skip)
1943 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (eap->skip)
1946 {
1947 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 --emsg_skip;
1950 }
1951 else if (i != FAIL)
1952 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001954 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 }
1958}
1959
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960/*
1961 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1962 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 * When "nextchars" is not NULL it points to a string with characters that
1964 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1965 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 * Returns OK or FAIL;
1967 */
1968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001969ex_let_vars(
1970 char_u *arg_start,
1971 typval_T *tv,
1972 int copy, /* copy values from "tv", don't move */
1973 int semicolon, /* from skip_var_list() */
1974 int var_count, /* from skip_var_list() */
1975 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976{
1977 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001978 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 listitem_T *item;
1981 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982
1983 if (*arg != '[')
1984 {
1985 /*
1986 * ":let var = expr" or ":for var in list"
1987 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 return FAIL;
1990 return OK;
1991 }
1992
1993 /*
1994 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1995 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001996 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 {
1998 EMSG(_(e_listreq));
1999 return FAIL;
2000 }
2001
2002 i = list_len(l);
2003 if (semicolon == 0 && var_count < i)
2004 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002005 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 return FAIL;
2007 }
2008 if (var_count - semicolon > i)
2009 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002010 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 return FAIL;
2012 }
2013
2014 item = l->lv_first;
2015 while (*arg != ']')
2016 {
2017 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002018 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 item = item->li_next;
2020 if (arg == NULL)
2021 return FAIL;
2022
2023 arg = skipwhite(arg);
2024 if (*arg == ';')
2025 {
2026 /* Put the rest of the list (may be empty) in the var after ';'.
2027 * Create a new list for this. */
2028 l = list_alloc();
2029 if (l == NULL)
2030 return FAIL;
2031 while (item != NULL)
2032 {
2033 list_append_tv(l, &item->li_tv);
2034 item = item->li_next;
2035 }
2036
2037 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002038 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 ltv.vval.v_list = l;
2040 l->lv_refcount = 1;
2041
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2043 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 clear_tv(&ltv);
2045 if (arg == NULL)
2046 return FAIL;
2047 break;
2048 }
2049 else if (*arg != ',' && *arg != ']')
2050 {
2051 EMSG2(_(e_intern2), "ex_let_vars()");
2052 return FAIL;
2053 }
2054 }
2055
2056 return OK;
2057}
2058
2059/*
2060 * Skip over assignable variable "var" or list of variables "[var, var]".
2061 * Used for ":let varvar = expr" and ":for varvar in expr".
2062 * For "[var, var]" increment "*var_count" for each variable.
2063 * for "[var, var; var]" set "semicolon".
2064 * Return NULL for an error.
2065 */
2066 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002067skip_var_list(
2068 char_u *arg,
2069 int *var_count,
2070 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071{
2072 char_u *p, *s;
2073
2074 if (*arg == '[')
2075 {
2076 /* "[var, var]": find the matching ']'. */
2077 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002078 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079 {
2080 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2081 s = skip_var_one(p);
2082 if (s == p)
2083 {
2084 EMSG2(_(e_invarg2), p);
2085 return NULL;
2086 }
2087 ++*var_count;
2088
2089 p = skipwhite(s);
2090 if (*p == ']')
2091 break;
2092 else if (*p == ';')
2093 {
2094 if (*semicolon == 1)
2095 {
2096 EMSG(_("Double ; in list of variables"));
2097 return NULL;
2098 }
2099 *semicolon = 1;
2100 }
2101 else if (*p != ',')
2102 {
2103 EMSG2(_(e_invarg2), p);
2104 return NULL;
2105 }
2106 }
2107 return p + 1;
2108 }
2109 else
2110 return skip_var_one(arg);
2111}
2112
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002114 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002115 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002118skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002119{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002120 if (*arg == '@' && arg[1] != NUL)
2121 return arg + 2;
2122 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2123 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124}
2125
Bram Moolenaara7043832005-01-21 11:56:39 +00002126/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 * List variables for hashtab "ht" with prefix "prefix".
2128 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002129 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002131list_hashtable_vars(
2132 hashtab_T *ht,
2133 char_u *prefix,
2134 int empty,
2135 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002136{
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 hashitem_T *hi;
2138 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 int todo;
2140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002141 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2143 {
2144 if (!HASHITEM_EMPTY(hi))
2145 {
2146 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002147 di = HI2DI(hi);
2148 if (empty || di->di_tv.v_type != VAR_STRING
2149 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151 }
2152 }
2153}
2154
2155/*
2156 * List global variables.
2157 */
2158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002159list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002160{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002162}
2163
2164/*
2165 * List buffer variables.
2166 */
2167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002168list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002169{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002170 char_u numbuf[NUMBUFLEN];
2171
Bram Moolenaar429fa852013-04-15 12:27:36 +02002172 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174
2175 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2177 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002178}
2179
2180/*
2181 * List window variables.
2182 */
2183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002184list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002185{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002186 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002188}
2189
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002190#ifdef FEAT_WINDOWS
2191/*
2192 * List tab page variables.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002197 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002199}
2200#endif
2201
Bram Moolenaara7043832005-01-21 11:56:39 +00002202/*
2203 * List Vim variables.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209}
2210
2211/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212 * List script-local variables, if there is a script.
2213 */
2214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002215list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216{
2217 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2219 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220}
2221
2222/*
2223 * List function variables, if there is a function.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227{
2228 if (current_funccal != NULL)
2229 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002231}
2232
2233/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 * List variables in "arg".
2235 */
2236 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238{
2239 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 char_u *name_start;
2243 char_u *arg_subsc;
2244 char_u *tofree;
2245 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246
2247 while (!ends_excmd(*arg) && !got_int)
2248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002251 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2253 {
2254 emsg_severe = TRUE;
2255 EMSG(_(e_trailing));
2256 break;
2257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 /* get_name_len() takes care of expanding curly braces */
2262 name_start = name = arg;
2263 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2264 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 /* This is mainly to keep test 49 working: when expanding
2267 * curly braces fails overrule the exception error message. */
2268 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 emsg_severe = TRUE;
2271 EMSG2(_(e_invarg2), arg);
2272 break;
2273 }
2274 error = TRUE;
2275 }
2276 else
2277 {
2278 if (tofree != NULL)
2279 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002280 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 else
2283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 /* handle d.key, l[idx], f(expr) */
2285 arg_subsc = arg;
2286 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 'g': list_glob_vars(first); break;
2295 case 'b': list_buf_vars(first); break;
2296 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002297#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002299#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002300 case 'v': list_vim_vars(first); break;
2301 case 's': list_script_vars(first); break;
2302 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 default:
2304 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
2307 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 {
2309 char_u numbuf[NUMBUFLEN];
2310 char_u *tf;
2311 int c;
2312 char_u *s;
2313
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002314 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 c = *arg;
2316 *arg = NUL;
2317 list_one_var_a((char_u *)"",
2318 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002319 tv.v_type,
2320 s == NULL ? (char_u *)"" : s,
2321 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 *arg = c;
2323 vim_free(tf);
2324 }
2325 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
2328 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329
2330 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332
2333 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
2335
2336 return arg;
2337}
2338
2339/*
2340 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2341 * Returns a pointer to the char just after the var name.
2342 * Returns NULL if there is an error.
2343 */
2344 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002345ex_let_one(
2346 char_u *arg, /* points to variable name */
2347 typval_T *tv, /* value to assign to variable */
2348 int copy, /* copy value from "tv" */
2349 char_u *endchars, /* valid chars after variable name or NULL */
2350 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351{
2352 int c1;
2353 char_u *name;
2354 char_u *p;
2355 char_u *arg_end = NULL;
2356 int len;
2357 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359
2360 /*
2361 * ":let $VAR = expr": Set environment variable.
2362 */
2363 if (*arg == '$')
2364 {
2365 /* Find the end of the name. */
2366 ++arg;
2367 name = arg;
2368 len = get_env_len(&arg);
2369 if (len == 0)
2370 EMSG2(_(e_invarg2), name - 1);
2371 else
2372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 if (op != NULL && (*op == '+' || *op == '-'))
2374 EMSG2(_(e_letwrong), op);
2375 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002376 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002378 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 {
2380 c1 = name[len];
2381 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 p = get_tv_string_chk(tv);
2383 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 {
2385 int mustfree = FALSE;
2386 char_u *s = vim_getenv(name, &mustfree);
2387
2388 if (s != NULL)
2389 {
2390 p = tofree = concat_str(s, p);
2391 if (mustfree)
2392 vim_free(s);
2393 }
2394 }
2395 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002398 if (STRICMP(name, "HOME") == 0)
2399 init_homedir();
2400 else if (didset_vim && STRICMP(name, "VIM") == 0)
2401 didset_vim = FALSE;
2402 else if (didset_vimruntime
2403 && STRICMP(name, "VIMRUNTIME") == 0)
2404 didset_vimruntime = FALSE;
2405 arg_end = arg;
2406 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 }
2410 }
2411 }
2412
2413 /*
2414 * ":let &option = expr": Set option value.
2415 * ":let &l:option = expr": Set local option value.
2416 * ":let &g:option = expr": Set global option value.
2417 */
2418 else if (*arg == '&')
2419 {
2420 /* Find the end of the name. */
2421 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002422 if (p == NULL || (endchars != NULL
2423 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 EMSG(_(e_letunexp));
2425 else
2426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 long n;
2428 int opt_type;
2429 long numval;
2430 char_u *stringval = NULL;
2431 char_u *s;
2432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 c1 = *p;
2434 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435
2436 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002437 s = get_tv_string_chk(tv); /* != NULL if number or string */
2438 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 {
2440 opt_type = get_option_value(arg, &numval,
2441 &stringval, opt_flags);
2442 if ((opt_type == 1 && *op == '.')
2443 || (opt_type == 0 && *op != '.'))
2444 EMSG2(_(e_letwrong), op);
2445 else
2446 {
2447 if (opt_type == 1) /* number */
2448 {
2449 if (*op == '+')
2450 n = numval + n;
2451 else
2452 n = numval - n;
2453 }
2454 else if (opt_type == 0 && stringval != NULL) /* string */
2455 {
2456 s = concat_str(stringval, s);
2457 vim_free(stringval);
2458 stringval = s;
2459 }
2460 }
2461 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002462 if (s != NULL)
2463 {
2464 set_option_value(arg, n, s, opt_flags);
2465 arg_end = p;
2466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 }
2470 }
2471
2472 /*
2473 * ":let @r = expr": Set register contents.
2474 */
2475 else if (*arg == '@')
2476 {
2477 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 if (op != NULL && (*op == '+' || *op == '-'))
2479 EMSG2(_(e_letwrong), op);
2480 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002481 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 EMSG(_(e_letunexp));
2483 else
2484 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002485 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 char_u *s;
2487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002488 p = get_tv_string_chk(tv);
2489 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002491 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 if (s != NULL)
2493 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002494 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 vim_free(s);
2496 }
2497 }
2498 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 arg_end = arg + 1;
2502 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002503 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 }
2505 }
2506
2507 /*
2508 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002511 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002515 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2519 EMSG(_(e_letunexp));
2520 else
2521 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002522 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 arg_end = p;
2524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 }
2528
2529 else
2530 EMSG2(_(e_invarg2), arg);
2531
2532 return arg_end;
2533}
2534
2535/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2537 */
2538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002539check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540{
2541 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2542 {
2543 EMSG2(_(e_readonlyvar), arg);
2544 return TRUE;
2545 }
2546 return FALSE;
2547}
2548
2549/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 * Get an lval: variable, Dict item or List item that can be assigned a value
2551 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2552 * "name.key", "name.key[expr]" etc.
2553 * Indexing only works if "name" is an existing List or Dictionary.
2554 * "name" points to the start of the name.
2555 * If "rettv" is not NULL it points to the value to be assigned.
2556 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2557 * wrong; must end in space or cmd separator.
2558 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002559 * flags:
2560 * GLV_QUIET: do not give error messages
2561 * GLV_NO_AUTOLOAD: do not use script autoloading
2562 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002564 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 */
2567 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002568get_lval(
2569 char_u *name,
2570 typval_T *rettv,
2571 lval_T *lp,
2572 int unlet,
2573 int skip,
2574 int flags, /* GLV_ values */
2575 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 char_u *expr_start, *expr_end;
2579 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 dictitem_T *v;
2581 typval_T var1;
2582 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002588 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592
2593 if (skip)
2594 {
2595 /* When skipping just find the end of the name. */
2596 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002597 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 }
2599
2600 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002601 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (expr_start != NULL)
2603 {
2604 /* Don't expand the name when we already know there is an error. */
2605 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2606 && *p != '[' && *p != '.')
2607 {
2608 EMSG(_(e_trailing));
2609 return NULL;
2610 }
2611
2612 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2613 if (lp->ll_exp_name == NULL)
2614 {
2615 /* Report an invalid expression in braces, unless the
2616 * expression evaluation has been cancelled due to an
2617 * aborting error, an interrupt, or an exception. */
2618 if (!aborting() && !quiet)
2619 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002620 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 EMSG2(_(e_invarg2), name);
2622 return NULL;
2623 }
2624 }
2625 lp->ll_name = lp->ll_exp_name;
2626 }
2627 else
2628 lp->ll_name = name;
2629
2630 /* Without [idx] or .key we are done. */
2631 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2632 return p;
2633
2634 cc = *p;
2635 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002636 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (v == NULL && !quiet)
2638 EMSG2(_(e_undefvar), lp->ll_name);
2639 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 if (v == NULL)
2641 return NULL;
2642
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 /*
2644 * Loop until no more [idx] or .key is following.
2645 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2650 && !(lp->ll_tv->v_type == VAR_DICT
2651 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_("E689: Can only index a List or Dictionary"));
2655 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E708: [:] must come last"));
2661 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 len = -1;
2665 if (*p == '.')
2666 {
2667 key = p + 1;
2668 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2669 ;
2670 if (len == 0)
2671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!quiet)
2673 EMSG(_(e_emptykey));
2674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676 p = key + len;
2677 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 else
2679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (*p == ':')
2683 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 else
2685 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 empty1 = FALSE;
2687 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002689 if (get_tv_string_chk(&var1) == NULL)
2690 {
2691 /* not a number or string */
2692 clear_tv(&var1);
2693 return NULL;
2694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 }
2696
2697 /* Optionally get the second index [ :expr]. */
2698 if (*p == ':')
2699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 if (!empty1)
2705 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (rettv != NULL && (rettv->v_type != VAR_LIST
2709 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (!quiet)
2712 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (!empty1)
2714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
2717 p = skipwhite(p + 1);
2718 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (!empty1)
2726 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002729 if (get_tv_string_chk(&var2) == NULL)
2730 {
2731 /* not a number or string */
2732 if (!empty1)
2733 clear_tv(&var1);
2734 clear_tv(&var2);
2735 return NULL;
2736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002742
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (!quiet)
2746 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (!empty1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 }
2753
2754 /* Skip to past ']'. */
2755 ++p;
2756 }
2757
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 {
2760 if (len == -1)
2761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 if (*key == NUL)
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
2767 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 lp->ll_list = NULL;
2773 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002774 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002775
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002776 /* When assigning to a scope dictionary check that a function and
2777 * variable name is valid (only variable name unless it is l: or
2778 * g: dictionary). Disallow overwriting a builtin function. */
2779 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 int prevval;
2782 int wrong;
2783
2784 if (len != -1)
2785 {
2786 prevval = key[len];
2787 key[len] = NUL;
2788 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002789 else
2790 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2792 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002794 || !valid_varname(key);
2795 if (len != -1)
2796 key[len] = prevval;
2797 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 return NULL;
2799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 /* Can't add "v:" variable. */
2804 if (lp->ll_dict == &vimvardict)
2805 {
2806 EMSG2(_(e_illvar), name);
2807 return NULL;
2808 }
2809
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002810 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 if (len == -1)
2816 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 }
2819 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 if (len == -1)
2824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 p = NULL;
2827 break;
2828 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002829 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002830 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002831 return NULL;
2832
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 if (len == -1)
2834 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 }
2837 else
2838 {
2839 /*
2840 * Get the number and item for the only or first index of the List.
2841 */
2842 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 else
2845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002846 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 clear_tv(&var1);
2848 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_list = lp->ll_tv->vval.v_list;
2851 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2852 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002854 if (lp->ll_n1 < 0)
2855 {
2856 lp->ll_n1 = 0;
2857 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2858 }
2859 }
2860 if (lp->ll_li == NULL)
2861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 }
2868
2869 /*
2870 * May need to find the item or absolute index for the second
2871 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 * When no index given: "lp->ll_empty2" is TRUE.
2873 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002877 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002883 {
2884 if (!quiet)
2885 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002887 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2892 if (lp->ll_n1 < 0)
2893 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2894 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 {
2896 if (!quiet)
2897 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002899 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002900 }
2901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002904 }
2905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return p;
2907}
2908
2909/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 */
2912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002913clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914{
2915 vim_free(lp->ll_exp_name);
2916 vim_free(lp->ll_newkey);
2917}
2918
2919/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002920 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 */
2924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925set_var_lval(
2926 lval_T *lp,
2927 char_u *endp,
2928 typval_T *rettv,
2929 int copy,
2930 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931{
2932 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002933 listitem_T *ri;
2934 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935
2936 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 cc = *endp;
2941 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 if (op != NULL && *op != '=')
2943 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002946 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002948 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002951 if ((di == NULL
2952 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2953 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2954 FALSE)))
2955 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 set_var(lp->ll_name, &tv, FALSE);
2957 clear_tv(&tv);
2958 }
2959 }
2960 else
2961 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002965 else if (tv_check_lock(lp->ll_newkey == NULL
2966 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002967 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002968 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 else if (lp->ll_range)
2970 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002971 listitem_T *ll_li = lp->ll_li;
2972 int ll_n1 = lp->ll_n1;
2973
2974 /*
2975 * Check whether any of the list items is locked
2976 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002977 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002979 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002980 return;
2981 ri = ri->li_next;
2982 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2983 break;
2984 ll_li = ll_li->li_next;
2985 ++ll_n1;
2986 }
2987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /*
2989 * Assign the List values to the list items.
2990 */
2991 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 if (op != NULL && *op != '=')
2994 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2995 else
2996 {
2997 clear_tv(&lp->ll_li->li_tv);
2998 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2999 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 ri = ri->li_next;
3001 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3002 break;
3003 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003006 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 ri = NULL;
3009 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003010 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 lp->ll_li = lp->ll_li->li_next;
3013 ++lp->ll_n1;
3014 }
3015 if (ri != NULL)
3016 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 else if (lp->ll_empty2
3018 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 : lp->ll_n1 != lp->ll_n2)
3020 EMSG(_("E711: List value has not enough items"));
3021 }
3022 else
3023 {
3024 /*
3025 * Assign to a List or Dictionary item.
3026 */
3027 if (lp->ll_newkey != NULL)
3028 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 if (op != NULL && *op != '=')
3030 {
3031 EMSG2(_(e_letwrong), op);
3032 return;
3033 }
3034
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003036 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 if (di == NULL)
3038 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003039 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3040 {
3041 vim_free(di);
3042 return;
3043 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003045 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 else if (op != NULL && *op != '=')
3047 {
3048 tv_op(lp->ll_tv, rettv, op);
3049 return;
3050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003053
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 /*
3055 * Assign the value to the variable or list item.
3056 */
3057 if (copy)
3058 copy_tv(rettv, lp->ll_tv);
3059 else
3060 {
3061 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003062 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064 }
3065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066}
3067
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003068/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3070 * Returns OK or FAIL.
3071 */
3072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003073tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074{
3075 long n;
3076 char_u numbuf[NUMBUFLEN];
3077 char_u *s;
3078
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003079 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3080 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3081 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 {
3083 switch (tv1->v_type)
3084 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003085 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 case VAR_DICT:
3087 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003088 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003089 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003090 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 break;
3092
3093 case VAR_LIST:
3094 if (*op != '+' || tv2->v_type != VAR_LIST)
3095 break;
3096 /* List += List */
3097 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3098 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3099 return OK;
3100
3101 case VAR_NUMBER:
3102 case VAR_STRING:
3103 if (tv2->v_type == VAR_LIST)
3104 break;
3105 if (*op == '+' || *op == '-')
3106 {
3107 /* nr += nr or nr -= nr*/
3108 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003109#ifdef FEAT_FLOAT
3110 if (tv2->v_type == VAR_FLOAT)
3111 {
3112 float_T f = n;
3113
3114 if (*op == '+')
3115 f += tv2->vval.v_float;
3116 else
3117 f -= tv2->vval.v_float;
3118 clear_tv(tv1);
3119 tv1->v_type = VAR_FLOAT;
3120 tv1->vval.v_float = f;
3121 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123#endif
3124 {
3125 if (*op == '+')
3126 n += get_tv_number(tv2);
3127 else
3128 n -= get_tv_number(tv2);
3129 clear_tv(tv1);
3130 tv1->v_type = VAR_NUMBER;
3131 tv1->vval.v_number = n;
3132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 }
3134 else
3135 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136 if (tv2->v_type == VAR_FLOAT)
3137 break;
3138
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 /* str .= str */
3140 s = get_tv_string(tv1);
3141 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3142 clear_tv(tv1);
3143 tv1->v_type = VAR_STRING;
3144 tv1->vval.v_string = s;
3145 }
3146 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147
3148#ifdef FEAT_FLOAT
3149 case VAR_FLOAT:
3150 {
3151 float_T f;
3152
3153 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3154 && tv2->v_type != VAR_NUMBER
3155 && tv2->v_type != VAR_STRING))
3156 break;
3157 if (tv2->v_type == VAR_FLOAT)
3158 f = tv2->vval.v_float;
3159 else
3160 f = get_tv_number(tv2);
3161 if (*op == '+')
3162 tv1->vval.v_float += f;
3163 else
3164 tv1->vval.v_float -= f;
3165 }
3166 return OK;
3167#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003168 }
3169 }
3170
3171 EMSG2(_(e_letwrong), op);
3172 return FAIL;
3173}
3174
3175/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 * Add a watcher to a list.
3177 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180{
3181 lw->lw_next = l->lv_watch;
3182 l->lv_watch = lw;
3183}
3184
3185/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003186 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 * No warning when it isn't found...
3188 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003190list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193
3194 lwp = &l->lv_watch;
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 {
3197 if (lw == lwrem)
3198 {
3199 *lwp = lw->lw_next;
3200 break;
3201 }
3202 lwp = &lw->lw_next;
3203 }
3204}
3205
3206/*
3207 * Just before removing an item from a list: advance watchers to the next
3208 * item.
3209 */
3210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3216 if (lw->lw_item == item)
3217 lw->lw_item = item->li_next;
3218}
3219
3220/*
3221 * Evaluate the expression used in a ":for var in expr" command.
3222 * "arg" points to "var".
3223 * Set "*errp" to TRUE for an error, FALSE otherwise;
3224 * Return a pointer that holds the info. Null when there is an error.
3225 */
3226 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227eval_for_line(
3228 char_u *arg,
3229 int *errp,
3230 char_u **nextcmdp,
3231 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 typval_T tv;
3236 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237
3238 *errp = TRUE; /* default: there is an error */
3239
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 if (fi == NULL)
3242 return NULL;
3243
3244 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3245 if (expr == NULL)
3246 return fi;
3247
3248 expr = skipwhite(expr);
3249 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3250 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003251 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 return fi;
3253 }
3254
3255 if (skip)
3256 ++emsg_skip;
3257 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3258 {
3259 *errp = FALSE;
3260 if (!skip)
3261 {
3262 l = tv.vval.v_list;
3263 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 clear_tv(&tv);
3267 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 else
3269 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003270 /* No need to increment the refcount, it's already set for the
3271 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 fi->fi_list = l;
3273 list_add_watch(l, &fi->fi_lw);
3274 fi->fi_lw.lw_item = l->lv_first;
3275 }
3276 }
3277 }
3278 if (skip)
3279 --emsg_skip;
3280
3281 return fi;
3282}
3283
3284/*
3285 * Use the first item in a ":for" list. Advance to the next.
3286 * Assign the values to the variable (list). "arg" points to the first one.
3287 * Return TRUE when a valid item was found, FALSE when at end of list or
3288 * something wrong.
3289 */
3290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003291next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003293 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296
3297 item = fi->fi_lw.lw_item;
3298 if (item == NULL)
3299 result = FALSE;
3300 else
3301 {
3302 fi->fi_lw.lw_item = item->li_next;
3303 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3304 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3305 }
3306 return result;
3307}
3308
3309/*
3310 * Free the structure used to store info used by ":for".
3311 */
3312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003313free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314{
Bram Moolenaar33570922005-01-25 22:26:29 +00003315 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003317 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003318 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 list_unref(fi->fi_list);
3321 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 vim_free(fi);
3323}
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3326
3327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328set_context_for_expression(
3329 expand_T *xp,
3330 char_u *arg,
3331 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 int got_eq = FALSE;
3334 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 if (cmdidx == CMD_let)
3338 {
3339 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003340 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 {
3342 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003343 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 {
3345 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003346 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 if (vim_iswhite(*p))
3348 break;
3349 }
3350 return;
3351 }
3352 }
3353 else
3354 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3355 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 while ((xp->xp_pattern = vim_strpbrk(arg,
3357 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3358 {
3359 c = *xp->xp_pattern;
3360 if (c == '&')
3361 {
3362 c = xp->xp_pattern[1];
3363 if (c == '&')
3364 {
3365 ++xp->xp_pattern;
3366 xp->xp_context = cmdidx != CMD_let || got_eq
3367 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3368 }
3369 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003372 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3373 xp->xp_pattern += 2;
3374
3375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 else if (c == '$')
3378 {
3379 /* environment variable */
3380 xp->xp_context = EXPAND_ENV_VARS;
3381 }
3382 else if (c == '=')
3383 {
3384 got_eq = TRUE;
3385 xp->xp_context = EXPAND_EXPRESSION;
3386 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003387 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 && xp->xp_context == EXPAND_FUNCTIONS
3389 && vim_strchr(xp->xp_pattern, '(') == NULL)
3390 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003391 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 break;
3393 }
3394 else if (cmdidx != CMD_let || got_eq)
3395 {
3396 if (c == '"') /* string */
3397 {
3398 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3399 if (c == '\\' && xp->xp_pattern[1] != NUL)
3400 ++xp->xp_pattern;
3401 xp->xp_context = EXPAND_NOTHING;
3402 }
3403 else if (c == '\'') /* literal string */
3404 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003405 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3407 /* skip */ ;
3408 xp->xp_context = EXPAND_NOTHING;
3409 }
3410 else if (c == '|')
3411 {
3412 if (xp->xp_pattern[1] == '|')
3413 {
3414 ++xp->xp_pattern;
3415 xp->xp_context = EXPAND_EXPRESSION;
3416 }
3417 else
3418 xp->xp_context = EXPAND_COMMANDS;
3419 }
3420 else
3421 xp->xp_context = EXPAND_EXPRESSION;
3422 }
3423 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003424 /* Doesn't look like something valid, expand as an expression
3425 * anyway. */
3426 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 arg = xp->xp_pattern;
3428 if (*arg != NUL)
3429 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3430 /* skip */ ;
3431 }
3432 xp->xp_pattern = arg;
3433}
3434
3435#endif /* FEAT_CMDL_COMPL */
3436
3437/*
3438 * ":1,25call func(arg1, arg2)" function call.
3439 */
3440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003441ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 char_u *arg = eap->arg;
3444 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003446 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 linenr_T lnum;
3450 int doesrange;
3451 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003454 if (eap->skip)
3455 {
3456 /* trans_function_name() doesn't work well when skipping, use eval0()
3457 * instead to skip to any following command, e.g. for:
3458 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003459 ++emsg_skip;
3460 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3461 clear_tv(&rettv);
3462 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003463 return;
3464 }
3465
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003466 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003467 if (fudi.fd_newkey != NULL)
3468 {
3469 /* Still need to give an error message for missing key. */
3470 EMSG2(_(e_dictkey), fudi.fd_newkey);
3471 vim_free(fudi.fd_newkey);
3472 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003473 if (tofree == NULL)
3474 return;
3475
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003476 /* Increase refcount on dictionary, it could get deleted when evaluating
3477 * the arguments. */
3478 if (fudi.fd_dict != NULL)
3479 ++fudi.fd_dict->dv_refcount;
3480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003482 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003483 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaar532c7802005-01-27 14:44:31 +00003485 /* Skip white space to allow ":call func ()". Not good, but required for
3486 * backward compatibility. */
3487 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003488 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 if (*startarg != '(')
3491 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003492 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 goto end;
3494 }
3495
3496 /*
3497 * When skipping, evaluate the function once, to find the end of the
3498 * arguments.
3499 * When the function takes a range, this is discovered after the first
3500 * call, and the loop is broken.
3501 */
3502 if (eap->skip)
3503 {
3504 ++emsg_skip;
3505 lnum = eap->line2; /* do it once, also with an invalid range */
3506 }
3507 else
3508 lnum = eap->line1;
3509 for ( ; lnum <= eap->line2; ++lnum)
3510 {
3511 if (!eap->skip && eap->addr_count > 0)
3512 {
3513 curwin->w_cursor.lnum = lnum;
3514 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003515#ifdef FEAT_VIRTUALEDIT
3516 curwin->w_cursor.coladd = 0;
3517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003520 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003521 eap->line1, eap->line2, &doesrange,
3522 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 failed = TRUE;
3525 break;
3526 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003527
3528 /* Handle a function returning a Funcref, Dictionary or List. */
3529 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3530 {
3531 failed = TRUE;
3532 break;
3533 }
3534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003535 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (doesrange || eap->skip)
3537 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003542 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (aborting())
3544 break;
3545 }
3546 if (eap->skip)
3547 --emsg_skip;
3548
3549 if (!failed)
3550 {
3551 /* Check for trailing illegal characters and a following command. */
3552 if (!ends_excmd(*arg))
3553 {
3554 emsg_severe = TRUE;
3555 EMSG(_(e_trailing));
3556 }
3557 else
3558 eap->nextcmd = check_nextcmd(arg);
3559 }
3560
3561end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003562 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003563 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564}
3565
3566/*
3567 * ":unlet[!] var1 ... " command.
3568 */
3569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003570ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 ex_unletlock(eap, eap->arg, 0);
3573}
3574
3575/*
3576 * ":lockvar" and ":unlockvar" commands
3577 */
3578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 int deep = 2;
3583
3584 if (eap->forceit)
3585 deep = -1;
3586 else if (vim_isdigit(*arg))
3587 {
3588 deep = getdigits(&arg);
3589 arg = skipwhite(arg);
3590 }
3591
3592 ex_unletlock(eap, arg, deep);
3593}
3594
3595/*
3596 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3597 */
3598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003599ex_unletlock(
3600 exarg_T *eap,
3601 char_u *argstart,
3602 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603{
3604 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 do
3610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003612 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003613 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (lv.ll_name == NULL)
3615 error = TRUE; /* error but continue parsing */
3616 if (name_end == NULL || (!vim_iswhite(*name_end)
3617 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (name_end != NULL)
3620 {
3621 emsg_severe = TRUE;
3622 EMSG(_(e_trailing));
3623 }
3624 if (!(eap->skip || error))
3625 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 break;
3627 }
3628
3629 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 {
3631 if (eap->cmdidx == CMD_unlet)
3632 {
3633 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3634 error = TRUE;
3635 }
3636 else
3637 {
3638 if (do_lock_var(&lv, name_end, deep,
3639 eap->cmdidx == CMD_lockvar) == FAIL)
3640 error = TRUE;
3641 }
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (!eap->skip)
3645 clear_lval(&lv);
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 arg = skipwhite(name_end);
3648 } while (!ends_excmd(*arg));
3649
3650 eap->nextcmd = check_nextcmd(arg);
3651}
3652
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654do_unlet_var(
3655 lval_T *lp,
3656 char_u *name_end,
3657 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 int ret = OK;
3660 int cc;
3661
3662 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 cc = *name_end;
3665 *name_end = NUL;
3666
3667 /* Normal name or expanded name. */
3668 if (check_changedtick(lp->ll_name))
3669 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 else if (lp->ll_range)
3680 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003682 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003683 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003684
3685 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3686 {
3687 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 return FAIL;
3690 ll_li = li;
3691 ++ll_n1;
3692 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693
3694 /* Delete a range of List items. */
3695 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3696 {
3697 li = lp->ll_li->li_next;
3698 listitem_remove(lp->ll_list, lp->ll_li);
3699 lp->ll_li = li;
3700 ++lp->ll_n1;
3701 }
3702 }
3703 else
3704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 /* unlet a List item. */
3707 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003710 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 }
3712
3713 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714}
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716/*
3717 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
3720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003721do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 hashtab_T *ht;
3724 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003725 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003726 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003727 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 ht = find_var_ht(name, &varname);
3730 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003732 if (ht == &globvarht)
3733 d = &globvardict;
3734 else if (current_funccal != NULL
3735 && ht == &current_funccal->l_vars.dv_hashtab)
3736 d = &current_funccal->l_vars;
3737 else if (ht == &compat_hashtab)
3738 d = &vimvardict;
3739 else
3740 {
3741 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3742 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3743 }
3744 if (d == NULL)
3745 {
3746 EMSG2(_(e_intern2), "do_unlet()");
3747 return FAIL;
3748 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 hi = hash_find(ht, varname);
3750 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003751 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003752 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003753 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003754 || var_check_ro(di->di_flags, name, FALSE)
3755 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003756 return FAIL;
3757
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003758 delete_var(ht, hi);
3759 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762 if (forceit)
3763 return OK;
3764 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return FAIL;
3766}
3767
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768/*
3769 * Lock or unlock variable indicated by "lp".
3770 * "deep" is the levels to go (-1 for unlimited);
3771 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3772 */
3773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003774do_lock_var(
3775 lval_T *lp,
3776 char_u *name_end,
3777 int deep,
3778 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779{
3780 int ret = OK;
3781 int cc;
3782 dictitem_T *di;
3783
3784 if (deep == 0) /* nothing to do */
3785 return OK;
3786
3787 if (lp->ll_tv == NULL)
3788 {
3789 cc = *name_end;
3790 *name_end = NUL;
3791
3792 /* Normal name or expanded name. */
3793 if (check_changedtick(lp->ll_name))
3794 ret = FAIL;
3795 else
3796 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003797 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003798 if (di == NULL)
3799 ret = FAIL;
3800 else
3801 {
3802 if (lock)
3803 di->di_flags |= DI_FLAGS_LOCK;
3804 else
3805 di->di_flags &= ~DI_FLAGS_LOCK;
3806 item_lock(&di->di_tv, deep, lock);
3807 }
3808 }
3809 *name_end = cc;
3810 }
3811 else if (lp->ll_range)
3812 {
3813 listitem_T *li = lp->ll_li;
3814
3815 /* (un)lock a range of List items. */
3816 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3817 {
3818 item_lock(&li->li_tv, deep, lock);
3819 li = li->li_next;
3820 ++lp->ll_n1;
3821 }
3822 }
3823 else if (lp->ll_list != NULL)
3824 /* (un)lock a List item. */
3825 item_lock(&lp->ll_li->li_tv, deep, lock);
3826 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003827 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003828 item_lock(&lp->ll_di->di_tv, deep, lock);
3829
3830 return ret;
3831}
3832
3833/*
3834 * Lock or unlock an item. "deep" is nr of levels to go.
3835 */
3836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003837item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003838{
3839 static int recurse = 0;
3840 list_T *l;
3841 listitem_T *li;
3842 dict_T *d;
3843 hashitem_T *hi;
3844 int todo;
3845
3846 if (recurse >= DICT_MAXNEST)
3847 {
3848 EMSG(_("E743: variable nested too deep for (un)lock"));
3849 return;
3850 }
3851 if (deep == 0)
3852 return;
3853 ++recurse;
3854
3855 /* lock/unlock the item itself */
3856 if (lock)
3857 tv->v_lock |= VAR_LOCKED;
3858 else
3859 tv->v_lock &= ~VAR_LOCKED;
3860
3861 switch (tv->v_type)
3862 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003863 case VAR_UNKNOWN:
3864 case VAR_NUMBER:
3865 case VAR_STRING:
3866 case VAR_FUNC:
3867 case VAR_FLOAT:
3868 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003869 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003870 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003871 break;
3872
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003873 case VAR_LIST:
3874 if ((l = tv->vval.v_list) != NULL)
3875 {
3876 if (lock)
3877 l->lv_lock |= VAR_LOCKED;
3878 else
3879 l->lv_lock &= ~VAR_LOCKED;
3880 if (deep < 0 || deep > 1)
3881 /* recursive: lock/unlock the items the List contains */
3882 for (li = l->lv_first; li != NULL; li = li->li_next)
3883 item_lock(&li->li_tv, deep - 1, lock);
3884 }
3885 break;
3886 case VAR_DICT:
3887 if ((d = tv->vval.v_dict) != NULL)
3888 {
3889 if (lock)
3890 d->dv_lock |= VAR_LOCKED;
3891 else
3892 d->dv_lock &= ~VAR_LOCKED;
3893 if (deep < 0 || deep > 1)
3894 {
3895 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003896 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003897 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3898 {
3899 if (!HASHITEM_EMPTY(hi))
3900 {
3901 --todo;
3902 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3903 }
3904 }
3905 }
3906 }
3907 }
3908 --recurse;
3909}
3910
Bram Moolenaara40058a2005-07-11 22:42:07 +00003911/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003912 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3913 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003914 */
3915 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003916tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003917{
3918 return (tv->v_lock & VAR_LOCKED)
3919 || (tv->v_type == VAR_LIST
3920 && tv->vval.v_list != NULL
3921 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3922 || (tv->v_type == VAR_DICT
3923 && tv->vval.v_dict != NULL
3924 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3925}
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3928/*
3929 * Delete all "menutrans_" variables.
3930 */
3931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003932del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003935 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003938 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 {
3941 if (!HASHITEM_EMPTY(hi))
3942 {
3943 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3945 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 }
3947 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949}
3950#endif
3951
3952#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3953
3954/*
3955 * Local string buffer for the next two functions to store a variable name
3956 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3957 * get_user_var_name().
3958 */
3959
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003960static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962static char_u *varnamebuf = NULL;
3963static int varnamebuflen = 0;
3964
3965/*
3966 * Function to concatenate a prefix and a variable name.
3967 */
3968 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003969cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
3971 int len;
3972
3973 len = (int)STRLEN(name) + 3;
3974 if (len > varnamebuflen)
3975 {
3976 vim_free(varnamebuf);
3977 len += 10; /* some additional space */
3978 varnamebuf = alloc(len);
3979 if (varnamebuf == NULL)
3980 {
3981 varnamebuflen = 0;
3982 return NULL;
3983 }
3984 varnamebuflen = len;
3985 }
3986 *varnamebuf = prefix;
3987 varnamebuf[1] = ':';
3988 STRCPY(varnamebuf + 2, name);
3989 return varnamebuf;
3990}
3991
3992/*
3993 * Function given to ExpandGeneric() to obtain the list of user defined
3994 * (global/buffer/window/built-in) variable names.
3995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003997get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003999 static long_u gdone;
4000 static long_u bdone;
4001 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002#ifdef FEAT_WINDOWS
4003 static long_u tdone;
4004#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004005 static int vidx;
4006 static hashitem_T *hi;
4007 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012#ifdef FEAT_WINDOWS
4013 tdone = 0;
4014#endif
4015 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004016
4017 /* Global variables */
4018 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004020 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004022 else
4023 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 while (HASHITEM_EMPTY(hi))
4025 ++hi;
4026 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4027 return cat_prefix_varname('g', hi->hi_key);
4028 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004030
4031 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004032 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004035 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004037 else
4038 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 while (HASHITEM_EMPTY(hi))
4040 ++hi;
4041 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004045 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 return (char_u *)"b:changedtick";
4047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
4049 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004050 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004053 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004054 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004055 else
4056 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004057 while (HASHITEM_EMPTY(hi))
4058 ++hi;
4059 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004061
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004062#ifdef FEAT_WINDOWS
4063 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004064 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004065 if (tdone < ht->ht_used)
4066 {
4067 if (tdone++ == 0)
4068 hi = ht->ht_array;
4069 else
4070 ++hi;
4071 while (HASHITEM_EMPTY(hi))
4072 ++hi;
4073 return cat_prefix_varname('t', hi->hi_key);
4074 }
4075#endif
4076
Bram Moolenaar33570922005-01-25 22:26:29 +00004077 /* v: variables */
4078 if (vidx < VV_LEN)
4079 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 vim_free(varnamebuf);
4082 varnamebuf = NULL;
4083 varnamebuflen = 0;
4084 return NULL;
4085}
4086
4087#endif /* FEAT_CMDL_COMPL */
4088
4089/*
4090 * types for expressions.
4091 */
4092typedef enum
4093{
4094 TYPE_UNKNOWN = 0
4095 , TYPE_EQUAL /* == */
4096 , TYPE_NEQUAL /* != */
4097 , TYPE_GREATER /* > */
4098 , TYPE_GEQUAL /* >= */
4099 , TYPE_SMALLER /* < */
4100 , TYPE_SEQUAL /* <= */
4101 , TYPE_MATCH /* =~ */
4102 , TYPE_NOMATCH /* !~ */
4103} exptype_T;
4104
4105/*
4106 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4109 */
4110
4111/*
4112 * Handle zero level expression.
4113 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004115 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * Return OK or FAIL.
4117 */
4118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004119eval0(
4120 char_u *arg,
4121 typval_T *rettv,
4122 char_u **nextcmd,
4123 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
4125 int ret;
4126 char_u *p;
4127
4128 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (ret == FAIL || !ends_excmd(*p))
4131 {
4132 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /*
4135 * Report the invalid expression unless the expression evaluation has
4136 * been cancelled due to an aborting error, an interrupt, or an
4137 * exception.
4138 */
4139 if (!aborting())
4140 EMSG2(_(e_invexpr2), arg);
4141 ret = FAIL;
4142 }
4143 if (nextcmd != NULL)
4144 *nextcmd = check_nextcmd(p);
4145
4146 return ret;
4147}
4148
4149/*
4150 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004151 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 *
4153 * "arg" must point to the first non-white of the expression.
4154 * "arg" is advanced to the next non-white after the recognized expression.
4155 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004156 * Note: "rettv.v_lock" is not set.
4157 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * Return OK or FAIL.
4159 */
4160 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004161eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
4163 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /*
4167 * Get the first variable.
4168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return FAIL;
4171
4172 if ((*arg)[0] == '?')
4173 {
4174 result = FALSE;
4175 if (evaluate)
4176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 int error = FALSE;
4178
4179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192
4193 /*
4194 * Check for the ":".
4195 */
4196 if ((*arg)[0] != ':')
4197 {
4198 EMSG(_("E109: Missing ':' after '?'"));
4199 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202 }
4203
4204 /*
4205 * Get the third variable.
4206 */
4207 *arg = skipwhite(*arg + 1);
4208 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4209 {
4210 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213 }
4214 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217
4218 return OK;
4219}
4220
4221/*
4222 * Handle first level expression:
4223 * expr2 || expr2 || expr2 logical OR
4224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004231eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 long result;
4235 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 /*
4239 * Get the first variable.
4240 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return FAIL;
4243
4244 /*
4245 * Repeat until there is no following "||".
4246 */
4247 first = TRUE;
4248 result = FALSE;
4249 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4250 {
4251 if (evaluate && first)
4252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (error)
4257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 first = FALSE;
4259 }
4260
4261 /*
4262 * Get the second variable.
4263 */
4264 *arg = skipwhite(*arg + 2);
4265 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4266 return FAIL;
4267
4268 /*
4269 * Compute the result.
4270 */
4271 if (evaluate && !result)
4272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (error)
4277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 if (evaluate)
4280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 rettv->v_type = VAR_NUMBER;
4282 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 }
4285
4286 return OK;
4287}
4288
4289/*
4290 * Handle second level expression:
4291 * expr3 && expr3 && expr3 logical AND
4292 *
4293 * "arg" must point to the first non-white of the expression.
4294 * "arg" is advanced to the next non-white after the recognized expression.
4295 *
4296 * Return OK or FAIL.
4297 */
4298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004299eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 long result;
4303 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 /*
4313 * Repeat until there is no following "&&".
4314 */
4315 first = TRUE;
4316 result = TRUE;
4317 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4318 {
4319 if (evaluate && first)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (error)
4325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 first = FALSE;
4327 }
4328
4329 /*
4330 * Get the second variable.
4331 */
4332 *arg = skipwhite(*arg + 2);
4333 if (eval4(arg, &var2, evaluate && result) == FAIL)
4334 return FAIL;
4335
4336 /*
4337 * Compute the result.
4338 */
4339 if (evaluate && result)
4340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 if (error)
4345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 if (evaluate)
4348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 rettv->v_type = VAR_NUMBER;
4350 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 }
4353
4354 return OK;
4355}
4356
4357/*
4358 * Handle third level expression:
4359 * var1 == var2
4360 * var1 =~ var2
4361 * var1 != var2
4362 * var1 !~ var2
4363 * var1 > var2
4364 * var1 >= var2
4365 * var1 < var2
4366 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 * var1 is var2
4368 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 *
4370 * "arg" must point to the first non-white of the expression.
4371 * "arg" is advanced to the next non-white after the recognized expression.
4372 *
4373 * Return OK or FAIL.
4374 */
4375 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004376eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
Bram Moolenaar33570922005-01-25 22:26:29 +00004378 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 char_u *p;
4380 int i;
4381 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 int len = 2;
4384 long n1, n2;
4385 char_u *s1, *s2;
4386 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4387 regmatch_T regmatch;
4388 int ic;
4389 char_u *save_cpo;
4390
4391 /*
4392 * Get the first variable.
4393 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 return FAIL;
4396
4397 p = *arg;
4398 switch (p[0])
4399 {
4400 case '=': if (p[1] == '=')
4401 type = TYPE_EQUAL;
4402 else if (p[1] == '~')
4403 type = TYPE_MATCH;
4404 break;
4405 case '!': if (p[1] == '=')
4406 type = TYPE_NEQUAL;
4407 else if (p[1] == '~')
4408 type = TYPE_NOMATCH;
4409 break;
4410 case '>': if (p[1] != '=')
4411 {
4412 type = TYPE_GREATER;
4413 len = 1;
4414 }
4415 else
4416 type = TYPE_GEQUAL;
4417 break;
4418 case '<': if (p[1] != '=')
4419 {
4420 type = TYPE_SMALLER;
4421 len = 1;
4422 }
4423 else
4424 type = TYPE_SEQUAL;
4425 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 case 'i': if (p[1] == 's')
4427 {
4428 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4429 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004430 i = p[len];
4431 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 {
4433 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4434 type_is = TRUE;
4435 }
4436 }
4437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439
4440 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004441 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 */
4443 if (type != TYPE_UNKNOWN)
4444 {
4445 /* extra question mark appended: ignore case */
4446 if (p[len] == '?')
4447 {
4448 ic = TRUE;
4449 ++len;
4450 }
4451 /* extra '#' appended: match case */
4452 else if (p[len] == '#')
4453 {
4454 ic = FALSE;
4455 ++len;
4456 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004457 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 else
4459 ic = p_ic;
4460
4461 /*
4462 * Get the second variable.
4463 */
4464 *arg = skipwhite(p + len);
4465 if (eval5(arg, &var2, evaluate) == FAIL)
4466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 return FAIL;
4469 }
4470
4471 if (evaluate)
4472 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004473 if (type_is && rettv->v_type != var2.v_type)
4474 {
4475 /* For "is" a different type always means FALSE, for "notis"
4476 * it means TRUE. */
4477 n1 = (type == TYPE_NEQUAL);
4478 }
4479 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4480 {
4481 if (type_is)
4482 {
4483 n1 = (rettv->v_type == var2.v_type
4484 && rettv->vval.v_list == var2.vval.v_list);
4485 if (type == TYPE_NEQUAL)
4486 n1 = !n1;
4487 }
4488 else if (rettv->v_type != var2.v_type
4489 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4490 {
4491 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004492 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004494 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 clear_tv(rettv);
4496 clear_tv(&var2);
4497 return FAIL;
4498 }
4499 else
4500 {
4501 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004502 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4503 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004504 if (type == TYPE_NEQUAL)
4505 n1 = !n1;
4506 }
4507 }
4508
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004509 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4510 {
4511 if (type_is)
4512 {
4513 n1 = (rettv->v_type == var2.v_type
4514 && rettv->vval.v_dict == var2.vval.v_dict);
4515 if (type == TYPE_NEQUAL)
4516 n1 = !n1;
4517 }
4518 else if (rettv->v_type != var2.v_type
4519 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4520 {
4521 if (rettv->v_type != var2.v_type)
4522 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4523 else
4524 EMSG(_("E736: Invalid operation for Dictionary"));
4525 clear_tv(rettv);
4526 clear_tv(&var2);
4527 return FAIL;
4528 }
4529 else
4530 {
4531 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004532 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4533 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004534 if (type == TYPE_NEQUAL)
4535 n1 = !n1;
4536 }
4537 }
4538
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004539 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4540 {
4541 if (rettv->v_type != var2.v_type
4542 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4543 {
4544 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004545 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004547 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 clear_tv(rettv);
4549 clear_tv(&var2);
4550 return FAIL;
4551 }
4552 else
4553 {
4554 /* Compare two Funcrefs for being equal or unequal. */
4555 if (rettv->vval.v_string == NULL
4556 || var2.vval.v_string == NULL)
4557 n1 = FALSE;
4558 else
4559 n1 = STRCMP(rettv->vval.v_string,
4560 var2.vval.v_string) == 0;
4561 if (type == TYPE_NEQUAL)
4562 n1 = !n1;
4563 }
4564 }
4565
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004566#ifdef FEAT_FLOAT
4567 /*
4568 * If one of the two variables is a float, compare as a float.
4569 * When using "=~" or "!~", always compare as string.
4570 */
4571 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4572 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4573 {
4574 float_T f1, f2;
4575
4576 if (rettv->v_type == VAR_FLOAT)
4577 f1 = rettv->vval.v_float;
4578 else
4579 f1 = get_tv_number(rettv);
4580 if (var2.v_type == VAR_FLOAT)
4581 f2 = var2.vval.v_float;
4582 else
4583 f2 = get_tv_number(&var2);
4584 n1 = FALSE;
4585 switch (type)
4586 {
4587 case TYPE_EQUAL: n1 = (f1 == f2); break;
4588 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4589 case TYPE_GREATER: n1 = (f1 > f2); break;
4590 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4591 case TYPE_SMALLER: n1 = (f1 < f2); break;
4592 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4593 case TYPE_UNKNOWN:
4594 case TYPE_MATCH:
4595 case TYPE_NOMATCH: break; /* avoid gcc warning */
4596 }
4597 }
4598#endif
4599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 /*
4601 * If one of the two variables is a number, compare as a number.
4602 * When using "=~" or "!~", always compare as string.
4603 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004604 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607 n1 = get_tv_number(rettv);
4608 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 switch (type)
4610 {
4611 case TYPE_EQUAL: n1 = (n1 == n2); break;
4612 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4613 case TYPE_GREATER: n1 = (n1 > n2); break;
4614 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4615 case TYPE_SMALLER: n1 = (n1 < n2); break;
4616 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4617 case TYPE_UNKNOWN:
4618 case TYPE_MATCH:
4619 case TYPE_NOMATCH: break; /* avoid gcc warning */
4620 }
4621 }
4622 else
4623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624 s1 = get_tv_string_buf(rettv, buf1);
4625 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4627 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4628 else
4629 i = 0;
4630 n1 = FALSE;
4631 switch (type)
4632 {
4633 case TYPE_EQUAL: n1 = (i == 0); break;
4634 case TYPE_NEQUAL: n1 = (i != 0); break;
4635 case TYPE_GREATER: n1 = (i > 0); break;
4636 case TYPE_GEQUAL: n1 = (i >= 0); break;
4637 case TYPE_SMALLER: n1 = (i < 0); break;
4638 case TYPE_SEQUAL: n1 = (i <= 0); break;
4639
4640 case TYPE_MATCH:
4641 case TYPE_NOMATCH:
4642 /* avoid 'l' flag in 'cpoptions' */
4643 save_cpo = p_cpo;
4644 p_cpo = (char_u *)"";
4645 regmatch.regprog = vim_regcomp(s2,
4646 RE_MAGIC + RE_STRING);
4647 regmatch.rm_ic = ic;
4648 if (regmatch.regprog != NULL)
4649 {
4650 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004651 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (type == TYPE_NOMATCH)
4653 n1 = !n1;
4654 }
4655 p_cpo = save_cpo;
4656 break;
4657
4658 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4659 }
4660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 clear_tv(rettv);
4662 clear_tv(&var2);
4663 rettv->v_type = VAR_NUMBER;
4664 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
4666 }
4667
4668 return OK;
4669}
4670
4671/*
4672 * Handle fourth level expression:
4673 * + number addition
4674 * - number subtraction
4675 * . string concatenation
4676 *
4677 * "arg" must point to the first non-white of the expression.
4678 * "arg" is advanced to the next non-white after the recognized expression.
4679 *
4680 * Return OK or FAIL.
4681 */
4682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004683eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
Bram Moolenaar33570922005-01-25 22:26:29 +00004685 typval_T var2;
4686 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 int op;
4688 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689#ifdef FEAT_FLOAT
4690 float_T f1 = 0, f2 = 0;
4691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 char_u *s1, *s2;
4693 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4694 char_u *p;
4695
4696 /*
4697 * Get the first variable.
4698 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004699 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 return FAIL;
4701
4702 /*
4703 * Repeat computing, until no '+', '-' or '.' is following.
4704 */
4705 for (;;)
4706 {
4707 op = **arg;
4708 if (op != '+' && op != '-' && op != '.')
4709 break;
4710
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 if ((op != '+' || rettv->v_type != VAR_LIST)
4712#ifdef FEAT_FLOAT
4713 && (op == '.' || rettv->v_type != VAR_FLOAT)
4714#endif
4715 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 {
4717 /* For "list + ...", an illegal use of the first operand as
4718 * a number cannot be determined before evaluating the 2nd
4719 * operand: if this is also a list, all is ok.
4720 * For "something . ...", "something - ..." or "non-list + ...",
4721 * we know that the first operand needs to be a string or number
4722 * without evaluating the 2nd operand. So check before to avoid
4723 * side effects after an error. */
4724 if (evaluate && get_tv_string_chk(rettv) == NULL)
4725 {
4726 clear_tv(rettv);
4727 return FAIL;
4728 }
4729 }
4730
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 /*
4732 * Get the second variable.
4733 */
4734 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004735 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 return FAIL;
4739 }
4740
4741 if (evaluate)
4742 {
4743 /*
4744 * Compute the result.
4745 */
4746 if (op == '.')
4747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4749 s2 = get_tv_string_buf_chk(&var2, buf2);
4750 if (s2 == NULL) /* type error ? */
4751 {
4752 clear_tv(rettv);
4753 clear_tv(&var2);
4754 return FAIL;
4755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004756 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 clear_tv(rettv);
4758 rettv->v_type = VAR_STRING;
4759 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004761 else if (op == '+' && rettv->v_type == VAR_LIST
4762 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004763 {
4764 /* concatenate Lists */
4765 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4766 &var3) == FAIL)
4767 {
4768 clear_tv(rettv);
4769 clear_tv(&var2);
4770 return FAIL;
4771 }
4772 clear_tv(rettv);
4773 *rettv = var3;
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 else
4776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 int error = FALSE;
4778
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004779#ifdef FEAT_FLOAT
4780 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004782 f1 = rettv->vval.v_float;
4783 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785 else
4786#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 n1 = get_tv_number_chk(rettv, &error);
4789 if (error)
4790 {
4791 /* This can only happen for "list + non-list". For
4792 * "non-list + ..." or "something - ...", we returned
4793 * before evaluating the 2nd operand. */
4794 clear_tv(rettv);
4795 return FAIL;
4796 }
4797#ifdef FEAT_FLOAT
4798 if (var2.v_type == VAR_FLOAT)
4799 f1 = n1;
4800#endif
4801 }
4802#ifdef FEAT_FLOAT
4803 if (var2.v_type == VAR_FLOAT)
4804 {
4805 f2 = var2.vval.v_float;
4806 n2 = 0;
4807 }
4808 else
4809#endif
4810 {
4811 n2 = get_tv_number_chk(&var2, &error);
4812 if (error)
4813 {
4814 clear_tv(rettv);
4815 clear_tv(&var2);
4816 return FAIL;
4817 }
4818#ifdef FEAT_FLOAT
4819 if (rettv->v_type == VAR_FLOAT)
4820 f2 = n2;
4821#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824
4825#ifdef FEAT_FLOAT
4826 /* If there is a float on either side the result is a float. */
4827 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4828 {
4829 if (op == '+')
4830 f1 = f1 + f2;
4831 else
4832 f1 = f1 - f2;
4833 rettv->v_type = VAR_FLOAT;
4834 rettv->vval.v_float = f1;
4835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#endif
4838 {
4839 if (op == '+')
4840 n1 = n1 + n2;
4841 else
4842 n1 = n1 - n2;
4843 rettv->v_type = VAR_NUMBER;
4844 rettv->vval.v_number = n1;
4845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 }
4850 return OK;
4851}
4852
4853/*
4854 * Handle fifth level expression:
4855 * * number multiplication
4856 * / number division
4857 * % number modulo
4858 *
4859 * "arg" must point to the first non-white of the expression.
4860 * "arg" is advanced to the next non-white after the recognized expression.
4861 *
4862 * Return OK or FAIL.
4863 */
4864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004865eval6(
4866 char_u **arg,
4867 typval_T *rettv,
4868 int evaluate,
4869 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 int op;
4873 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 int use_float = FALSE;
4876 float_T f1 = 0, f2;
4877#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /*
4881 * Get the first variable.
4882 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004883 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 return FAIL;
4885
4886 /*
4887 * Repeat computing, until no '*', '/' or '%' is following.
4888 */
4889 for (;;)
4890 {
4891 op = **arg;
4892 if (op != '*' && op != '/' && op != '%')
4893 break;
4894
4895 if (evaluate)
4896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897#ifdef FEAT_FLOAT
4898 if (rettv->v_type == VAR_FLOAT)
4899 {
4900 f1 = rettv->vval.v_float;
4901 use_float = TRUE;
4902 n1 = 0;
4903 }
4904 else
4905#endif
4906 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004907 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004908 if (error)
4909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 else
4912 n1 = 0;
4913
4914 /*
4915 * Get the second variable.
4916 */
4917 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004918 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 return FAIL;
4920
4921 if (evaluate)
4922 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923#ifdef FEAT_FLOAT
4924 if (var2.v_type == VAR_FLOAT)
4925 {
4926 if (!use_float)
4927 {
4928 f1 = n1;
4929 use_float = TRUE;
4930 }
4931 f2 = var2.vval.v_float;
4932 n2 = 0;
4933 }
4934 else
4935#endif
4936 {
4937 n2 = get_tv_number_chk(&var2, &error);
4938 clear_tv(&var2);
4939 if (error)
4940 return FAIL;
4941#ifdef FEAT_FLOAT
4942 if (use_float)
4943 f2 = n2;
4944#endif
4945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
4947 /*
4948 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004951#ifdef FEAT_FLOAT
4952 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954 if (op == '*')
4955 f1 = f1 * f2;
4956 else if (op == '/')
4957 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004958# ifdef VMS
4959 /* VMS crashes on divide by zero, work around it */
4960 if (f2 == 0.0)
4961 {
4962 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004963 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004965 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 }
4969 else
4970 f1 = f1 / f2;
4971# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 /* We rely on the floating point library to handle divide
4973 * by zero to result in "inf" and not a crash. */
4974 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004979 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 return FAIL;
4981 }
4982 rettv->v_type = VAR_FLOAT;
4983 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 if (op == '*')
4989 n1 = n1 * n2;
4990 else if (op == '/')
4991 {
4992 if (n2 == 0) /* give an error message? */
4993 {
4994 if (n1 == 0)
4995 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4996 else if (n1 < 0)
4997 n1 = -0x7fffffffL;
4998 else
4999 n1 = 0x7fffffffL;
5000 }
5001 else
5002 n1 = n1 / n2;
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 {
5006 if (n2 == 0) /* give an error message? */
5007 n1 = 0;
5008 else
5009 n1 = n1 % n2;
5010 }
5011 rettv->v_type = VAR_NUMBER;
5012 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015 }
5016
5017 return OK;
5018}
5019
5020/*
5021 * Handle sixth level expression:
5022 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005023 * "string" string constant
5024 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 * &option-name option value
5026 * @r register contents
5027 * identifier variable value
5028 * function() function call
5029 * $VAR environment variable
5030 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005031 * [expr, expr] List
5032 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 *
5034 * Also handle:
5035 * ! in front logical NOT
5036 * - in front unary minus
5037 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 * trailing [] subscript in String or List
5039 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * "arg" must point to the first non-white of the expression.
5042 * "arg" is advanced to the next non-white after the recognized expression.
5043 *
5044 * Return OK or FAIL.
5045 */
5046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005047eval7(
5048 char_u **arg,
5049 typval_T *rettv,
5050 int evaluate,
5051 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 long n;
5054 int len;
5055 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 char_u *start_leader, *end_leader;
5057 int ret = OK;
5058 char_u *alias;
5059
5060 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005062 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
5066 /*
5067 * Skip '!' and '-' characters. They are handled later.
5068 */
5069 start_leader = *arg;
5070 while (**arg == '!' || **arg == '-' || **arg == '+')
5071 *arg = skipwhite(*arg + 1);
5072 end_leader = *arg;
5073
5074 switch (**arg)
5075 {
5076 /*
5077 * Number constant.
5078 */
5079 case '0':
5080 case '1':
5081 case '2':
5082 case '3':
5083 case '4':
5084 case '5':
5085 case '6':
5086 case '7':
5087 case '8':
5088 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005089 {
5090#ifdef FEAT_FLOAT
5091 char_u *p = skipdigits(*arg + 1);
5092 int get_float = FALSE;
5093
5094 /* We accept a float when the format matches
5095 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005096 * strict to avoid backwards compatibility problems.
5097 * Don't look for a float after the "." operator, so that
5098 * ":let vers = 1.2.3" doesn't fail. */
5099 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005101 get_float = TRUE;
5102 p = skipdigits(p + 2);
5103 if (*p == 'e' || *p == 'E')
5104 {
5105 ++p;
5106 if (*p == '-' || *p == '+')
5107 ++p;
5108 if (!vim_isdigit(*p))
5109 get_float = FALSE;
5110 else
5111 p = skipdigits(p + 1);
5112 }
5113 if (ASCII_ISALPHA(*p) || *p == '.')
5114 get_float = FALSE;
5115 }
5116 if (get_float)
5117 {
5118 float_T f;
5119
5120 *arg += string2float(*arg, &f);
5121 if (evaluate)
5122 {
5123 rettv->v_type = VAR_FLOAT;
5124 rettv->vval.v_float = f;
5125 }
5126 }
5127 else
5128#endif
5129 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005130 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005131 *arg += len;
5132 if (evaluate)
5133 {
5134 rettv->v_type = VAR_NUMBER;
5135 rettv->vval.v_number = n;
5136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140
5141 /*
5142 * String constant: "string".
5143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146
5147 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 break;
5152
5153 /*
5154 * List: [expr, expr]
5155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158
5159 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 * Dictionary: {key: val, key: val}
5161 */
5162 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5163 break;
5164
5165 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005168 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 break;
5170
5171 /*
5172 * Environment variable: $VAR.
5173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 /*
5178 * Register contents: @r.
5179 */
5180 case '@': ++*arg;
5181 if (evaluate)
5182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005184 rettv->vval.v_string = get_reg_contents(**arg,
5185 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187 if (**arg != NUL)
5188 ++*arg;
5189 break;
5190
5191 /*
5192 * nested expression: (expression).
5193 */
5194 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (**arg == ')')
5197 ++*arg;
5198 else if (ret == OK)
5199 {
5200 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 ret = FAIL;
5203 }
5204 break;
5205
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
5208 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209
5210 if (ret == NOTDONE)
5211 {
5212 /*
5213 * Must be a variable or function name.
5214 * Can also be a curly-braces kind of name: {expr}.
5215 */
5216 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005217 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 if (alias != NULL)
5219 s = alias;
5220
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 ret = FAIL;
5223 else
5224 {
5225 if (**arg == '(') /* recursive! */
5226 {
5227 /* If "s" is the name of a variable of type VAR_FUNC
5228 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005229 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230
5231 /* Invoke the function. */
5232 ret = get_func_tv(s, len, rettv, arg,
5233 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005234 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005235
5236 /* If evaluate is FALSE rettv->v_type was not set in
5237 * get_func_tv, but it's needed in handle_subscript() to parse
5238 * what follows. So set it here. */
5239 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5240 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005241 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242 rettv->v_type = VAR_FUNC;
5243 }
5244
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 /* Stop the expression evaluation when immediately
5246 * aborting on error, or when an interrupt occurred or
5247 * an exception was thrown but not caught. */
5248 if (aborting())
5249 {
5250 if (ret == OK)
5251 clear_tv(rettv);
5252 ret = FAIL;
5253 }
5254 }
5255 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005256 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005257 else
5258 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005260 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 }
5262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 *arg = skipwhite(*arg);
5264
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5266 * expr(expr). */
5267 if (ret == OK)
5268 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 /*
5271 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5272 */
5273 if (ret == OK && evaluate && end_leader > start_leader)
5274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005275 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005276 int val = 0;
5277#ifdef FEAT_FLOAT
5278 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005280 if (rettv->v_type == VAR_FLOAT)
5281 f = rettv->vval.v_float;
5282 else
5283#endif
5284 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287 clear_tv(rettv);
5288 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290 else
5291 {
5292 while (end_leader > start_leader)
5293 {
5294 --end_leader;
5295 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005296 {
5297#ifdef FEAT_FLOAT
5298 if (rettv->v_type == VAR_FLOAT)
5299 f = !f;
5300 else
5301#endif
5302 val = !val;
5303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005305 {
5306#ifdef FEAT_FLOAT
5307 if (rettv->v_type == VAR_FLOAT)
5308 f = -f;
5309 else
5310#endif
5311 val = -val;
5312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005314#ifdef FEAT_FLOAT
5315 if (rettv->v_type == VAR_FLOAT)
5316 {
5317 clear_tv(rettv);
5318 rettv->vval.v_float = f;
5319 }
5320 else
5321#endif
5322 {
5323 clear_tv(rettv);
5324 rettv->v_type = VAR_NUMBER;
5325 rettv->vval.v_number = val;
5326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329
5330 return ret;
5331}
5332
5333/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005334 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5335 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5337 */
5338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005339eval_index(
5340 char_u **arg,
5341 typval_T *rettv,
5342 int evaluate,
5343 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344{
5345 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005346 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 long len = -1;
5349 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352
Bram Moolenaara03f2332016-02-06 18:09:59 +01005353 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005355 case VAR_FUNC:
5356 if (verbose)
5357 EMSG(_("E695: Cannot index a Funcref"));
5358 return FAIL;
5359 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005360#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 if (verbose)
5362 EMSG(_(e_float_as_string));
5363 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005364#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005365 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005366 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005367 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 if (verbose)
5369 EMSG(_("E909: Cannot index a special variable"));
5370 return FAIL;
5371 case VAR_UNKNOWN:
5372 if (evaluate)
5373 return FAIL;
5374 /* FALLTHROUGH */
5375
5376 case VAR_STRING:
5377 case VAR_NUMBER:
5378 case VAR_LIST:
5379 case VAR_DICT:
5380 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005381 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005383 init_tv(&var1);
5384 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 /*
5388 * dict.name
5389 */
5390 key = *arg + 1;
5391 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5392 ;
5393 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 /*
5400 * something[idx]
5401 *
5402 * Get the (first) variable from inside the [].
5403 */
5404 *arg = skipwhite(*arg + 1);
5405 if (**arg == ':')
5406 empty1 = TRUE;
5407 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5408 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005409 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5410 {
5411 /* not a number or string */
5412 clear_tv(&var1);
5413 return FAIL;
5414 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415
5416 /*
5417 * Get the second variable from inside the [:].
5418 */
5419 if (**arg == ':')
5420 {
5421 range = TRUE;
5422 *arg = skipwhite(*arg + 1);
5423 if (**arg == ']')
5424 empty2 = TRUE;
5425 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005427 if (!empty1)
5428 clear_tv(&var1);
5429 return FAIL;
5430 }
5431 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5432 {
5433 /* not a number or string */
5434 if (!empty1)
5435 clear_tv(&var1);
5436 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 return FAIL;
5438 }
5439 }
5440
5441 /* Check for the ']'. */
5442 if (**arg != ']')
5443 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005444 if (verbose)
5445 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 clear_tv(&var1);
5447 if (range)
5448 clear_tv(&var2);
5449 return FAIL;
5450 }
5451 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
5453
5454 if (evaluate)
5455 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 n1 = 0;
5457 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 n1 = get_tv_number(&var1);
5460 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 if (range)
5463 {
5464 if (empty2)
5465 n2 = -1;
5466 else
5467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 n2 = get_tv_number(&var2);
5469 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 }
5471 }
5472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005475 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005476 case VAR_FUNC:
5477 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005478 case VAR_SPECIAL:
5479 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005480 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005481 break; /* not evaluating, skipping over subscript */
5482
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 case VAR_NUMBER:
5484 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 len = (long)STRLEN(s);
5487 if (range)
5488 {
5489 /* The resulting variable is a substring. If the indexes
5490 * are out of range the result is empty. */
5491 if (n1 < 0)
5492 {
5493 n1 = len + n1;
5494 if (n1 < 0)
5495 n1 = 0;
5496 }
5497 if (n2 < 0)
5498 n2 = len + n2;
5499 else if (n2 >= len)
5500 n2 = len;
5501 if (n1 >= len || n2 < 0 || n1 > n2)
5502 s = NULL;
5503 else
5504 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5505 }
5506 else
5507 {
5508 /* The resulting variable is a string of a single
5509 * character. If the index is too big or negative the
5510 * result is empty. */
5511 if (n1 >= len || n1 < 0)
5512 s = NULL;
5513 else
5514 s = vim_strnsave(s + n1, 1);
5515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 clear_tv(rettv);
5517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 break;
5520
5521 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 if (n1 < 0)
5524 n1 = len + n1;
5525 if (!empty1 && (n1 < 0 || n1 >= len))
5526 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005527 /* For a range we allow invalid values and return an empty
5528 * list. A list index out of range is an error. */
5529 if (!range)
5530 {
5531 if (verbose)
5532 EMSGN(_(e_listidx), n1);
5533 return FAIL;
5534 }
5535 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 if (range)
5538 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 list_T *l;
5540 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541
5542 if (n2 < 0)
5543 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005544 else if (n2 >= len)
5545 n2 = len - 1;
5546 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 l = list_alloc();
5549 if (l == NULL)
5550 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 n1 <= n2; ++n1)
5553 {
5554 if (list_append_tv(l, &item->li_tv) == FAIL)
5555 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005556 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return FAIL;
5558 }
5559 item = item->li_next;
5560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 clear_tv(rettv);
5562 rettv->v_type = VAR_LIST;
5563 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005564 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 else
5567 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005568 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 }
5572 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573
5574 case VAR_DICT:
5575 if (range)
5576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005577 if (verbose)
5578 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 if (len == -1)
5580 clear_tv(&var1);
5581 return FAIL;
5582 }
5583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585
5586 if (len == -1)
5587 {
5588 key = get_tv_string(&var1);
5589 if (*key == NUL)
5590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (verbose)
5592 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 clear_tv(&var1);
5594 return FAIL;
5595 }
5596 }
5597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005598 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005601 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 if (len == -1)
5603 clear_tv(&var1);
5604 if (item == NULL)
5605 return FAIL;
5606
5607 copy_tv(&item->di_tv, &var1);
5608 clear_tv(rettv);
5609 *rettv = var1;
5610 }
5611 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 }
5614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 return OK;
5616}
5617
5618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * Get an option value.
5620 * "arg" points to the '&' or '+' before the option name.
5621 * "arg" is advanced to character after the option name.
5622 * Return OK or FAIL.
5623 */
5624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005625get_option_tv(
5626 char_u **arg,
5627 typval_T *rettv, /* when NULL, only check if option exists */
5628 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 char_u *option_end;
5631 long numval;
5632 char_u *stringval;
5633 int opt_type;
5634 int c;
5635 int working = (**arg == '+'); /* has("+option") */
5636 int ret = OK;
5637 int opt_flags;
5638
5639 /*
5640 * Isolate the option name and find its value.
5641 */
5642 option_end = find_option_end(arg, &opt_flags);
5643 if (option_end == NULL)
5644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005645 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 EMSG2(_("E112: Option name missing: %s"), *arg);
5647 return FAIL;
5648 }
5649
5650 if (!evaluate)
5651 {
5652 *arg = option_end;
5653 return OK;
5654 }
5655
5656 c = *option_end;
5657 *option_end = NUL;
5658 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 if (opt_type == -3) /* invalid name */
5662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 EMSG2(_("E113: Unknown option: %s"), *arg);
5665 ret = FAIL;
5666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 if (opt_type == -2) /* hidden string option */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 rettv->v_type = VAR_STRING;
5672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else if (opt_type == -1) /* hidden number option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_NUMBER;
5677 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else if (opt_type == 1) /* number option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_NUMBER;
5682 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else /* string option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 }
5690 else if (working && (opt_type == -2 || opt_type == -1))
5691 ret = FAIL;
5692
5693 *option_end = c; /* put back for error messages */
5694 *arg = option_end;
5695
5696 return ret;
5697}
5698
5699/*
5700 * Allocate a variable for a string constant.
5701 * Return OK or FAIL.
5702 */
5703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005704get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
5706 char_u *p;
5707 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 int extra = 0;
5709
5710 /*
5711 * Find the end of the string, skipping backslashed characters.
5712 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
5715 if (*p == '\\' && p[1] != NUL)
5716 {
5717 ++p;
5718 /* A "\<x>" form occupies at least 4 characters, and produces up
5719 * to 6 characters: reserve space for 2 extra */
5720 if (*p == '<')
5721 extra += 2;
5722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724
5725 if (*p != '"')
5726 {
5727 EMSG2(_("E114: Missing quote: %s"), *arg);
5728 return FAIL;
5729 }
5730
5731 /* If only parsing, set *arg and return here */
5732 if (!evaluate)
5733 {
5734 *arg = p + 1;
5735 return OK;
5736 }
5737
5738 /*
5739 * Copy the string into allocated memory, handling backslashed
5740 * characters.
5741 */
5742 name = alloc((unsigned)(p - *arg + extra));
5743 if (name == NULL)
5744 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 rettv->v_type = VAR_STRING;
5746 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 {
5750 if (*p == '\\')
5751 {
5752 switch (*++p)
5753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 case 'b': *name++ = BS; ++p; break;
5755 case 'e': *name++ = ESC; ++p; break;
5756 case 'f': *name++ = FF; ++p; break;
5757 case 'n': *name++ = NL; ++p; break;
5758 case 'r': *name++ = CAR; ++p; break;
5759 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 case 'X': /* hex: "\x1", "\x12" */
5762 case 'x':
5763 case 'u': /* Unicode: "\u0023" */
5764 case 'U':
5765 if (vim_isxdigit(p[1]))
5766 {
5767 int n, nr;
5768 int c = toupper(*p);
5769
5770 if (c == 'X')
5771 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005772 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005774 else
5775 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 nr = 0;
5777 while (--n >= 0 && vim_isxdigit(p[1]))
5778 {
5779 ++p;
5780 nr = (nr << 4) + hex2nr(*p);
5781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783#ifdef FEAT_MBYTE
5784 /* For "\u" store the number according to
5785 * 'encoding'. */
5786 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 else
5789#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 break;
5793
5794 /* octal: "\1", "\12", "\123" */
5795 case '0':
5796 case '1':
5797 case '2':
5798 case '3':
5799 case '4':
5800 case '5':
5801 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 case '7': *name = *p++ - '0';
5803 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 *name = (*name << 3) + *p++ - '0';
5806 if (*p >= '0' && *p <= '7')
5807 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 break;
5811
5812 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 if (extra != 0)
5815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818 }
5819 /* FALLTHROUGH */
5820
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 break;
5823 }
5824 }
5825 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 *arg = p + 1;
5831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 return OK;
5833}
5834
5835/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 * Return OK or FAIL.
5838 */
5839 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005840get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 int reduce = 0;
5845
5846 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 */
5849 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 break;
5855 ++reduce;
5856 ++p;
5857 }
5858 }
5859
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 return FAIL;
5864 }
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 if (!evaluate)
5868 {
5869 *arg = p + 1;
5870 return OK;
5871 }
5872
5873 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 */
5876 str = alloc((unsigned)((p - *arg) - reduce));
5877 if (str == NULL)
5878 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 rettv->v_type = VAR_STRING;
5880 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 break;
5888 ++p;
5889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 *arg = p + 1;
5894
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 return OK;
5896}
5897
5898/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 * Allocate a variable for a List and fill it from "*arg".
5900 * Return OK or FAIL.
5901 */
5902 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005903get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904{
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *l = NULL;
5906 typval_T tv;
5907 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908
5909 if (evaluate)
5910 {
5911 l = list_alloc();
5912 if (l == NULL)
5913 return FAIL;
5914 }
5915
5916 *arg = skipwhite(*arg + 1);
5917 while (**arg != ']' && **arg != NUL)
5918 {
5919 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5920 goto failret;
5921 if (evaluate)
5922 {
5923 item = listitem_alloc();
5924 if (item != NULL)
5925 {
5926 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005927 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 list_append(l, item);
5929 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005930 else
5931 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 }
5933
5934 if (**arg == ']')
5935 break;
5936 if (**arg != ',')
5937 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 goto failret;
5940 }
5941 *arg = skipwhite(*arg + 1);
5942 }
5943
5944 if (**arg != ']')
5945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947failret:
5948 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005949 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 return FAIL;
5951 }
5952
5953 *arg = skipwhite(*arg + 1);
5954 if (evaluate)
5955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005956 rettv->v_type = VAR_LIST;
5957 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 ++l->lv_refcount;
5959 }
5960
5961 return OK;
5962}
5963
5964/*
5965 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005966 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005968 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005971 list_T *l;
5972
5973 l = (list_T *)alloc_clear(sizeof(list_T));
5974 if (l != NULL)
5975 {
5976 /* Prepend the list to the list of lists for garbage collection. */
5977 if (first_list != NULL)
5978 first_list->lv_used_prev = l;
5979 l->lv_used_prev = NULL;
5980 l->lv_used_next = first_list;
5981 first_list = l;
5982 }
5983 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984}
5985
5986/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005987 * Allocate an empty list for a return value.
5988 * Returns OK or FAIL.
5989 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005990 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005992{
5993 list_T *l = list_alloc();
5994
5995 if (l == NULL)
5996 return FAIL;
5997
5998 rettv->vval.v_list = l;
5999 rettv->v_type = VAR_LIST;
6000 ++l->lv_refcount;
6001 return OK;
6002}
6003
6004/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 * Unreference a list: decrement the reference count and free it when it
6006 * becomes zero.
6007 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006009list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006011 if (l != NULL && --l->lv_refcount <= 0)
6012 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013}
6014
6015/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006016 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 * Ignores the reference count.
6018 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006020list_free(
6021 list_T *l,
6022 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 /* Remove the list from the list of lists for garbage collection. */
6027 if (l->lv_used_prev == NULL)
6028 first_list = l->lv_used_next;
6029 else
6030 l->lv_used_prev->lv_used_next = l->lv_used_next;
6031 if (l->lv_used_next != NULL)
6032 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6033
Bram Moolenaard9fba312005-06-26 22:34:35 +00006034 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036 /* Remove the item before deleting it. */
6037 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006038 if (recurse || (item->li_tv.v_type != VAR_LIST
6039 && item->li_tv.v_type != VAR_DICT))
6040 clear_tv(&item->li_tv);
6041 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 }
6043 vim_free(l);
6044}
6045
6046/*
6047 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006048 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006050 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006051listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052{
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054}
6055
6056/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006057 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006059 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006060listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006062 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 vim_free(item);
6064}
6065
6066/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006067 * Remove a list item from a List and free it. Also clears the value.
6068 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006069 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006070listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006072 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006073 listitem_free(item);
6074}
6075
6076/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 * Get the number of items in a list.
6078 */
6079 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 if (l == NULL)
6083 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006084 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085}
6086
6087/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088 * Return TRUE when two lists have exactly the same values.
6089 */
6090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091list_equal(
6092 list_T *l1,
6093 list_T *l2,
6094 int ic, /* ignore case for strings */
6095 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096{
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006099 if (l1 == NULL || l2 == NULL)
6100 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006101 if (l1 == l2)
6102 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006103 if (list_len(l1) != list_len(l2))
6104 return FALSE;
6105
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 for (item1 = l1->lv_first, item2 = l2->lv_first;
6107 item1 != NULL && item2 != NULL;
6108 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 return FALSE;
6111 return item1 == NULL && item2 == NULL;
6112}
6113
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006114/*
6115 * Return the dictitem that an entry in a hashtable points to.
6116 */
6117 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006119{
6120 return HI2DI(hi);
6121}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006122
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006124 * Return TRUE when two dictionaries have exactly the same key/values.
6125 */
6126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006127dict_equal(
6128 dict_T *d1,
6129 dict_T *d2,
6130 int ic, /* ignore case for strings */
6131 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132{
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hashitem_T *hi;
6134 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006135 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006137 if (d1 == NULL || d2 == NULL)
6138 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006139 if (d1 == d2)
6140 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141 if (dict_len(d1) != dict_len(d2))
6142 return FALSE;
6143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006144 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 if (!HASHITEM_EMPTY(hi))
6148 {
6149 item2 = dict_find(d2, hi->hi_key, -1);
6150 if (item2 == NULL)
6151 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006152 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006153 return FALSE;
6154 --todo;
6155 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156 }
6157 return TRUE;
6158}
6159
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160static int tv_equal_recurse_limit;
6161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006162/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006163 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006165 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 */
6167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006168tv_equal(
6169 typval_T *tv1,
6170 typval_T *tv2,
6171 int ic, /* ignore case */
6172 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173{
6174 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006175 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006176 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006177 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006179 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006180 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006182 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 * recursiveness to a limit. We guess they are equal then.
6184 * A fixed limit has the problem of still taking an awful long time.
6185 * Reduce the limit every time running into it. That should work fine for
6186 * deeply linked structures that are not recursively linked and catch
6187 * recursiveness quickly. */
6188 if (!recursive)
6189 tv_equal_recurse_limit = 1000;
6190 if (recursive_cnt >= tv_equal_recurse_limit)
6191 {
6192 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006193 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195
6196 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199 ++recursive_cnt;
6200 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6201 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006202 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 ++recursive_cnt;
6206 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6207 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006208 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006209
6210 case VAR_FUNC:
6211 return (tv1->vval.v_string != NULL
6212 && tv2->vval.v_string != NULL
6213 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6214
6215 case VAR_NUMBER:
6216 return tv1->vval.v_number == tv2->vval.v_number;
6217
6218 case VAR_STRING:
6219 s1 = get_tv_string_buf(tv1, buf1);
6220 s2 = get_tv_string_buf(tv2, buf2);
6221 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006222
6223 case VAR_SPECIAL:
6224 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006225
6226 case VAR_FLOAT:
6227#ifdef FEAT_FLOAT
6228 return tv1->vval.v_float == tv2->vval.v_float;
6229#endif
6230 case VAR_JOB:
6231#ifdef FEAT_JOB
6232 return tv1->vval.v_job == tv2->vval.v_job;
6233#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006234 case VAR_CHANNEL:
6235#ifdef FEAT_CHANNEL
6236 return tv1->vval.v_channel == tv2->vval.v_channel;
6237#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006238 case VAR_UNKNOWN:
6239 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006241
Bram Moolenaara03f2332016-02-06 18:09:59 +01006242 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6243 * does not equal anything, not even itself. */
6244 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245}
6246
6247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 * Locate item with index "n" in list "l" and return it.
6249 * A negative index is counted from the end; -1 is the last item.
6250 * Returns NULL when "n" is out of range.
6251 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006252 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006253list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254{
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 long idx;
6257
6258 if (l == NULL)
6259 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260
6261 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263 n = l->lv_len + n;
6264
6265 /* Check for index out of range. */
6266 if (n < 0 || n >= l->lv_len)
6267 return NULL;
6268
6269 /* When there is a cached index may start search from there. */
6270 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006272 if (n < l->lv_idx / 2)
6273 {
6274 /* closest to the start of the list */
6275 item = l->lv_first;
6276 idx = 0;
6277 }
6278 else if (n > (l->lv_idx + l->lv_len) / 2)
6279 {
6280 /* closest to the end of the list */
6281 item = l->lv_last;
6282 idx = l->lv_len - 1;
6283 }
6284 else
6285 {
6286 /* closest to the cached index */
6287 item = l->lv_idx_item;
6288 idx = l->lv_idx;
6289 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 }
6291 else
6292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 if (n < l->lv_len / 2)
6294 {
6295 /* closest to the start of the list */
6296 item = l->lv_first;
6297 idx = 0;
6298 }
6299 else
6300 {
6301 /* closest to the end of the list */
6302 item = l->lv_last;
6303 idx = l->lv_len - 1;
6304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306
6307 while (n > idx)
6308 {
6309 /* search forward */
6310 item = item->li_next;
6311 ++idx;
6312 }
6313 while (n < idx)
6314 {
6315 /* search backward */
6316 item = item->li_prev;
6317 --idx;
6318 }
6319
6320 /* cache the used index */
6321 l->lv_idx = idx;
6322 l->lv_idx_item = item;
6323
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 return item;
6325}
6326
6327/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006328 * Get list item "l[idx]" as a number.
6329 */
6330 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006331list_find_nr(
6332 list_T *l,
6333 long idx,
6334 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006335{
6336 listitem_T *li;
6337
6338 li = list_find(l, idx);
6339 if (li == NULL)
6340 {
6341 if (errorp != NULL)
6342 *errorp = TRUE;
6343 return -1L;
6344 }
6345 return get_tv_number_chk(&li->li_tv, errorp);
6346}
6347
6348/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006349 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6350 */
6351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006352list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006353{
6354 listitem_T *li;
6355
6356 li = list_find(l, idx - 1);
6357 if (li == NULL)
6358 {
6359 EMSGN(_(e_listidx), idx);
6360 return NULL;
6361 }
6362 return get_tv_string(&li->li_tv);
6363}
6364
6365/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006366 * Locate "item" list "l" and return its index.
6367 * Returns -1 when "item" is not in the list.
6368 */
6369 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006371{
6372 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006374
6375 if (l == NULL)
6376 return -1;
6377 idx = 0;
6378 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6379 ++idx;
6380 if (li == NULL)
6381 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006382 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006383}
6384
6385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006386 * Append item "item" to the end of list "l".
6387 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006389list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390{
6391 if (l->lv_last == NULL)
6392 {
6393 /* empty list */
6394 l->lv_first = item;
6395 l->lv_last = item;
6396 item->li_prev = NULL;
6397 }
6398 else
6399 {
6400 l->lv_last->li_next = item;
6401 item->li_prev = l->lv_last;
6402 l->lv_last = item;
6403 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006404 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 item->li_next = NULL;
6406}
6407
6408/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006413list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 copy_tv(tv, &li->li_tv);
6420 list_append(l, li);
6421 return OK;
6422}
6423
6424/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006425 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 * Return FAIL when out of memory.
6427 */
6428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006429list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430{
6431 listitem_T *li = listitem_alloc();
6432
6433 if (li == NULL)
6434 return FAIL;
6435 li->li_tv.v_type = VAR_DICT;
6436 li->li_tv.v_lock = 0;
6437 li->li_tv.vval.v_dict = dict;
6438 list_append(list, li);
6439 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440 return OK;
6441}
6442
6443/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006444 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006445 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006446 * Returns FAIL when out of memory.
6447 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006450{
6451 listitem_T *li = listitem_alloc();
6452
6453 if (li == NULL)
6454 return FAIL;
6455 list_append(l, li);
6456 li->li_tv.v_type = VAR_STRING;
6457 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006458 if (str == NULL)
6459 li->li_tv.vval.v_string = NULL;
6460 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006461 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006462 return FAIL;
6463 return OK;
6464}
6465
6466/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006467 * Append "n" to list "l".
6468 * Returns FAIL when out of memory.
6469 */
6470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006472{
6473 listitem_T *li;
6474
6475 li = listitem_alloc();
6476 if (li == NULL)
6477 return FAIL;
6478 li->li_tv.v_type = VAR_NUMBER;
6479 li->li_tv.v_lock = 0;
6480 li->li_tv.vval.v_number = n;
6481 list_append(l, li);
6482 return OK;
6483}
6484
6485/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006487 * If "item" is NULL append at the end.
6488 * Return FAIL when out of memory.
6489 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006491list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494
6495 if (ni == NULL)
6496 return FAIL;
6497 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006498 list_insert(l, ni, item);
6499 return OK;
6500}
6501
6502 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006503list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006504{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 if (item == NULL)
6506 /* Append new item at end of list. */
6507 list_append(l, ni);
6508 else
6509 {
6510 /* Insert new item before existing item. */
6511 ni->li_prev = item->li_prev;
6512 ni->li_next = item;
6513 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006514 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006516 ++l->lv_idx;
6517 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006519 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 l->lv_idx_item = NULL;
6522 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006524 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526}
6527
6528/*
6529 * Extend "l1" with "l2".
6530 * If "bef" is NULL append at the end, otherwise insert before this item.
6531 * Returns FAIL when out of memory.
6532 */
6533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535{
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006537 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006539 /* We also quit the loop when we have inserted the original item count of
6540 * the list, avoid a hang when we extend a list with itself. */
6541 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6543 return FAIL;
6544 return OK;
6545}
6546
6547/*
6548 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6549 * Return FAIL when out of memory.
6550 */
6551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553{
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006556 if (l1 == NULL || l2 == NULL)
6557 return FAIL;
6558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 if (l == NULL)
6562 return FAIL;
6563 tv->v_type = VAR_LIST;
6564 tv->vval.v_list = l;
6565
6566 /* append all items from the second list */
6567 return list_extend(l, l2, NULL);
6568}
6569
6570/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * Returns NULL when out of memory.
6575 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578{
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 list_T *copy;
6580 listitem_T *item;
6581 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582
6583 if (orig == NULL)
6584 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585
6586 copy = list_alloc();
6587 if (copy != NULL)
6588 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 if (copyID != 0)
6590 {
6591 /* Do this before adding the items, because one of the items may
6592 * refer back to this list. */
6593 orig->lv_copyID = copyID;
6594 orig->lv_copylist = copy;
6595 }
6596 for (item = orig->lv_first; item != NULL && !got_int;
6597 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 {
6599 ni = listitem_alloc();
6600 if (ni == NULL)
6601 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006602 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006603 {
6604 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6605 {
6606 vim_free(ni);
6607 break;
6608 }
6609 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006611 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 list_append(copy, ni);
6613 }
6614 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 if (item != NULL)
6616 {
6617 list_unref(copy);
6618 copy = NULL;
6619 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 }
6621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 return copy;
6623}
6624
6625/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006627 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006628 * This used to be called list_remove, but that conflicts with a Sun header
6629 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633{
Bram Moolenaar33570922005-01-25 22:26:29 +00006634 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006636 /* notify watchers */
6637 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006639 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640 list_fix_watch(l, ip);
6641 if (ip == item2)
6642 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644
6645 if (item2->li_next == NULL)
6646 l->lv_last = item->li_prev;
6647 else
6648 item2->li_next->li_prev = item->li_prev;
6649 if (item->li_prev == NULL)
6650 l->lv_first = item2->li_next;
6651 else
6652 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006653 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654}
6655
6656/*
6657 * Return an allocated string with the string representation of a list.
6658 * May return NULL.
6659 */
6660 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662{
6663 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664
6665 if (tv->vval.v_list == NULL)
6666 return NULL;
6667 ga_init2(&ga, (int)sizeof(char), 80);
6668 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006669 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006670 {
6671 vim_free(ga.ga_data);
6672 return NULL;
6673 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674 ga_append(&ga, ']');
6675 ga_append(&ga, NUL);
6676 return (char_u *)ga.ga_data;
6677}
6678
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006679typedef struct join_S {
6680 char_u *s;
6681 char_u *tofree;
6682} join_T;
6683
6684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685list_join_inner(
6686 garray_T *gap, /* to store the result in */
6687 list_T *l,
6688 char_u *sep,
6689 int echo_style,
6690 int copyID,
6691 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692{
6693 int i;
6694 join_T *p;
6695 int len;
6696 int sumlen = 0;
6697 int first = TRUE;
6698 char_u *tofree;
6699 char_u numbuf[NUMBUFLEN];
6700 listitem_T *item;
6701 char_u *s;
6702
6703 /* Stringify each item in the list. */
6704 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6705 {
6706 if (echo_style)
6707 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6708 else
6709 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6710 if (s == NULL)
6711 return FAIL;
6712
6713 len = (int)STRLEN(s);
6714 sumlen += len;
6715
Bram Moolenaarcde88542015-08-11 19:14:00 +02006716 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6718 if (tofree != NULL || s != numbuf)
6719 {
6720 p->s = s;
6721 p->tofree = tofree;
6722 }
6723 else
6724 {
6725 p->s = vim_strnsave(s, len);
6726 p->tofree = p->s;
6727 }
6728
6729 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006730 if (did_echo_string_emsg) /* recursion error, bail out */
6731 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732 }
6733
6734 /* Allocate result buffer with its total size, avoid re-allocation and
6735 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6736 if (join_gap->ga_len >= 2)
6737 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6738 if (ga_grow(gap, sumlen + 2) == FAIL)
6739 return FAIL;
6740
6741 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6742 {
6743 if (first)
6744 first = FALSE;
6745 else
6746 ga_concat(gap, sep);
6747 p = ((join_T *)join_gap->ga_data) + i;
6748
6749 if (p->s != NULL)
6750 ga_concat(gap, p->s);
6751 line_breakcheck();
6752 }
6753
6754 return OK;
6755}
6756
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006757/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006759 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006760 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006761 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006763list_join(
6764 garray_T *gap,
6765 list_T *l,
6766 char_u *sep,
6767 int echo_style,
6768 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006770 garray_T join_ga;
6771 int retval;
6772 join_T *p;
6773 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774
Bram Moolenaard39a7512015-04-16 22:51:22 +02006775 if (l->lv_len < 1)
6776 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6778 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6779
6780 /* Dispose each item in join_ga. */
6781 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006783 p = (join_T *)join_ga.ga_data;
6784 for (i = 0; i < join_ga.ga_len; ++i)
6785 {
6786 vim_free(p->tofree);
6787 ++p;
6788 }
6789 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791
6792 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006793}
6794
6795/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006796 * Return the next (unique) copy ID.
6797 * Used for serializing nested structures.
6798 */
6799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006800get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006801{
6802 current_copyID += COPYID_INC;
6803 return current_copyID;
6804}
6805
6806/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 * Garbage collection for lists and dictionaries.
6808 *
6809 * We use reference counts to be able to free most items right away when they
6810 * are no longer used. But for composite items it's possible that it becomes
6811 * unused while the reference count is > 0: When there is a recursive
6812 * reference. Example:
6813 * :let l = [1, 2, 3]
6814 * :let d = {9: l}
6815 * :let l[1] = d
6816 *
6817 * Since this is quite unusual we handle this with garbage collection: every
6818 * once in a while find out which lists and dicts are not referenced from any
6819 * variable.
6820 *
6821 * Here is a good reference text about garbage collection (refers to Python
6822 * but it applies to all reference-counting mechanisms):
6823 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006824 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006825
6826/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 * Do garbage collection for lists and dicts.
6828 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006831garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006833 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006834 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 buf_T *buf;
6836 win_T *wp;
6837 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006838 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006839 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006840 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006841#ifdef FEAT_WINDOWS
6842 tabpage_T *tp;
6843#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006844
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006845 /* Only do this once. */
6846 want_garbage_collect = FALSE;
6847 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006848 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006849
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006850 /* We advance by two because we add one for items referenced through
6851 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006852 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 /*
6855 * 1. Go through all accessible variables and mark all lists and dicts
6856 * with copyID.
6857 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858
6859 /* Don't free variables in the previous_funccal list unless they are only
6860 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006861 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6863 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006864 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6865 NULL);
6866 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6867 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 }
6869
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870 /* script-local variables */
6871 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873
6874 /* buffer-local variables */
6875 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006876 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6877 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878
6879 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006880 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6882 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006883#ifdef FEAT_AUTOCMD
6884 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6886 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006887#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006888
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006889#ifdef FEAT_WINDOWS
6890 /* tabpage-local variables */
6891 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006894#endif
6895
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898
6899 /* function-local variables */
6900 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6901 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6903 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 }
6905
Bram Moolenaard812df62008-11-09 12:46:09 +00006906 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006908
Bram Moolenaar1dced572012-04-05 16:54:08 +02006909#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006911#endif
6912
Bram Moolenaardb913952012-06-29 12:54:53 +02006913#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006915#endif
6916
6917#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006919#endif
6920
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006921#ifdef FEAT_CHANNEL
6922 abort = abort || set_ref_in_channel(copyID);
6923#endif
6924
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006926 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 /*
6928 * 2. Free lists and dictionaries that are not referenced.
6929 */
6930 did_free = free_unref_items(copyID);
6931
6932 /*
6933 * 3. Check if any funccal can be freed now.
6934 */
6935 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006936 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 if (can_free_funccal(*pfc, copyID))
6938 {
6939 fc = *pfc;
6940 *pfc = fc->caller;
6941 free_funccal(fc, TRUE);
6942 did_free = TRUE;
6943 did_free_funccal = TRUE;
6944 }
6945 else
6946 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 if (did_free_funccal)
6949 /* When a funccal was freed some more items might be garbage
6950 * collected, so run again. */
6951 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 else if (p_verbose > 0)
6954 {
6955 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6956 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957
6958 return did_free;
6959}
6960
6961/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006962 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 */
6964 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006967 dict_T *dd, *dd_next;
6968 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969 int did_free = FALSE;
6970
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 */
6974 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006975 {
6976 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006979 /* Free the Dictionary and ordinary items it contains, but don't
6980 * recurse into Lists and Dictionaries, they will be in the list
6981 * of dicts or list of lists. */
6982 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 dd = dd_next;
6986 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987
6988 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006989 * Go through the list of lists and free items without the copyID.
6990 * But don't free a list that has a watcher (used in a for loop), these
6991 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 */
6993 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006994 {
6995 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6997 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006999 /* Free the List and ordinary items it contains, but don't recurse
7000 * into Lists and Dictionaries, they will be in the list of dicts
7001 * or list of lists. */
7002 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007005 ll = ll_next;
7006 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007007
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 return did_free;
7009}
7010
7011/*
7012 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007013 * "list_stack" is used to add lists to be marked. Can be NULL.
7014 *
7015 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007018set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019{
7020 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 hashtab_T *cur_ht;
7024 ht_stack_T *ht_stack = NULL;
7025 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 cur_ht = ht;
7028 for (;;)
7029 {
7030 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 /* Mark each item in the hashtab. If the item contains a hashtab
7033 * it is added to ht_stack, if it contains a list it is added to
7034 * list_stack. */
7035 todo = (int)cur_ht->ht_used;
7036 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7037 if (!HASHITEM_EMPTY(hi))
7038 {
7039 --todo;
7040 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7041 &ht_stack, list_stack);
7042 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044
7045 if (ht_stack == NULL)
7046 break;
7047
7048 /* take an item from the stack */
7049 cur_ht = ht_stack->ht;
7050 tempitem = ht_stack;
7051 ht_stack = ht_stack->prev;
7052 free(tempitem);
7053 }
7054
7055 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056}
7057
7058/*
7059 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007060 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7061 *
7062 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007065set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 listitem_T *li;
7068 int abort = FALSE;
7069 list_T *cur_l;
7070 list_stack_T *list_stack = NULL;
7071 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 cur_l = l;
7074 for (;;)
7075 {
7076 if (!abort)
7077 /* Mark each item in the list. If the item contains a hashtab
7078 * it is added to ht_stack, if it contains a list it is added to
7079 * list_stack. */
7080 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7081 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7082 ht_stack, &list_stack);
7083 if (list_stack == NULL)
7084 break;
7085
7086 /* take an item from the stack */
7087 cur_l = list_stack->list;
7088 tempitem = list_stack;
7089 list_stack = list_stack->prev;
7090 free(tempitem);
7091 }
7092
7093 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094}
7095
7096/*
7097 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 * "list_stack" is used to add lists to be marked. Can be NULL.
7099 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7100 *
7101 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007104set_ref_in_item(
7105 typval_T *tv,
7106 int copyID,
7107 ht_stack_T **ht_stack,
7108 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109{
7110 dict_T *dd;
7111 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007112 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007113
Bram Moolenaara03f2332016-02-06 18:09:59 +01007114 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007115 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007116 dd = tv->vval.v_dict;
7117 if (dd != NULL && dd->dv_copyID != copyID)
7118 {
7119 /* Didn't see this dict yet. */
7120 dd->dv_copyID = copyID;
7121 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007122 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007123 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7124 }
7125 else
7126 {
7127 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7128 if (newitem == NULL)
7129 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 else
7131 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007132 newitem->ht = &dd->dv_hashtab;
7133 newitem->prev = *ht_stack;
7134 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007136 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007137 }
7138 }
7139 else if (tv->v_type == VAR_LIST)
7140 {
7141 ll = tv->vval.v_list;
7142 if (ll != NULL && ll->lv_copyID != copyID)
7143 {
7144 /* Didn't see this list yet. */
7145 ll->lv_copyID = copyID;
7146 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007148 abort = set_ref_in_list(ll, copyID, ht_stack);
7149 }
7150 else
7151 {
7152 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007153 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007154 if (newitem == NULL)
7155 abort = TRUE;
7156 else
7157 {
7158 newitem->list = ll;
7159 newitem->prev = *list_stack;
7160 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007163 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166}
7167
7168/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 * Allocate an empty header for a dictionary.
7170 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007171 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007172dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173{
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007175
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007178 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007179 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007180 if (first_dict != NULL)
7181 first_dict->dv_used_prev = d;
7182 d->dv_used_next = first_dict;
7183 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007184 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185
Bram Moolenaar33570922005-01-25 22:26:29 +00007186 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007187 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007188 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007189 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007190 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193}
7194
7195/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007196 * Allocate an empty dict for a return value.
7197 * Returns OK or FAIL.
7198 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007200rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007201{
7202 dict_T *d = dict_alloc();
7203
7204 if (d == NULL)
7205 return FAIL;
7206
7207 rettv->vval.v_dict = d;
7208 rettv->v_type = VAR_DICT;
7209 ++d->dv_refcount;
7210 return OK;
7211}
7212
7213
7214/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 * Unreference a Dictionary: decrement the reference count and free it when it
7216 * becomes zero.
7217 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007219dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 if (d != NULL && --d->dv_refcount <= 0)
7222 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223}
7224
7225/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007226 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227 * Ignores the reference count.
7228 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007230dict_free(
7231 dict_T *d,
7232 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007234 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007236 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007238 /* Remove the dict from the list of dicts for garbage collection. */
7239 if (d->dv_used_prev == NULL)
7240 first_dict = d->dv_used_next;
7241 else
7242 d->dv_used_prev->dv_used_next = d->dv_used_next;
7243 if (d->dv_used_next != NULL)
7244 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7245
7246 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007247 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007248 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251 if (!HASHITEM_EMPTY(hi))
7252 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007253 /* Remove the item before deleting it, just in case there is
7254 * something recursive causing trouble. */
7255 di = HI2DI(hi);
7256 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007257 if (recurse || (di->di_tv.v_type != VAR_LIST
7258 && di->di_tv.v_type != VAR_DICT))
7259 clear_tv(&di->di_tv);
7260 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007261 --todo;
7262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 vim_free(d);
7266}
7267
7268/*
7269 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 * The "key" is copied to the new item.
7271 * Note that the value of the item "di_tv" still needs to be initialized!
7272 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007274 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007275dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276{
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007279 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007283 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007284 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286}
7287
7288/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289 * Make a copy of a Dictionary item.
7290 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007292dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293{
Bram Moolenaar33570922005-01-25 22:26:29 +00007294 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007296 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7297 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 if (di != NULL)
7299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007301 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 copy_tv(&org->di_tv, &di->di_tv);
7303 }
7304 return di;
7305}
7306
7307/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 * Remove item "item" from Dictionary "dict" and free it.
7309 */
7310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007311dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312{
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 if (HASHITEM_EMPTY(hi))
7317 EMSG2(_(e_intern2), "dictitem_remove()");
7318 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 dictitem_free(item);
7321}
7322
7323/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324 * Free a dict item. Also clears the value.
7325 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007327dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007330 if (item->di_flags & DI_FLAGS_ALLOC)
7331 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332}
7333
7334/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7336 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007337 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 * Returns NULL when out of memory.
7339 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007341dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342{
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 dict_T *copy;
7344 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007347
7348 if (orig == NULL)
7349 return NULL;
7350
7351 copy = dict_alloc();
7352 if (copy != NULL)
7353 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007354 if (copyID != 0)
7355 {
7356 orig->dv_copyID = copyID;
7357 orig->dv_copydict = copy;
7358 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007359 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007360 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 --todo;
7365
7366 di = dictitem_alloc(hi->hi_key);
7367 if (di == NULL)
7368 break;
7369 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007370 {
7371 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7372 copyID) == FAIL)
7373 {
7374 vim_free(di);
7375 break;
7376 }
7377 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007378 else
7379 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7380 if (dict_add(copy, di) == FAIL)
7381 {
7382 dictitem_free(di);
7383 break;
7384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 if (todo > 0)
7390 {
7391 dict_unref(copy);
7392 copy = NULL;
7393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 }
7395
7396 return copy;
7397}
7398
7399/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007401 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007403 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007404dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405{
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407}
7408
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007410 * Add a number or string entry to dictionary "d".
7411 * When "str" is NULL use number "nr", otherwise use "str".
7412 * Returns FAIL when out of memory and when key already exists.
7413 */
7414 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007415dict_add_nr_str(
7416 dict_T *d,
7417 char *key,
7418 long nr,
7419 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007420{
7421 dictitem_T *item;
7422
7423 item = dictitem_alloc((char_u *)key);
7424 if (item == NULL)
7425 return FAIL;
7426 item->di_tv.v_lock = 0;
7427 if (str == NULL)
7428 {
7429 item->di_tv.v_type = VAR_NUMBER;
7430 item->di_tv.vval.v_number = nr;
7431 }
7432 else
7433 {
7434 item->di_tv.v_type = VAR_STRING;
7435 item->di_tv.vval.v_string = vim_strsave(str);
7436 }
7437 if (dict_add(d, item) == FAIL)
7438 {
7439 dictitem_free(item);
7440 return FAIL;
7441 }
7442 return OK;
7443}
7444
7445/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007446 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007447 * Returns FAIL when out of memory and when key already exists.
7448 */
7449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007450dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007451{
7452 dictitem_T *item;
7453
7454 item = dictitem_alloc((char_u *)key);
7455 if (item == NULL)
7456 return FAIL;
7457 item->di_tv.v_lock = 0;
7458 item->di_tv.v_type = VAR_LIST;
7459 item->di_tv.vval.v_list = list;
7460 if (dict_add(d, item) == FAIL)
7461 {
7462 dictitem_free(item);
7463 return FAIL;
7464 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007465 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007466 return OK;
7467}
7468
7469/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 * Get the number of items in a Dictionary.
7471 */
7472 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007473dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 if (d == NULL)
7476 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007477 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478}
7479
7480/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481 * Find item "key[len]" in Dictionary "d".
7482 * If "len" is negative use strlen(key).
7483 * Returns NULL when not found.
7484 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007485 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007486dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488#define AKEYLEN 200
7489 char_u buf[AKEYLEN];
7490 char_u *akey;
7491 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 if (len < 0)
7495 akey = key;
7496 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007497 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007498 tofree = akey = vim_strnsave(key, len);
7499 if (akey == NULL)
7500 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007501 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 else
7503 {
7504 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007505 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 akey = buf;
7507 }
7508
Bram Moolenaar33570922005-01-25 22:26:29 +00007509 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 vim_free(tofree);
7511 if (HASHITEM_EMPTY(hi))
7512 return NULL;
7513 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514}
7515
7516/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007517 * Get a string item from a dictionary.
7518 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007519 * Returns NULL if the entry doesn't exist or out of memory.
7520 */
7521 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007522get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007523{
7524 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007525 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007526
7527 di = dict_find(d, key, -1);
7528 if (di == NULL)
7529 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007530 s = get_tv_string(&di->di_tv);
7531 if (save && s != NULL)
7532 s = vim_strsave(s);
7533 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007534}
7535
7536/*
7537 * Get a number item from a dictionary.
7538 * Returns 0 if the entry doesn't exist or out of memory.
7539 */
7540 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007542{
7543 dictitem_T *di;
7544
7545 di = dict_find(d, key, -1);
7546 if (di == NULL)
7547 return 0;
7548 return get_tv_number(&di->di_tv);
7549}
7550
7551/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 * Return an allocated string with the string representation of a Dictionary.
7553 * May return NULL.
7554 */
7555 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557{
7558 garray_T ga;
7559 int first = TRUE;
7560 char_u *tofree;
7561 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007567 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 return NULL;
7569 ga_init2(&ga, (int)sizeof(char), 80);
7570 ga_append(&ga, '{');
7571
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007572 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007573 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 --todo;
7578
7579 if (first)
7580 first = FALSE;
7581 else
7582 ga_concat(&ga, (char_u *)", ");
7583
7584 tofree = string_quote(hi->hi_key, FALSE);
7585 if (tofree != NULL)
7586 {
7587 ga_concat(&ga, tofree);
7588 vim_free(tofree);
7589 }
7590 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007591 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 if (s != NULL)
7593 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007595 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007596 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007597 line_breakcheck();
7598
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007601 if (todo > 0)
7602 {
7603 vim_free(ga.ga_data);
7604 return NULL;
7605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606
7607 ga_append(&ga, '}');
7608 ga_append(&ga, NUL);
7609 return (char_u *)ga.ga_data;
7610}
7611
7612/*
7613 * Allocate a variable for a Dictionary and fill it from "*arg".
7614 * Return OK or FAIL. Returns NOTDONE for {expr}.
7615 */
7616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007617get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618{
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 dict_T *d = NULL;
7620 typval_T tvkey;
7621 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007622 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007625 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626
7627 /*
7628 * First check if it's not a curly-braces thing: {expr}.
7629 * Must do this without evaluating, otherwise a function may be called
7630 * twice. Unfortunately this means we need to call eval1() twice for the
7631 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007632 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007634 if (*start != '}')
7635 {
7636 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7637 return FAIL;
7638 if (*start == '}')
7639 return NOTDONE;
7640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641
7642 if (evaluate)
7643 {
7644 d = dict_alloc();
7645 if (d == NULL)
7646 return FAIL;
7647 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007648 tvkey.v_type = VAR_UNKNOWN;
7649 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650
7651 *arg = skipwhite(*arg + 1);
7652 while (**arg != '}' && **arg != NUL)
7653 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007654 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 goto failret;
7656 if (**arg != ':')
7657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007658 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 goto failret;
7661 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007662 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007664 key = get_tv_string_buf_chk(&tvkey, buf);
7665 if (key == NULL || *key == NUL)
7666 {
7667 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7668 if (key != NULL)
7669 EMSG(_(e_emptykey));
7670 clear_tv(&tvkey);
7671 goto failret;
7672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674
7675 *arg = skipwhite(*arg + 1);
7676 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7677 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007678 if (evaluate)
7679 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 goto failret;
7681 }
7682 if (evaluate)
7683 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007684 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 if (item != NULL)
7686 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007687 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 clear_tv(&tv);
7690 goto failret;
7691 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 item = dictitem_alloc(key);
7693 clear_tv(&tvkey);
7694 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007697 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 if (dict_add(d, item) == FAIL)
7699 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 }
7701 }
7702
7703 if (**arg == '}')
7704 break;
7705 if (**arg != ',')
7706 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007707 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 goto failret;
7709 }
7710 *arg = skipwhite(*arg + 1);
7711 }
7712
7713 if (**arg != '}')
7714 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007715 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716failret:
7717 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007718 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 return FAIL;
7720 }
7721
7722 *arg = skipwhite(*arg + 1);
7723 if (evaluate)
7724 {
7725 rettv->v_type = VAR_DICT;
7726 rettv->vval.v_dict = d;
7727 ++d->dv_refcount;
7728 }
7729
7730 return OK;
7731}
7732
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007733#if defined(FEAT_CHANNEL) || defined(PROTO)
7734/*
7735 * Decrement the reference count on "channel" and free it when it goes down to
7736 * zero.
7737 * Returns TRUE when the channel was freed.
7738 */
7739 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007740channel_unref(channel_T *channel)
7741{
7742 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007743 {
Bram Moolenaar77073442016-02-13 23:23:53 +01007744 channel_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007745 return TRUE;
7746 }
7747 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007748}
7749#endif
7750
Bram Moolenaar835dc632016-02-07 14:27:38 +01007751#ifdef FEAT_JOB
7752 static void
7753job_free(job_T *job)
7754{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007755# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007756 if (job->jv_channel != NULL)
7757 {
7758 /* The channel doesn't count as a references for the job, we need to
7759 * NULL the reference when the job is freed. */
7760 job->jv_channel->ch_job = NULL;
7761 channel_unref(job->jv_channel);
7762 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007763# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007764 mch_clear_job(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007765 vim_free(job);
7766}
7767
7768 static void
7769job_unref(job_T *job)
7770{
7771 if (job != NULL && --job->jv_refcount <= 0)
7772 job_free(job);
7773}
7774
7775/*
7776 * Allocate a job. Sets the refcount to one.
7777 */
7778 static job_T *
7779job_alloc(void)
7780{
7781 job_T *job;
7782
7783 job = (job_T *)alloc_clear(sizeof(job_T));
7784 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007785 job->jv_refcount = 1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007786 return job;
7787}
7788
7789#endif
7790
Bram Moolenaar17a13432016-01-24 14:22:10 +01007791 static char *
7792get_var_special_name(int nr)
7793{
7794 switch (nr)
7795 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007796 case VVAL_FALSE: return "v:false";
7797 case VVAL_TRUE: return "v:true";
7798 case VVAL_NONE: return "v:none";
7799 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007800 }
7801 EMSG2(_(e_intern2), "get_var_special_name()");
7802 return "42";
7803}
7804
Bram Moolenaar8c711452005-01-14 21:53:12 +00007805/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 * Return a string with the string representation of a variable.
7807 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007808 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007809 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007810 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007811 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 */
7813 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007814echo_string(
7815 typval_T *tv,
7816 char_u **tofree,
7817 char_u *numbuf,
7818 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007819{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 static int recurse = 0;
7821 char_u *r = NULL;
7822
Bram Moolenaar33570922005-01-25 22:26:29 +00007823 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007825 if (!did_echo_string_emsg)
7826 {
7827 /* Only give this message once for a recursive call to avoid
7828 * flooding the user with errors. And stop iterating over lists
7829 * and dicts. */
7830 did_echo_string_emsg = TRUE;
7831 EMSG(_("E724: variable nested too deep for displaying"));
7832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007833 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007834 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 }
7836 ++recurse;
7837
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007838 switch (tv->v_type)
7839 {
7840 case VAR_FUNC:
7841 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 r = tv->vval.v_string;
7843 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007844
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007845 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846 if (tv->vval.v_list == NULL)
7847 {
7848 *tofree = NULL;
7849 r = NULL;
7850 }
7851 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7852 {
7853 *tofree = NULL;
7854 r = (char_u *)"[...]";
7855 }
7856 else
7857 {
7858 tv->vval.v_list->lv_copyID = copyID;
7859 *tofree = list2string(tv, copyID);
7860 r = *tofree;
7861 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007862 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007863
Bram Moolenaar8c711452005-01-14 21:53:12 +00007864 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007865 if (tv->vval.v_dict == NULL)
7866 {
7867 *tofree = NULL;
7868 r = NULL;
7869 }
7870 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7871 {
7872 *tofree = NULL;
7873 r = (char_u *)"{...}";
7874 }
7875 else
7876 {
7877 tv->vval.v_dict->dv_copyID = copyID;
7878 *tofree = dict2string(tv, copyID);
7879 r = *tofree;
7880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007881 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007882
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883 case VAR_STRING:
7884 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007885 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007886 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007887 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 *tofree = NULL;
7889 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007890 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007891
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007892 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007893#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894 *tofree = NULL;
7895 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7896 r = numbuf;
7897 break;
7898#endif
7899
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007900 case VAR_SPECIAL:
7901 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007902 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007903 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905
Bram Moolenaar8502c702014-06-17 12:51:16 +02007906 if (--recurse == 0)
7907 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007908 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909}
7910
7911/*
7912 * Return a string with the string representation of a variable.
7913 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7914 * "numbuf" is used for a number.
7915 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007916 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917 */
7918 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007919tv2string(
7920 typval_T *tv,
7921 char_u **tofree,
7922 char_u *numbuf,
7923 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007924{
7925 switch (tv->v_type)
7926 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007927 case VAR_FUNC:
7928 *tofree = string_quote(tv->vval.v_string, TRUE);
7929 return *tofree;
7930 case VAR_STRING:
7931 *tofree = string_quote(tv->vval.v_string, FALSE);
7932 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007933#ifdef FEAT_FLOAT
7934 case VAR_FLOAT:
7935 *tofree = NULL;
7936 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7937 return numbuf;
7938#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007939 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007940 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007942 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007943 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007944 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007945 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007946 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007947 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007948 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949}
7950
7951/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 * Return string "str" in ' quotes, doubling ' characters.
7953 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007955 */
7956 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007957string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007958{
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 char_u *p, *r, *s;
7961
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 len = (function ? 13 : 3);
7963 if (str != NULL)
7964 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007965 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 for (p = str; *p != NUL; mb_ptr_adv(p))
7967 if (*p == '\'')
7968 ++len;
7969 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007970 s = r = alloc(len);
7971 if (r != NULL)
7972 {
7973 if (function)
7974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007975 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007976 r += 10;
7977 }
7978 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 if (str != NULL)
7981 for (p = str; *p != NUL; )
7982 {
7983 if (*p == '\'')
7984 *r++ = '\'';
7985 MB_COPY_CHAR(p, r);
7986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007987 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007988 if (function)
7989 *r++ = ')';
7990 *r++ = NUL;
7991 }
7992 return s;
7993}
7994
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007995#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007996/*
7997 * Convert the string "text" to a floating point number.
7998 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7999 * this always uses a decimal point.
8000 * Returns the length of the text that was consumed.
8001 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008003string2float(
8004 char_u *text,
8005 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008006{
8007 char *s = (char *)text;
8008 float_T f;
8009
8010 f = strtod(s, &s);
8011 *value = f;
8012 return (int)((char_u *)s - text);
8013}
8014#endif
8015
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 * Get the value of an environment variable.
8018 * "arg" is pointing to the '$'. It is advanced to after the name.
8019 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008020 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 */
8022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008023get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 char_u *string = NULL;
8026 int len;
8027 int cc;
8028 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008029 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030
8031 ++*arg;
8032 name = *arg;
8033 len = get_env_len(arg);
8034 if (evaluate)
8035 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008036 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008037 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008038
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008039 cc = name[len];
8040 name[len] = NUL;
8041 /* first try vim_getenv(), fast for normal environment vars */
8042 string = vim_getenv(name, &mustfree);
8043 if (string != NULL && *string != NUL)
8044 {
8045 if (!mustfree)
8046 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008048 else
8049 {
8050 if (mustfree)
8051 vim_free(string);
8052
8053 /* next try expanding things like $VIM and ${HOME} */
8054 string = expand_env_save(name - 1);
8055 if (string != NULL && *string == '$')
8056 {
8057 vim_free(string);
8058 string = NULL;
8059 }
8060 }
8061 name[len] = cc;
8062
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 rettv->v_type = VAR_STRING;
8064 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 }
8066
8067 return OK;
8068}
8069
8070/*
8071 * Array with names and number of arguments of all internal functions
8072 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8073 */
8074static struct fst
8075{
8076 char *f_name; /* function name */
8077 char f_min_argc; /* minimal number of arguments */
8078 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008079 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008080 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081} functions[] =
8082{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008083#ifdef FEAT_FLOAT
8084 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008085 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008087 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008088 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008089 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"append", 2, 2, f_append},
8091 {"argc", 0, 0, f_argc},
8092 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008093 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008094 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008095#ifdef FEAT_FLOAT
8096 {"asin", 1, 1, f_asin}, /* WJMc */
8097#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008098 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008099 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008100 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008101 {"assert_false", 1, 2, f_assert_false},
8102 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008103#ifdef FEAT_FLOAT
8104 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008105 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008108 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"bufexists", 1, 1, f_bufexists},
8110 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8111 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8112 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8113 {"buflisted", 1, 1, f_buflisted},
8114 {"bufloaded", 1, 1, f_bufloaded},
8115 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008116 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"bufwinnr", 1, 1, f_bufwinnr},
8118 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008119 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008120 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008121 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008122#ifdef FEAT_FLOAT
8123 {"ceil", 1, 1, f_ceil},
8124#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008125#ifdef FEAT_CHANNEL
8126 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008127 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008128 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008129 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008130 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8131 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar77073442016-02-13 23:23:53 +01008132 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008133#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008134 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008135 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008137 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008139#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008140 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008141 {"complete_add", 1, 1, f_complete_add},
8142 {"complete_check", 0, 0, f_complete_check},
8143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008146#ifdef FEAT_FLOAT
8147 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008148 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008149#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008150 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008152 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008153 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008154 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008156 {"diff_filler", 1, 1, f_diff_filler},
8157 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008158 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008159 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008161 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"eventhandler", 0, 0, f_eventhandler},
8163 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008164 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008166#ifdef FEAT_FLOAT
8167 {"exp", 1, 1, f_exp},
8168#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008169 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008171 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8173 {"filereadable", 1, 1, f_filereadable},
8174 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008175 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008176 {"finddir", 1, 3, f_finddir},
8177 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008178#ifdef FEAT_FLOAT
8179 {"float2nr", 1, 1, f_float2nr},
8180 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008181 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008183 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"fnamemodify", 2, 2, f_fnamemodify},
8185 {"foldclosed", 1, 1, f_foldclosed},
8186 {"foldclosedend", 1, 1, f_foldclosedend},
8187 {"foldlevel", 1, 1, f_foldlevel},
8188 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008189 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008191 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008192 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008193 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008194 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008195 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"getchar", 0, 1, f_getchar},
8197 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008198 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"getcmdline", 0, 0, f_getcmdline},
8200 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008201 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008202 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008203 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008204 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008205 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008206 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"getfsize", 1, 1, f_getfsize},
8208 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008209 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008210 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008211 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008212 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008213 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008214 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008215 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008216 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008218 {"gettabvar", 2, 3, f_gettabvar},
8219 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 {"getwinposx", 0, 0, f_getwinposx},
8221 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008222 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008223 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008224 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008225 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008227 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008228 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008229 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8231 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8232 {"histadd", 2, 2, f_histadd},
8233 {"histdel", 1, 2, f_histdel},
8234 {"histget", 1, 2, f_histget},
8235 {"histnr", 1, 1, f_histnr},
8236 {"hlID", 1, 1, f_hlID},
8237 {"hlexists", 1, 1, f_hlexists},
8238 {"hostname", 0, 0, f_hostname},
8239 {"iconv", 3, 3, f_iconv},
8240 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008242 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008244 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"inputrestore", 0, 0, f_inputrestore},
8246 {"inputsave", 0, 0, f_inputsave},
8247 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008248 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008249 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008251 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008252 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008253#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008254# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008255 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008256# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008257 {"job_start", 1, 2, f_job_start},
8258 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008259 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008260#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008261 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008262 {"js_decode", 1, 1, f_js_decode},
8263 {"js_encode", 1, 1, f_js_encode},
8264 {"json_decode", 1, 1, f_json_decode},
8265 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008266 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"libcall", 3, 3, f_libcall},
8270 {"libcallnr", 3, 3, f_libcallnr},
8271 {"line", 1, 1, f_line},
8272 {"line2byte", 1, 1, f_line2byte},
8273 {"lispindent", 1, 1, f_lispindent},
8274 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008275#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008276 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008277 {"log10", 1, 1, f_log10},
8278#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008279#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008280 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008281#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008282 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008283 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008284 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008285 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008286 {"matchadd", 2, 5, f_matchadd},
8287 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008288 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008289 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008290 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008291 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008292 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008293 {"max", 1, 1, f_max},
8294 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008295#ifdef vim_mkdir
8296 {"mkdir", 1, 3, f_mkdir},
8297#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008299#ifdef FEAT_MZSCHEME
8300 {"mzeval", 1, 1, f_mzeval},
8301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008303 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008304 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008305 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008306#ifdef FEAT_PERL
8307 {"perleval", 1, 1, f_perleval},
8308#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008309#ifdef FEAT_FLOAT
8310 {"pow", 2, 2, f_pow},
8311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008313 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008314 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008315#ifdef FEAT_PYTHON3
8316 {"py3eval", 1, 1, f_py3eval},
8317#endif
8318#ifdef FEAT_PYTHON
8319 {"pyeval", 1, 1, f_pyeval},
8320#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008321 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008322 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008323 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008324 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008325 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {"remote_expr", 2, 3, f_remote_expr},
8327 {"remote_foreground", 1, 1, f_remote_foreground},
8328 {"remote_peek", 1, 2, f_remote_peek},
8329 {"remote_read", 1, 1, f_remote_read},
8330 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008331 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008333 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008335 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008336#ifdef FEAT_FLOAT
8337 {"round", 1, 1, f_round},
8338#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008339 {"screenattr", 2, 2, f_screenattr},
8340 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008341 {"screencol", 0, 0, f_screencol},
8342 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008343 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008344 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008345 {"searchpair", 3, 7, f_searchpair},
8346 {"searchpairpos", 3, 7, f_searchpairpos},
8347 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"server2client", 2, 2, f_server2client},
8349 {"serverlist", 0, 0, f_serverlist},
8350 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008351 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"setcmdpos", 1, 1, f_setcmdpos},
8353 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008354 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008355 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008356 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008357 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008359 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008360 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008362#ifdef FEAT_CRYPT
8363 {"sha256", 1, 1, f_sha256},
8364#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008365 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008366 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368#ifdef FEAT_FLOAT
8369 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008370 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008372 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008373 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008374 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008375 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008376 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008377#ifdef FEAT_FLOAT
8378 {"sqrt", 1, 1, f_sqrt},
8379 {"str2float", 1, 1, f_str2float},
8380#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008381 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008382 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008383 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384#ifdef HAVE_STRFTIME
8385 {"strftime", 1, 2, f_strftime},
8386#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008387 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008388 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"strlen", 1, 1, f_strlen},
8390 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008391 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008393 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008394 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"substitute", 4, 4, f_substitute},
8396 {"synID", 3, 3, f_synID},
8397 {"synIDattr", 2, 3, f_synIDattr},
8398 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008399 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008400 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008401 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008402 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008403 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008404 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008405 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008406 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008407 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008408#ifdef FEAT_FLOAT
8409 {"tan", 1, 1, f_tan},
8410 {"tanh", 1, 1, f_tanh},
8411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008413 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {"tolower", 1, 1, f_tolower},
8415 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008416 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008417#ifdef FEAT_FLOAT
8418 {"trunc", 1, 1, f_trunc},
8419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008421 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008422 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008423 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008424 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {"virtcol", 1, 1, f_virtcol},
8426 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008427 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {"winbufnr", 1, 1, f_winbufnr},
8429 {"wincol", 0, 0, f_wincol},
8430 {"winheight", 1, 1, f_winheight},
8431 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008432 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008434 {"winrestview", 1, 1, f_winrestview},
8435 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008437 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008438 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008439 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440};
8441
8442#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8443
8444/*
8445 * Function given to ExpandGeneric() to obtain the list of internal
8446 * or user defined function names.
8447 */
8448 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008449get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 static int intidx = -1;
8452 char_u *name;
8453
8454 if (idx == 0)
8455 intidx = -1;
8456 if (intidx < 0)
8457 {
8458 name = get_user_func_name(xp, idx);
8459 if (name != NULL)
8460 return name;
8461 }
8462 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8463 {
8464 STRCPY(IObuff, functions[intidx].f_name);
8465 STRCAT(IObuff, "(");
8466 if (functions[intidx].f_max_argc == 0)
8467 STRCAT(IObuff, ")");
8468 return IObuff;
8469 }
8470
8471 return NULL;
8472}
8473
8474/*
8475 * Function given to ExpandGeneric() to obtain the list of internal or
8476 * user defined variable or function names.
8477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008479get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480{
8481 static int intidx = -1;
8482 char_u *name;
8483
8484 if (idx == 0)
8485 intidx = -1;
8486 if (intidx < 0)
8487 {
8488 name = get_function_name(xp, idx);
8489 if (name != NULL)
8490 return name;
8491 }
8492 return get_user_var_name(xp, ++intidx);
8493}
8494
8495#endif /* FEAT_CMDL_COMPL */
8496
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008497#if defined(EBCDIC) || defined(PROTO)
8498/*
8499 * Compare struct fst by function name.
8500 */
8501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008502compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008503{
8504 struct fst *p1 = (struct fst *)s1;
8505 struct fst *p2 = (struct fst *)s2;
8506
8507 return STRCMP(p1->f_name, p2->f_name);
8508}
8509
8510/*
8511 * Sort the function table by function name.
8512 * The sorting of the table above is ASCII dependant.
8513 * On machines using EBCDIC we have to sort it.
8514 */
8515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008516sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008517{
8518 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8519
8520 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8521}
8522#endif
8523
8524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525/*
8526 * Find internal function in table above.
8527 * Return index, or -1 if not found
8528 */
8529 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530find_internal_func(
8531 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 int first = 0;
8534 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8535 int cmp;
8536 int x;
8537
8538 /*
8539 * Find the function name in the table. Binary search.
8540 */
8541 while (first <= last)
8542 {
8543 x = first + ((unsigned)(last - first) >> 1);
8544 cmp = STRCMP(name, functions[x].f_name);
8545 if (cmp < 0)
8546 last = x - 1;
8547 else if (cmp > 0)
8548 first = x + 1;
8549 else
8550 return x;
8551 }
8552 return -1;
8553}
8554
8555/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008556 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8557 * name it contains, otherwise return "name".
8558 */
8559 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008560deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008561{
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008563 int cc;
8564
8565 cc = name[*lenp];
8566 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008567 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008568 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008569 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008572 {
8573 *lenp = 0;
8574 return (char_u *)""; /* just in case */
8575 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008576 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008577 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578 }
8579
8580 return name;
8581}
8582
8583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 * Allocate a variable for the result of a function.
8585 * Return OK or FAIL.
8586 */
8587 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008588get_func_tv(
8589 char_u *name, /* name of the function */
8590 int len, /* length of "name" */
8591 typval_T *rettv,
8592 char_u **arg, /* argument, pointing to the '(' */
8593 linenr_T firstline, /* first line of range */
8594 linenr_T lastline, /* last line of range */
8595 int *doesrange, /* return: function handled range */
8596 int evaluate,
8597 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599 char_u *argp;
8600 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008601 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 int argcount = 0; /* number of arguments found */
8603
8604 /*
8605 * Get the arguments.
8606 */
8607 argp = *arg;
8608 while (argcount < MAX_FUNC_ARGS)
8609 {
8610 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8611 if (*argp == ')' || *argp == ',' || *argp == NUL)
8612 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8614 {
8615 ret = FAIL;
8616 break;
8617 }
8618 ++argcount;
8619 if (*argp != ',')
8620 break;
8621 }
8622 if (*argp == ')')
8623 ++argp;
8624 else
8625 ret = FAIL;
8626
8627 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008628 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008629 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 {
8632 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008633 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008635 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
8638 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
8641 *arg = skipwhite(argp);
8642 return ret;
8643}
8644
8645
8646/*
8647 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008648 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008649 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008651 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008652call_func(
8653 char_u *funcname, /* name of the function */
8654 int len, /* length of "name" */
8655 typval_T *rettv, /* return value goes here */
8656 int argcount, /* number of "argvars" */
8657 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008658 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008659 linenr_T firstline, /* first line of range */
8660 linenr_T lastline, /* last line of range */
8661 int *doesrange, /* return: function handled range */
8662 int evaluate,
8663 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664{
8665 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666#define ERROR_UNKNOWN 0
8667#define ERROR_TOOMANY 1
8668#define ERROR_TOOFEW 2
8669#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008670#define ERROR_DICT 4
8671#define ERROR_NONE 5
8672#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 int error = ERROR_NONE;
8674 int i;
8675 int llen;
8676 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677#define FLEN_FIXED 40
8678 char_u fname_buf[FLEN_FIXED + 1];
8679 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008680 char_u *name;
8681
8682 /* Make a copy of the name, if it comes from a funcref variable it could
8683 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008684 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008685 if (name == NULL)
8686 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687
8688 /*
8689 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8690 * Change <SNR>123_name() to K_SNR 123_name().
8691 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 llen = eval_fname_script(name);
8694 if (llen > 0)
8695 {
8696 fname_buf[0] = K_SPECIAL;
8697 fname_buf[1] = KS_EXTRA;
8698 fname_buf[2] = (int)KE_SNR;
8699 i = 3;
8700 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8701 {
8702 if (current_SID <= 0)
8703 error = ERROR_SCRIPT;
8704 else
8705 {
8706 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8707 i = (int)STRLEN(fname_buf);
8708 }
8709 }
8710 if (i + STRLEN(name + llen) < FLEN_FIXED)
8711 {
8712 STRCPY(fname_buf + i, name + llen);
8713 fname = fname_buf;
8714 }
8715 else
8716 {
8717 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8718 if (fname == NULL)
8719 error = ERROR_OTHER;
8720 else
8721 {
8722 mch_memmove(fname, fname_buf, (size_t)i);
8723 STRCPY(fname + i, name + llen);
8724 }
8725 }
8726 }
8727 else
8728 fname = name;
8729
8730 *doesrange = FALSE;
8731
8732
8733 /* execute the function if no errors detected and executing */
8734 if (evaluate && error == ERROR_NONE)
8735 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008736 char_u *rfname = fname;
8737
8738 /* Ignore "g:" before a function name. */
8739 if (fname[0] == 'g' && fname[1] == ':')
8740 rfname = fname + 2;
8741
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008742 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8743 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 error = ERROR_UNKNOWN;
8745
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008746 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 {
8748 /*
8749 * User defined function.
8750 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008751 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008752
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008754 /* Trigger FuncUndefined event, may load the function. */
8755 if (fp == NULL
8756 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008757 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008758 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008760 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008761 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
8763#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008764 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008765 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008766 {
8767 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008768 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008769 }
8770
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 if (fp != NULL)
8772 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008773 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008775 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008777 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008779 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008780 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 else
8782 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008783 int did_save_redo = FALSE;
8784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 /*
8786 * Call the user function.
8787 * Save and restore search patterns, script variables and
8788 * redo buffer.
8789 */
8790 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008791#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008792 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008793#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008794 {
8795 saveRedobuff();
8796 did_save_redo = TRUE;
8797 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008798 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008800 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008801 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8802 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8803 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008804 /* Function was unreferenced while being used, free it
8805 * now. */
8806 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008807 if (did_save_redo)
8808 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 restore_search_patterns();
8810 error = ERROR_NONE;
8811 }
8812 }
8813 }
8814 else
8815 {
8816 /*
8817 * Find the function name in the table, call its implementation.
8818 */
8819 i = find_internal_func(fname);
8820 if (i >= 0)
8821 {
8822 if (argcount < functions[i].f_min_argc)
8823 error = ERROR_TOOFEW;
8824 else if (argcount > functions[i].f_max_argc)
8825 error = ERROR_TOOMANY;
8826 else
8827 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008828 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 error = ERROR_NONE;
8831 }
8832 }
8833 }
8834 /*
8835 * The function call (or "FuncUndefined" autocommand sequence) might
8836 * have been aborted by an error, an interrupt, or an explicitly thrown
8837 * exception that has not been caught so far. This situation can be
8838 * tested for by calling aborting(). For an error in an internal
8839 * function or for the "E132" error in call_user_func(), however, the
8840 * throw point at which the "force_abort" flag (temporarily reset by
8841 * emsg()) is normally updated has not been reached yet. We need to
8842 * update that flag first to make aborting() reliable.
8843 */
8844 update_force_abort();
8845 }
8846 if (error == ERROR_NONE)
8847 ret = OK;
8848
8849 /*
8850 * Report an error unless the argument evaluation or function call has been
8851 * cancelled due to an aborting error, an interrupt, or an exception.
8852 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008853 if (!aborting())
8854 {
8855 switch (error)
8856 {
8857 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008858 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008859 break;
8860 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008861 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008862 break;
8863 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008864 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008865 name);
8866 break;
8867 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008868 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008869 name);
8870 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008871 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008872 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008873 name);
8874 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008875 }
8876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 if (fname != name && fname != fname_buf)
8879 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008880 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
8882 return ret;
8883}
8884
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008885/*
8886 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008887 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008888 */
8889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008890emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008891{
8892 char_u *p;
8893
8894 if (*name == K_SPECIAL)
8895 p = concat_str((char_u *)"<SNR>", name + 3);
8896 else
8897 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008898 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008899 if (p != name)
8900 vim_free(p);
8901}
8902
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008903/*
8904 * Return TRUE for a non-zero Number and a non-empty String.
8905 */
8906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008907non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008908{
8909 return ((argvars[0].v_type == VAR_NUMBER
8910 && argvars[0].vval.v_number != 0)
8911 || (argvars[0].v_type == VAR_STRING
8912 && argvars[0].vval.v_string != NULL
8913 && *argvars[0].vval.v_string != NUL));
8914}
8915
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916/*********************************************
8917 * Implementation of the built-in functions
8918 */
8919
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008920#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008921static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008922
8923/*
8924 * Get the float value of "argvars[0]" into "f".
8925 * Returns FAIL when the argument is not a Number or Float.
8926 */
8927 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008928get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008929{
8930 if (argvars[0].v_type == VAR_FLOAT)
8931 {
8932 *f = argvars[0].vval.v_float;
8933 return OK;
8934 }
8935 if (argvars[0].v_type == VAR_NUMBER)
8936 {
8937 *f = (float_T)argvars[0].vval.v_number;
8938 return OK;
8939 }
8940 EMSG(_("E808: Number or Float required"));
8941 return FAIL;
8942}
8943
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944/*
8945 * "abs(expr)" function
8946 */
8947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008948f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008949{
8950 if (argvars[0].v_type == VAR_FLOAT)
8951 {
8952 rettv->v_type = VAR_FLOAT;
8953 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8954 }
8955 else
8956 {
8957 varnumber_T n;
8958 int error = FALSE;
8959
8960 n = get_tv_number_chk(&argvars[0], &error);
8961 if (error)
8962 rettv->vval.v_number = -1;
8963 else if (n > 0)
8964 rettv->vval.v_number = n;
8965 else
8966 rettv->vval.v_number = -n;
8967 }
8968}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008969
8970/*
8971 * "acos()" function
8972 */
8973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008974f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008975{
8976 float_T f;
8977
8978 rettv->v_type = VAR_FLOAT;
8979 if (get_float_arg(argvars, &f) == OK)
8980 rettv->vval.v_float = acos(f);
8981 else
8982 rettv->vval.v_float = 0.0;
8983}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008984#endif
8985
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008987 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 */
8989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008990f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008995 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008997 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008998 && !tv_check_lock(l->lv_lock,
8999 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009000 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009002 }
9003 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009004 EMSG(_(e_listreq));
9005}
9006
9007/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009008 * "alloc_fail(id, countdown, repeat)" function
9009 */
9010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009011f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009012{
9013 if (argvars[0].v_type != VAR_NUMBER
9014 || argvars[0].vval.v_number <= 0
9015 || argvars[1].v_type != VAR_NUMBER
9016 || argvars[1].vval.v_number < 0
9017 || argvars[2].v_type != VAR_NUMBER)
9018 EMSG(_(e_invarg));
9019 else
9020 {
9021 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009022 if (alloc_fail_id >= aid_last)
9023 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009024 alloc_fail_countdown = argvars[1].vval.v_number;
9025 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009026 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009027 }
9028}
9029
9030/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009031 * "and(expr, expr)" function
9032 */
9033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009034f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009035{
9036 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9037 & get_tv_number_chk(&argvars[1], NULL);
9038}
9039
9040/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009041 * "append(lnum, string/list)" function
9042 */
9043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009044f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009045{
9046 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009047 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 list_T *l = NULL;
9049 listitem_T *li = NULL;
9050 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009051 long added = 0;
9052
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009053 /* When coming here from Insert mode, sync undo, so that this can be
9054 * undone separately from what was previously inserted. */
9055 if (u_sync_once == 2)
9056 {
9057 u_sync_once = 1; /* notify that u_sync() was called */
9058 u_sync(TRUE);
9059 }
9060
Bram Moolenaar0d660222005-01-07 21:51:51 +00009061 lnum = get_tv_lnum(argvars);
9062 if (lnum >= 0
9063 && lnum <= curbuf->b_ml.ml_line_count
9064 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009065 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009066 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009067 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009068 l = argvars[1].vval.v_list;
9069 if (l == NULL)
9070 return;
9071 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009072 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009073 for (;;)
9074 {
9075 if (l == NULL)
9076 tv = &argvars[1]; /* append a string */
9077 else if (li == NULL)
9078 break; /* end of list */
9079 else
9080 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009081 line = get_tv_string_chk(tv);
9082 if (line == NULL) /* type error */
9083 {
9084 rettv->vval.v_number = 1; /* Failed */
9085 break;
9086 }
9087 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009088 ++added;
9089 if (l == NULL)
9090 break;
9091 li = li->li_next;
9092 }
9093
9094 appended_lines_mark(lnum, added);
9095 if (curwin->w_cursor.lnum > lnum)
9096 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098 else
9099 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100}
9101
9102/*
9103 * "argc()" function
9104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009106f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109}
9110
9111/*
9112 * "argidx()" function
9113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009115f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118}
9119
9120/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009121 * "arglistid()" function
9122 */
9123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009124f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009125{
9126 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009127
9128 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009129 wp = find_tabwin(&argvars[0], &argvars[1]);
9130 if (wp != NULL)
9131 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009132}
9133
9134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 * "argv(nr)" function
9136 */
9137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009138f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140 int idx;
9141
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009142 if (argvars[0].v_type != VAR_UNKNOWN)
9143 {
9144 idx = get_tv_number_chk(&argvars[0], NULL);
9145 if (idx >= 0 && idx < ARGCOUNT)
9146 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9147 else
9148 rettv->vval.v_string = NULL;
9149 rettv->v_type = VAR_STRING;
9150 }
9151 else if (rettv_list_alloc(rettv) == OK)
9152 for (idx = 0; idx < ARGCOUNT; ++idx)
9153 list_append_string(rettv->vval.v_list,
9154 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155}
9156
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009157static void prepare_assert_error(garray_T*gap);
9158static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9159static void assert_error(garray_T *gap);
9160static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009161
9162/*
9163 * Prepare "gap" for an assert error and add the sourcing position.
9164 */
9165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009166prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009167{
9168 char buf[NUMBUFLEN];
9169
9170 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009171 if (sourcing_name != NULL)
9172 {
9173 ga_concat(gap, sourcing_name);
9174 if (sourcing_lnum > 0)
9175 ga_concat(gap, (char_u *)" ");
9176 }
9177 if (sourcing_lnum > 0)
9178 {
9179 sprintf(buf, "line %ld", (long)sourcing_lnum);
9180 ga_concat(gap, (char_u *)buf);
9181 }
9182 if (sourcing_name != NULL || sourcing_lnum > 0)
9183 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009184}
9185
9186/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009187 * Append "str" to "gap", escaping unprintable characters.
9188 * Changes NL to \n, CR to \r, etc.
9189 */
9190 static void
9191ga_concat_esc(garray_T *gap, char_u *str)
9192{
9193 char_u *p;
9194 char_u buf[NUMBUFLEN];
9195
9196 for (p = str; *p != NUL; ++p)
9197 switch (*p)
9198 {
9199 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9200 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9201 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9202 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9203 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9204 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9205 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9206 default:
9207 if (*p < ' ')
9208 {
9209 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9210 ga_concat(gap, buf);
9211 }
9212 else
9213 ga_append(gap, *p);
9214 break;
9215 }
9216}
9217
9218/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009219 * Fill "gap" with information about an assert error.
9220 */
9221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009222fill_assert_error(
9223 garray_T *gap,
9224 typval_T *opt_msg_tv,
9225 char_u *exp_str,
9226 typval_T *exp_tv,
9227 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009228{
9229 char_u numbuf[NUMBUFLEN];
9230 char_u *tofree;
9231
9232 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9233 {
9234 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9235 vim_free(tofree);
9236 }
9237 else
9238 {
9239 ga_concat(gap, (char_u *)"Expected ");
9240 if (exp_str == NULL)
9241 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009242 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009243 vim_free(tofree);
9244 }
9245 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009246 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009248 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009249 vim_free(tofree);
9250 }
9251}
Bram Moolenaar43345542015-11-29 17:35:35 +01009252
9253/*
9254 * Add an assert error to v:errors.
9255 */
9256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009257assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009258{
9259 struct vimvar *vp = &vimvars[VV_ERRORS];
9260
9261 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9262 /* Make sure v:errors is a list. */
9263 set_vim_var_list(VV_ERRORS, list_alloc());
9264 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9265}
9266
Bram Moolenaar43345542015-11-29 17:35:35 +01009267/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009268 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009269 */
9270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009271f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009272{
9273 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009274
9275 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9276 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009277 prepare_assert_error(&ga);
9278 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9279 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009280 ga_clear(&ga);
9281 }
9282}
9283
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009284/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009285 * "assert_exception(string[, msg])" function
9286 */
9287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009288f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009289{
9290 garray_T ga;
9291 char *error;
9292
9293 error = (char *)get_tv_string_chk(&argvars[0]);
9294 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9295 {
9296 prepare_assert_error(&ga);
9297 ga_concat(&ga, (char_u *)"v:exception is not set");
9298 assert_error(&ga);
9299 ga_clear(&ga);
9300 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009301 else if (error != NULL
9302 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009303 {
9304 prepare_assert_error(&ga);
9305 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9306 &vimvars[VV_EXCEPTION].vv_tv);
9307 assert_error(&ga);
9308 ga_clear(&ga);
9309 }
9310}
9311
9312/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009313 * "assert_fails(cmd [, error])" function
9314 */
9315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009316f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009317{
9318 char_u *cmd = get_tv_string_chk(&argvars[0]);
9319 garray_T ga;
9320
9321 called_emsg = FALSE;
9322 suppress_errthrow = TRUE;
9323 emsg_silent = TRUE;
9324 do_cmdline_cmd(cmd);
9325 if (!called_emsg)
9326 {
9327 prepare_assert_error(&ga);
9328 ga_concat(&ga, (char_u *)"command did not fail: ");
9329 ga_concat(&ga, cmd);
9330 assert_error(&ga);
9331 ga_clear(&ga);
9332 }
9333 else if (argvars[1].v_type != VAR_UNKNOWN)
9334 {
9335 char_u buf[NUMBUFLEN];
9336 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9337
9338 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9339 {
9340 prepare_assert_error(&ga);
9341 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9342 &vimvars[VV_ERRMSG].vv_tv);
9343 assert_error(&ga);
9344 ga_clear(&ga);
9345 }
9346 }
9347
9348 called_emsg = FALSE;
9349 suppress_errthrow = FALSE;
9350 emsg_silent = FALSE;
9351 emsg_on_display = FALSE;
9352 set_vim_var_string(VV_ERRMSG, NULL, 0);
9353}
9354
9355/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009356 * Common for assert_true() and assert_false().
9357 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009359assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009360{
9361 int error = FALSE;
9362 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009363
Bram Moolenaar37127922016-02-06 20:29:28 +01009364 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009365 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009366 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009367 if (argvars[0].v_type != VAR_NUMBER
9368 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9369 || error)
9370 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009371 prepare_assert_error(&ga);
9372 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009373 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009374 NULL, &argvars[0]);
9375 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009376 ga_clear(&ga);
9377 }
9378}
9379
9380/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009381 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009382 */
9383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009384f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009385{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009386 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009387}
9388
9389/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009390 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009391 */
9392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009393f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009394{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009395 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009396}
9397
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009398#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009399/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009400 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009401 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009403f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009404{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009405 float_T f;
9406
9407 rettv->v_type = VAR_FLOAT;
9408 if (get_float_arg(argvars, &f) == OK)
9409 rettv->vval.v_float = asin(f);
9410 else
9411 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009412}
9413
9414/*
9415 * "atan()" function
9416 */
9417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009418f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009419{
9420 float_T f;
9421
9422 rettv->v_type = VAR_FLOAT;
9423 if (get_float_arg(argvars, &f) == OK)
9424 rettv->vval.v_float = atan(f);
9425 else
9426 rettv->vval.v_float = 0.0;
9427}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009428
9429/*
9430 * "atan2()" function
9431 */
9432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009433f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009434{
9435 float_T fx, fy;
9436
9437 rettv->v_type = VAR_FLOAT;
9438 if (get_float_arg(argvars, &fx) == OK
9439 && get_float_arg(&argvars[1], &fy) == OK)
9440 rettv->vval.v_float = atan2(fx, fy);
9441 else
9442 rettv->vval.v_float = 0.0;
9443}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009444#endif
9445
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446/*
9447 * "browse(save, title, initdir, default)" function
9448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009450f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452#ifdef FEAT_BROWSE
9453 int save;
9454 char_u *title;
9455 char_u *initdir;
9456 char_u *defname;
9457 char_u buf[NUMBUFLEN];
9458 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009459 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009461 save = get_tv_number_chk(&argvars[0], &error);
9462 title = get_tv_string_chk(&argvars[1]);
9463 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9464 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009466 if (error || title == NULL || initdir == NULL || defname == NULL)
9467 rettv->vval.v_string = NULL;
9468 else
9469 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009470 do_browse(save ? BROWSE_SAVE : 0,
9471 title, defname, NULL, initdir, NULL, curbuf);
9472#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009474#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009476}
9477
9478/*
9479 * "browsedir(title, initdir)" function
9480 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009483{
9484#ifdef FEAT_BROWSE
9485 char_u *title;
9486 char_u *initdir;
9487 char_u buf[NUMBUFLEN];
9488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009489 title = get_tv_string_chk(&argvars[0]);
9490 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009492 if (title == NULL || initdir == NULL)
9493 rettv->vval.v_string = NULL;
9494 else
9495 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009496 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501}
9502
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009503static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009504
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505/*
9506 * Find a buffer by number or exact name.
9507 */
9508 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009509find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
9511 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 if (avar->v_type == VAR_NUMBER)
9514 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009515 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009517 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009518 if (buf == NULL)
9519 {
9520 /* No full path name match, try a match with a URL or a "nofile"
9521 * buffer, these don't use the full path. */
9522 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9523 if (buf->b_fname != NULL
9524 && (path_with_url(buf->b_fname)
9525#ifdef FEAT_QUICKFIX
9526 || bt_nofile(buf)
9527#endif
9528 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009529 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009530 break;
9531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 }
9533 return buf;
9534}
9535
9536/*
9537 * "bufexists(expr)" function
9538 */
9539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009540f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543}
9544
9545/*
9546 * "buflisted(expr)" function
9547 */
9548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009549f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551 buf_T *buf;
9552
9553 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555}
9556
9557/*
9558 * "bufloaded(expr)" function
9559 */
9560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009561f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563 buf_T *buf;
9564
9565 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567}
9568
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009569static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009570
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571/*
9572 * Get buffer by number or pattern.
9573 */
9574 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009575get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 int save_magic;
9579 char_u *save_cpo;
9580 buf_T *buf;
9581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 if (tv->v_type == VAR_NUMBER)
9583 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009584 if (tv->v_type != VAR_STRING)
9585 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (name == NULL || *name == NUL)
9587 return curbuf;
9588 if (name[0] == '$' && name[1] == NUL)
9589 return lastbuf;
9590
9591 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9592 save_magic = p_magic;
9593 p_magic = TRUE;
9594 save_cpo = p_cpo;
9595 p_cpo = (char_u *)"";
9596
9597 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009598 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599
9600 p_magic = save_magic;
9601 p_cpo = save_cpo;
9602
9603 /* If not found, try expanding the name, like done for bufexists(). */
9604 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
9607 return buf;
9608}
9609
9610/*
9611 * "bufname(expr)" function
9612 */
9613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009614f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616 buf_T *buf;
9617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009618 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009620 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 --emsg_off;
9627}
9628
9629/*
9630 * "bufnr(expr)" function
9631 */
9632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009633f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009636 int error = FALSE;
9637 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009641 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009642 --emsg_off;
9643
9644 /* If the buffer isn't found and the second argument is not zero create a
9645 * new buffer. */
9646 if (buf == NULL
9647 && argvars[1].v_type != VAR_UNKNOWN
9648 && get_tv_number_chk(&argvars[1], &error) != 0
9649 && !error
9650 && (name = get_tv_string_chk(&argvars[0])) != NULL
9651 && !error)
9652 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9653
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658}
9659
9660/*
9661 * "bufwinnr(nr)" function
9662 */
9663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009664f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
9666#ifdef FEAT_WINDOWS
9667 win_T *wp;
9668 int winnr = 0;
9669#endif
9670 buf_T *buf;
9671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009672 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009674 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675#ifdef FEAT_WINDOWS
9676 for (wp = firstwin; wp; wp = wp->w_next)
9677 {
9678 ++winnr;
9679 if (wp->w_buffer == buf)
9680 break;
9681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685#endif
9686 --emsg_off;
9687}
9688
9689/*
9690 * "byte2line(byte)" function
9691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009693f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697#else
9698 long boff = 0;
9699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009700 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 (linenr_T)0, &boff);
9706#endif
9707}
9708
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009710byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009711{
9712#ifdef FEAT_MBYTE
9713 char_u *t;
9714#endif
9715 char_u *str;
9716 long idx;
9717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009718 str = get_tv_string_chk(&argvars[0]);
9719 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009721 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009722 return;
9723
9724#ifdef FEAT_MBYTE
9725 t = str;
9726 for ( ; idx > 0; idx--)
9727 {
9728 if (*t == NUL) /* EOL reached */
9729 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009730 if (enc_utf8 && comp)
9731 t += utf_ptr2len(t);
9732 else
9733 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009734 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009735 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009736#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009737 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009739#endif
9740}
9741
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009742/*
9743 * "byteidx()" function
9744 */
9745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009746f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009747{
9748 byteidx(argvars, rettv, FALSE);
9749}
9750
9751/*
9752 * "byteidxcomp()" function
9753 */
9754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009755f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009756{
9757 byteidx(argvars, rettv, TRUE);
9758}
9759
Bram Moolenaardb913952012-06-29 12:54:53 +02009760 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009761func_call(
9762 char_u *name,
9763 typval_T *args,
9764 dict_T *selfdict,
9765 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009766{
9767 listitem_T *item;
9768 typval_T argv[MAX_FUNC_ARGS + 1];
9769 int argc = 0;
9770 int dummy;
9771 int r = 0;
9772
9773 for (item = args->vval.v_list->lv_first; item != NULL;
9774 item = item->li_next)
9775 {
9776 if (argc == MAX_FUNC_ARGS)
9777 {
9778 EMSG(_("E699: Too many arguments"));
9779 break;
9780 }
9781 /* Make a copy of each argument. This is needed to be able to set
9782 * v_lock to VAR_FIXED in the copy without changing the original list.
9783 */
9784 copy_tv(&item->li_tv, &argv[argc++]);
9785 }
9786
9787 if (item == NULL)
9788 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9789 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9790 &dummy, TRUE, selfdict);
9791
9792 /* Free the arguments. */
9793 while (argc > 0)
9794 clear_tv(&argv[--argc]);
9795
9796 return r;
9797}
9798
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009799/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009800 * "call(func, arglist)" function
9801 */
9802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009803f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009804{
9805 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009807
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808 if (argvars[1].v_type != VAR_LIST)
9809 {
9810 EMSG(_(e_listreq));
9811 return;
9812 }
9813 if (argvars[1].vval.v_list == NULL)
9814 return;
9815
9816 if (argvars[0].v_type == VAR_FUNC)
9817 func = argvars[0].vval.v_string;
9818 else
9819 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009820 if (*func == NUL)
9821 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009822
Bram Moolenaare9a41262005-01-15 22:18:47 +00009823 if (argvars[2].v_type != VAR_UNKNOWN)
9824 {
9825 if (argvars[2].v_type != VAR_DICT)
9826 {
9827 EMSG(_(e_dictreq));
9828 return;
9829 }
9830 selfdict = argvars[2].vval.v_dict;
9831 }
9832
Bram Moolenaardb913952012-06-29 12:54:53 +02009833 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009834}
9835
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009836#ifdef FEAT_FLOAT
9837/*
9838 * "ceil({float})" function
9839 */
9840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009841f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009842{
9843 float_T f;
9844
9845 rettv->v_type = VAR_FLOAT;
9846 if (get_float_arg(argvars, &f) == OK)
9847 rettv->vval.v_float = ceil(f);
9848 else
9849 rettv->vval.v_float = 0.0;
9850}
9851#endif
9852
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009853#ifdef FEAT_CHANNEL
9854/*
Bram Moolenaar77073442016-02-13 23:23:53 +01009855 * Get the channel from the argument.
9856 * Returns NULL if the handle is invalid.
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009857 */
Bram Moolenaar77073442016-02-13 23:23:53 +01009858 static channel_T *
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009859get_channel_arg(typval_T *tv)
9860{
Bram Moolenaar77073442016-02-13 23:23:53 +01009861 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009862
Bram Moolenaar77073442016-02-13 23:23:53 +01009863 if (tv->v_type != VAR_CHANNEL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009864 {
9865 EMSG2(_(e_invarg2), get_tv_string(tv));
Bram Moolenaar77073442016-02-13 23:23:53 +01009866 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009867 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009868 channel = tv->vval.v_channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009869
Bram Moolenaar77073442016-02-13 23:23:53 +01009870 if (channel == NULL || !channel_is_open(channel))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009871 {
Bram Moolenaar77073442016-02-13 23:23:53 +01009872 EMSG(_("E906: not an open channel"));
9873 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009874 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009875 return channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009876}
9877
9878/*
9879 * "ch_close()" function
9880 */
9881 static void
9882f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9883{
Bram Moolenaar77073442016-02-13 23:23:53 +01009884 channel_T *channel = get_channel_arg(&argvars[0]);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009885
Bram Moolenaar77073442016-02-13 23:23:53 +01009886 if (channel != NULL)
9887 channel_close(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009888}
9889
9890/*
9891 * Get a callback from "arg". It can be a Funcref or a function name.
9892 * When "arg" is zero return an empty string.
9893 * Return NULL for an invalid argument.
9894 */
9895 static char_u *
9896get_callback(typval_T *arg)
9897{
9898 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9899 return arg->vval.v_string;
9900 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9901 return (char_u *)"";
9902 EMSG(_("E999: Invalid callback argument"));
9903 return NULL;
9904}
9905
9906/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009907 * "ch_logfile()" function
9908 */
9909 static void
9910f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9911{
9912 char_u *fname;
9913 char_u *opt = (char_u *)"";
9914 char_u buf[NUMBUFLEN];
9915 FILE *file = NULL;
9916
9917 fname = get_tv_string(&argvars[0]);
9918 if (argvars[1].v_type == VAR_STRING)
9919 opt = get_tv_string_buf(&argvars[1], buf);
9920 if (*fname != NUL)
9921 {
9922 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
9923 if (file == NULL)
9924 {
9925 EMSG2(_(e_notopen), fname);
9926 return;
9927 }
9928 }
9929 ch_logfile(file);
9930}
9931
9932/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009933 * "ch_open()" function
9934 */
9935 static void
9936f_ch_open(typval_T *argvars, typval_T *rettv)
9937{
9938 char_u *address;
9939 char_u *mode;
9940 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009941 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009942 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009943 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009944 int waittime = 0;
9945 int timeout = 2000;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009946 ch_mode_T ch_mode = MODE_JSON;
Bram Moolenaar77073442016-02-13 23:23:53 +01009947 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009948
9949 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +01009950 rettv->v_type = VAR_CHANNEL;
9951 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009952
9953 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009954 if (argvars[1].v_type != VAR_UNKNOWN
9955 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009956 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009957 EMSG(_(e_invarg));
9958 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009959 }
9960
9961 /* parse address */
9962 p = vim_strchr(address, ':');
9963 if (p == NULL)
9964 {
9965 EMSG2(_(e_invarg2), address);
9966 return;
9967 }
9968 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009969 port = strtol((char *)p, &rest, 10);
9970 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009971 {
9972 p[-1] = ':';
9973 EMSG2(_(e_invarg2), address);
9974 return;
9975 }
9976
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009977 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009978 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009979 dict_T *dict = argvars[1].vval.v_dict;
9980 dictitem_T *item;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009981
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009982 /* parse argdict */
9983 if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009984 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009985 mode = get_tv_string(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009986 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009987 ch_mode = MODE_RAW;
9988 else if (STRCMP(mode, "js") == 0)
9989 ch_mode = MODE_JS;
9990 else if (STRCMP(mode, "json") == 0)
9991 ch_mode = MODE_JSON;
9992 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009993 {
9994 EMSG2(_(e_invarg2), mode);
9995 return;
9996 }
9997 }
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009998 if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
9999 waittime = get_tv_number(&item->di_tv);
10000 if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
10001 timeout = get_tv_number(&item->di_tv);
10002 if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
10003 callback = get_callback(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010004 }
10005 if (waittime < 0 || timeout < 0)
10006 {
10007 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010008 return;
10009 }
10010
Bram Moolenaar77073442016-02-13 23:23:53 +010010011 channel = channel_open((char *)address, port, waittime, NULL);
10012 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010013 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010014 rettv->vval.v_channel = channel;
Bram Moolenaar77073442016-02-13 23:23:53 +010010015 channel_set_json_mode(channel, ch_mode);
10016 channel_set_timeout(channel, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010017 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010018 channel_set_callback(channel, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010019 }
10020}
10021
10022/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010023 * "ch_readraw()" function
10024 */
10025 static void
10026f_ch_readraw(typval_T *argvars, typval_T *rettv)
10027{
Bram Moolenaar77073442016-02-13 23:23:53 +010010028 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010029
10030 /* return an empty string by default */
10031 rettv->v_type = VAR_STRING;
10032 rettv->vval.v_string = NULL;
10033
Bram Moolenaar77073442016-02-13 23:23:53 +010010034 channel = get_channel_arg(&argvars[0]);
10035 if (channel != NULL)
10036 rettv->vval.v_string = channel_read_block(channel);
10037}
10038
10039/*
10040 * "ch_status()" function
10041 */
10042 static void
10043f_ch_status(typval_T *argvars, typval_T *rettv)
10044{
10045 /* return an empty string by default */
10046 rettv->v_type = VAR_STRING;
10047
10048 if (argvars[0].v_type != VAR_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010049 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010050 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10051 rettv->vval.v_string = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010052 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010053 else
10054 rettv->vval.v_string = vim_strsave(
10055 (char_u *)channel_status(argvars[0].vval.v_channel));
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010056}
10057
10058/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010059 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010060 * Returns the channel if the caller should read the response.
10061 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010062 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010063 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010064send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010065{
Bram Moolenaar77073442016-02-13 23:23:53 +010010066 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010067 char_u *callback = NULL;
10068
Bram Moolenaar77073442016-02-13 23:23:53 +010010069 channel = get_channel_arg(&argvars[0]);
10070 if (channel == NULL)
10071 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010072
10073 if (argvars[2].v_type != VAR_UNKNOWN)
10074 {
10075 callback = get_callback(&argvars[2]);
10076 if (callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010077 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010078 }
Bram Moolenaara07fec92016-02-05 21:04:08 +010010079 /* Set the callback. An empty callback means no callback and not reading
10080 * the response. */
10081 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010082 channel_set_req_callback(channel, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010083
Bram Moolenaar77073442016-02-13 23:23:53 +010010084 if (channel_send(channel, text, fun) == OK && callback == NULL)
10085 return channel;
10086 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010087}
10088
10089/*
10090 * "ch_sendexpr()" function
10091 */
10092 static void
10093f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10094{
10095 char_u *text;
10096 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010097 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010098 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010099 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010100
10101 /* return an empty string by default */
10102 rettv->v_type = VAR_STRING;
10103 rettv->vval.v_string = NULL;
10104
Bram Moolenaar77073442016-02-13 23:23:53 +010010105 channel = get_channel_arg(&argvars[0]);
10106 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010107 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010108
Bram Moolenaar77073442016-02-13 23:23:53 +010010109 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010110 if (ch_mode == MODE_RAW)
10111 {
10112 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10113 return;
10114 }
10115
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010116 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010117 text = json_encode_nr_expr(id, &argvars[1],
10118 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010119 if (text == NULL)
10120 return;
10121
Bram Moolenaar77073442016-02-13 23:23:53 +010010122 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010123 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010124 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010125 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010126 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010127 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010128 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010129
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010130 /* Move the item from the list and then change the type to
10131 * avoid the value being freed. */
10132 *rettv = list->lv_last->li_tv;
10133 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010134 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010135 }
10136 }
10137}
10138
10139/*
10140 * "ch_sendraw()" function
10141 */
10142 static void
10143f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10144{
10145 char_u buf[NUMBUFLEN];
10146 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010147 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010148
10149 /* return an empty string by default */
10150 rettv->v_type = VAR_STRING;
10151 rettv->vval.v_string = NULL;
10152
10153 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010154 channel = send_common(argvars, text, 0, "sendraw");
10155 if (channel != NULL)
10156 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010157}
10158#endif
10159
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010160/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010161 * "changenr()" function
10162 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010164f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010165{
10166 rettv->vval.v_number = curbuf->b_u_seq_cur;
10167}
10168
10169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 * "char2nr(string)" function
10171 */
10172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010173f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174{
10175#ifdef FEAT_MBYTE
10176 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010177 {
10178 int utf8 = 0;
10179
10180 if (argvars[1].v_type != VAR_UNKNOWN)
10181 utf8 = get_tv_number_chk(&argvars[1], NULL);
10182
10183 if (utf8)
10184 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10185 else
10186 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 else
10189#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191}
10192
10193/*
10194 * "cindent(lnum)" function
10195 */
10196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010197f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198{
10199#ifdef FEAT_CINDENT
10200 pos_T pos;
10201 linenr_T lnum;
10202
10203 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010204 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10206 {
10207 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 curwin->w_cursor = pos;
10210 }
10211 else
10212#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214}
10215
10216/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010217 * "clearmatches()" function
10218 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010220f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010221{
10222#ifdef FEAT_SEARCH_EXTRA
10223 clear_matches(curwin);
10224#endif
10225}
10226
10227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 * "col(string)" function
10229 */
10230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010231f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232{
10233 colnr_T col = 0;
10234 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010235 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010237 fp = var2fpos(&argvars[0], FALSE, &fnum);
10238 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 {
10240 if (fp->col == MAXCOL)
10241 {
10242 /* '> can be MAXCOL, get the length of the line then */
10243 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010244 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 else
10246 col = MAXCOL;
10247 }
10248 else
10249 {
10250 col = fp->col + 1;
10251#ifdef FEAT_VIRTUALEDIT
10252 /* col(".") when the cursor is on the NUL at the end of the line
10253 * because of "coladd" can be seen as an extra column. */
10254 if (virtual_active() && fp == &curwin->w_cursor)
10255 {
10256 char_u *p = ml_get_cursor();
10257
10258 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10259 curwin->w_virtcol - curwin->w_cursor.coladd))
10260 {
10261# ifdef FEAT_MBYTE
10262 int l;
10263
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010264 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 col += l;
10266# else
10267 if (*p != NUL && p[1] == NUL)
10268 ++col;
10269# endif
10270 }
10271 }
10272#endif
10273 }
10274 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276}
10277
Bram Moolenaar572cb562005-08-05 21:35:02 +000010278#if defined(FEAT_INS_EXPAND)
10279/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010280 * "complete()" function
10281 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010283f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010284{
10285 int startcol;
10286
10287 if ((State & INSERT) == 0)
10288 {
10289 EMSG(_("E785: complete() can only be used in Insert mode"));
10290 return;
10291 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010292
10293 /* Check for undo allowed here, because if something was already inserted
10294 * the line was already saved for undo and this check isn't done. */
10295 if (!undo_allowed())
10296 return;
10297
Bram Moolenaarade00832006-03-10 21:46:58 +000010298 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10299 {
10300 EMSG(_(e_invarg));
10301 return;
10302 }
10303
10304 startcol = get_tv_number_chk(&argvars[0], NULL);
10305 if (startcol <= 0)
10306 return;
10307
10308 set_completion(startcol - 1, argvars[1].vval.v_list);
10309}
10310
10311/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010312 * "complete_add()" function
10313 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010315f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010316{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010317 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010318}
10319
10320/*
10321 * "complete_check()" function
10322 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010324f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010325{
10326 int saved = RedrawingDisabled;
10327
10328 RedrawingDisabled = 0;
10329 ins_compl_check_keys(0);
10330 rettv->vval.v_number = compl_interrupted;
10331 RedrawingDisabled = saved;
10332}
10333#endif
10334
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335/*
10336 * "confirm(message, buttons[, default [, type]])" function
10337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010339f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340{
10341#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10342 char_u *message;
10343 char_u *buttons = NULL;
10344 char_u buf[NUMBUFLEN];
10345 char_u buf2[NUMBUFLEN];
10346 int def = 1;
10347 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010348 char_u *typestr;
10349 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010351 message = get_tv_string_chk(&argvars[0]);
10352 if (message == NULL)
10353 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010354 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010356 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10357 if (buttons == NULL)
10358 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010359 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010361 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010362 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010364 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10365 if (typestr == NULL)
10366 error = TRUE;
10367 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010369 switch (TOUPPER_ASC(*typestr))
10370 {
10371 case 'E': type = VIM_ERROR; break;
10372 case 'Q': type = VIM_QUESTION; break;
10373 case 'I': type = VIM_INFO; break;
10374 case 'W': type = VIM_WARNING; break;
10375 case 'G': type = VIM_GENERIC; break;
10376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 }
10378 }
10379 }
10380 }
10381
10382 if (buttons == NULL || *buttons == NUL)
10383 buttons = (char_u *)_("&Ok");
10384
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010385 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010387 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388#endif
10389}
10390
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010391/*
10392 * "copy()" function
10393 */
10394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010395f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010396{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010397 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010398}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010400#ifdef FEAT_FLOAT
10401/*
10402 * "cos()" function
10403 */
10404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010405f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010406{
10407 float_T f;
10408
10409 rettv->v_type = VAR_FLOAT;
10410 if (get_float_arg(argvars, &f) == OK)
10411 rettv->vval.v_float = cos(f);
10412 else
10413 rettv->vval.v_float = 0.0;
10414}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010415
10416/*
10417 * "cosh()" function
10418 */
10419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010420f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010421{
10422 float_T f;
10423
10424 rettv->v_type = VAR_FLOAT;
10425 if (get_float_arg(argvars, &f) == OK)
10426 rettv->vval.v_float = cosh(f);
10427 else
10428 rettv->vval.v_float = 0.0;
10429}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010430#endif
10431
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010433 * "count()" function
10434 */
10435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010436f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010437{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010438 long n = 0;
10439 int ic = FALSE;
10440
Bram Moolenaare9a41262005-01-15 22:18:47 +000010441 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010442 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010443 listitem_T *li;
10444 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010445 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010446
Bram Moolenaare9a41262005-01-15 22:18:47 +000010447 if ((l = argvars[0].vval.v_list) != NULL)
10448 {
10449 li = l->lv_first;
10450 if (argvars[2].v_type != VAR_UNKNOWN)
10451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 int error = FALSE;
10453
10454 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010455 if (argvars[3].v_type != VAR_UNKNOWN)
10456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 idx = get_tv_number_chk(&argvars[3], &error);
10458 if (!error)
10459 {
10460 li = list_find(l, idx);
10461 if (li == NULL)
10462 EMSGN(_(e_listidx), idx);
10463 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010464 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010465 if (error)
10466 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010467 }
10468
10469 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010470 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 ++n;
10472 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010473 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010474 else if (argvars[0].v_type == VAR_DICT)
10475 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 int todo;
10477 dict_T *d;
10478 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010479
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010480 if ((d = argvars[0].vval.v_dict) != NULL)
10481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 int error = FALSE;
10483
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 if (argvars[2].v_type != VAR_UNKNOWN)
10485 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010487 if (argvars[3].v_type != VAR_UNKNOWN)
10488 EMSG(_(e_invarg));
10489 }
10490
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010491 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010492 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010493 {
10494 if (!HASHITEM_EMPTY(hi))
10495 {
10496 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010497 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010498 ++n;
10499 }
10500 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010501 }
10502 }
10503 else
10504 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010505 rettv->vval.v_number = n;
10506}
10507
10508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10510 *
10511 * Checks the existence of a cscope connection.
10512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010514f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515{
10516#ifdef FEAT_CSCOPE
10517 int num = 0;
10518 char_u *dbpath = NULL;
10519 char_u *prepend = NULL;
10520 char_u buf[NUMBUFLEN];
10521
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010522 if (argvars[0].v_type != VAR_UNKNOWN
10523 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525 num = (int)get_tv_number(&argvars[0]);
10526 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 }
10530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532#endif
10533}
10534
10535/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010536 * "cursor(lnum, col)" function, or
10537 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010539 * Moves the cursor to the specified line and column.
10540 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010543f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544{
10545 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010546#ifdef FEAT_VIRTUALEDIT
10547 long coladd = 0;
10548#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010549 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010551 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010552 if (argvars[1].v_type == VAR_UNKNOWN)
10553 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010554 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010555 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010556
Bram Moolenaar493c1782014-05-28 14:34:46 +020010557 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010558 {
10559 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010560 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010561 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010562 line = pos.lnum;
10563 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010564#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010565 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010566#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010567 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010568 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010569 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010570 set_curswant = FALSE;
10571 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010572 }
10573 else
10574 {
10575 line = get_tv_lnum(argvars);
10576 col = get_tv_number_chk(&argvars[1], NULL);
10577#ifdef FEAT_VIRTUALEDIT
10578 if (argvars[2].v_type != VAR_UNKNOWN)
10579 coladd = get_tv_number_chk(&argvars[2], NULL);
10580#endif
10581 }
10582 if (line < 0 || col < 0
10583#ifdef FEAT_VIRTUALEDIT
10584 || coladd < 0
10585#endif
10586 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 if (line > 0)
10589 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 if (col > 0)
10591 curwin->w_cursor.col = col - 1;
10592#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010593 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594#endif
10595
10596 /* Make sure the cursor is in a valid position. */
10597 check_cursor();
10598#ifdef FEAT_MBYTE
10599 /* Correct cursor for multi-byte character. */
10600 if (has_mbyte)
10601 mb_adjust_cursor();
10602#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010603
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010604 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010605 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606}
10607
10608/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010609 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 */
10611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010612f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010614 int noref = 0;
10615
10616 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010618 if (noref < 0 || noref > 1)
10619 EMSG(_(e_invarg));
10620 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010621 {
10622 current_copyID += COPYID_INC;
10623 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625}
10626
10627/*
10628 * "delete()" function
10629 */
10630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010631f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010633 char_u nbuf[NUMBUFLEN];
10634 char_u *name;
10635 char_u *flags;
10636
10637 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010639 return;
10640
10641 name = get_tv_string(&argvars[0]);
10642 if (name == NULL || *name == NUL)
10643 {
10644 EMSG(_(e_invarg));
10645 return;
10646 }
10647
10648 if (argvars[1].v_type != VAR_UNKNOWN)
10649 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010651 flags = (char_u *)"";
10652
10653 if (*flags == NUL)
10654 /* delete a file */
10655 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10656 else if (STRCMP(flags, "d") == 0)
10657 /* delete an empty directory */
10658 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10659 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010660 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010661 rettv->vval.v_number = delete_recursive(name);
10662 else
10663 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664}
10665
10666/*
10667 * "did_filetype()" function
10668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010670f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671{
10672#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674#endif
10675}
10676
10677/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010678 * "diff_filler()" function
10679 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010681f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010682{
10683#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010685#endif
10686}
10687
10688/*
10689 * "diff_hlID()" function
10690 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010692f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010693{
10694#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010696 static linenr_T prev_lnum = 0;
10697 static int changedtick = 0;
10698 static int fnum = 0;
10699 static int change_start = 0;
10700 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010701 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010702 int filler_lines;
10703 int col;
10704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 if (lnum < 0) /* ignore type error in {lnum} arg */
10706 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010707 if (lnum != prev_lnum
10708 || changedtick != curbuf->b_changedtick
10709 || fnum != curbuf->b_fnum)
10710 {
10711 /* New line, buffer, change: need to get the values. */
10712 filler_lines = diff_check(curwin, lnum);
10713 if (filler_lines < 0)
10714 {
10715 if (filler_lines == -1)
10716 {
10717 change_start = MAXCOL;
10718 change_end = -1;
10719 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10720 hlID = HLF_ADD; /* added line */
10721 else
10722 hlID = HLF_CHD; /* changed line */
10723 }
10724 else
10725 hlID = HLF_ADD; /* added line */
10726 }
10727 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010728 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010729 prev_lnum = lnum;
10730 changedtick = curbuf->b_changedtick;
10731 fnum = curbuf->b_fnum;
10732 }
10733
10734 if (hlID == HLF_CHD || hlID == HLF_TXD)
10735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010737 if (col >= change_start && col <= change_end)
10738 hlID = HLF_TXD; /* changed text */
10739 else
10740 hlID = HLF_CHD; /* changed line */
10741 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010742 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010743#endif
10744}
10745
10746/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010747 * "disable_char_avail_for_testing({expr})" function
10748 */
10749 static void
10750f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10751{
10752 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10753}
10754
10755/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010756 * "empty({expr})" function
10757 */
10758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010759f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010760{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010761 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010762
10763 switch (argvars[0].v_type)
10764 {
10765 case VAR_STRING:
10766 case VAR_FUNC:
10767 n = argvars[0].vval.v_string == NULL
10768 || *argvars[0].vval.v_string == NUL;
10769 break;
10770 case VAR_NUMBER:
10771 n = argvars[0].vval.v_number == 0;
10772 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010773 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010774#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010775 n = argvars[0].vval.v_float == 0.0;
10776 break;
10777#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010778 case VAR_LIST:
10779 n = argvars[0].vval.v_list == NULL
10780 || argvars[0].vval.v_list->lv_first == NULL;
10781 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 case VAR_DICT:
10783 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010785 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010786 case VAR_SPECIAL:
10787 n = argvars[0].vval.v_number != VVAL_TRUE;
10788 break;
10789
Bram Moolenaar835dc632016-02-07 14:27:38 +010010790 case VAR_JOB:
10791#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010792 n = argvars[0].vval.v_job == NULL
10793 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10794 break;
10795#endif
10796 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010797#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010798 n = argvars[0].vval.v_channel == NULL
10799 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010800 break;
10801#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010802 case VAR_UNKNOWN:
10803 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10804 n = TRUE;
10805 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010806 }
10807
10808 rettv->vval.v_number = n;
10809}
10810
10811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 * "escape({string}, {chars})" function
10813 */
10814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010815f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816{
10817 char_u buf[NUMBUFLEN];
10818
Bram Moolenaar758711c2005-02-02 23:11:38 +000010819 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10820 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822}
10823
10824/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010825 * "eval()" function
10826 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010828f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010829{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010830 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010832 s = get_tv_string_chk(&argvars[0]);
10833 if (s != NULL)
10834 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010835
Bram Moolenaar615b9972015-01-14 17:15:05 +010010836 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010837 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10838 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010839 if (p != NULL && !aborting())
10840 EMSG2(_(e_invexpr2), p);
10841 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010843 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010844 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010845 else if (*s != NUL)
10846 EMSG(_(e_trailing));
10847}
10848
10849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 * "eventhandler()" function
10851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010853f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856}
10857
10858/*
10859 * "executable()" function
10860 */
10861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010862f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010864 char_u *name = get_tv_string(&argvars[0]);
10865
10866 /* Check in $PATH and also check directly if there is a directory name. */
10867 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10868 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010869}
10870
10871/*
10872 * "exepath()" function
10873 */
10874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010875f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010876{
10877 char_u *p = NULL;
10878
Bram Moolenaarb5971142015-03-21 17:32:19 +010010879 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010880 rettv->v_type = VAR_STRING;
10881 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882}
10883
10884/*
10885 * "exists()" function
10886 */
10887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010888f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889{
10890 char_u *p;
10891 char_u *name;
10892 int n = FALSE;
10893 int len = 0;
10894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 if (*p == '$') /* environment variable */
10897 {
10898 /* first try "normal" environment variables (fast) */
10899 if (mch_getenv(p + 1) != NULL)
10900 n = TRUE;
10901 else
10902 {
10903 /* try expanding things like $VIM and ${HOME} */
10904 p = expand_env_save(p);
10905 if (p != NULL && *p != '$')
10906 n = TRUE;
10907 vim_free(p);
10908 }
10909 }
10910 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010913 if (*skipwhite(p) != NUL)
10914 n = FALSE; /* trailing garbage */
10915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 else if (*p == '*') /* internal or user defined function */
10917 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010918 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 }
10920 else if (*p == ':')
10921 {
10922 n = cmd_exists(p + 1);
10923 }
10924 else if (*p == '#')
10925 {
10926#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010927 if (p[1] == '#')
10928 n = autocmd_supported(p + 2);
10929 else
10930 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931#endif
10932 }
10933 else /* internal variable */
10934 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010935 char_u *tofree;
10936 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010938 /* get_name_len() takes care of expanding curly braces */
10939 name = p;
10940 len = get_name_len(&p, &tofree, TRUE, FALSE);
10941 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010943 if (tofree != NULL)
10944 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010945 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010946 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010948 /* handle d.key, l[idx], f(expr) */
10949 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10950 if (n)
10951 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 }
10953 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010954 if (*p != NUL)
10955 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010957 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 }
10959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961}
10962
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010963#ifdef FEAT_FLOAT
10964/*
10965 * "exp()" function
10966 */
10967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010968f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010969{
10970 float_T f;
10971
10972 rettv->v_type = VAR_FLOAT;
10973 if (get_float_arg(argvars, &f) == OK)
10974 rettv->vval.v_float = exp(f);
10975 else
10976 rettv->vval.v_float = 0.0;
10977}
10978#endif
10979
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980/*
10981 * "expand()" function
10982 */
10983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010984f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985{
10986 char_u *s;
10987 int len;
10988 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010989 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010992 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010994 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010995 if (argvars[1].v_type != VAR_UNKNOWN
10996 && argvars[2].v_type != VAR_UNKNOWN
10997 && get_tv_number_chk(&argvars[2], &error)
10998 && !error)
10999 {
11000 rettv->v_type = VAR_LIST;
11001 rettv->vval.v_list = NULL;
11002 }
11003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 if (*s == '%' || *s == '#' || *s == '<')
11006 {
11007 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011008 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011010 if (rettv->v_type == VAR_LIST)
11011 {
11012 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11013 list_append_string(rettv->vval.v_list, result, -1);
11014 else
11015 vim_free(result);
11016 }
11017 else
11018 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 }
11020 else
11021 {
11022 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011023 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 if (argvars[1].v_type != VAR_UNKNOWN
11025 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011026 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011027 if (!error)
11028 {
11029 ExpandInit(&xpc);
11030 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011031 if (p_wic)
11032 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011033 if (rettv->v_type == VAR_STRING)
11034 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11035 options, WILD_ALL);
11036 else if (rettv_list_alloc(rettv) != FAIL)
11037 {
11038 int i;
11039
11040 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11041 for (i = 0; i < xpc.xp_numfiles; i++)
11042 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11043 ExpandCleanup(&xpc);
11044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 }
11046 else
11047 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 }
11049}
11050
11051/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011052 * Go over all entries in "d2" and add them to "d1".
11053 * When "action" is "error" then a duplicate key is an error.
11054 * When "action" is "force" then a duplicate key is overwritten.
11055 * Otherwise duplicate keys are ignored ("action" is "keep").
11056 */
11057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011058dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011059{
11060 dictitem_T *di1;
11061 hashitem_T *hi2;
11062 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011063 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011064
11065 todo = (int)d2->dv_hashtab.ht_used;
11066 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11067 {
11068 if (!HASHITEM_EMPTY(hi2))
11069 {
11070 --todo;
11071 di1 = dict_find(d1, hi2->hi_key, -1);
11072 if (d1->dv_scope != 0)
11073 {
11074 /* Disallow replacing a builtin function in l: and g:.
11075 * Check the key to be valid when adding to any
11076 * scope. */
11077 if (d1->dv_scope == VAR_DEF_SCOPE
11078 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11079 && var_check_func_name(hi2->hi_key,
11080 di1 == NULL))
11081 break;
11082 if (!valid_varname(hi2->hi_key))
11083 break;
11084 }
11085 if (di1 == NULL)
11086 {
11087 di1 = dictitem_copy(HI2DI(hi2));
11088 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11089 dictitem_free(di1);
11090 }
11091 else if (*action == 'e')
11092 {
11093 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11094 break;
11095 }
11096 else if (*action == 'f' && HI2DI(hi2) != di1)
11097 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011098 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11099 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011100 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011101 clear_tv(&di1->di_tv);
11102 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11103 }
11104 }
11105 }
11106}
11107
11108/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011109 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011111 */
11112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011113f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011114{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011115 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011116
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011118 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 list_T *l1, *l2;
11120 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011123
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 l1 = argvars[0].vval.v_list;
11125 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011126 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011127 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011128 {
11129 if (argvars[2].v_type != VAR_UNKNOWN)
11130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 before = get_tv_number_chk(&argvars[2], &error);
11132 if (error)
11133 return; /* type error; errmsg already given */
11134
Bram Moolenaar758711c2005-02-02 23:11:38 +000011135 if (before == l1->lv_len)
11136 item = NULL;
11137 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011139 item = list_find(l1, before);
11140 if (item == NULL)
11141 {
11142 EMSGN(_(e_listidx), before);
11143 return;
11144 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011145 }
11146 }
11147 else
11148 item = NULL;
11149 list_extend(l1, l2, item);
11150
Bram Moolenaare9a41262005-01-15 22:18:47 +000011151 copy_tv(&argvars[0], rettv);
11152 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011154 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11155 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011156 dict_T *d1, *d2;
11157 char_u *action;
11158 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011159
11160 d1 = argvars[0].vval.v_dict;
11161 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011162 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011163 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011164 {
11165 /* Check the third argument. */
11166 if (argvars[2].v_type != VAR_UNKNOWN)
11167 {
11168 static char *(av[]) = {"keep", "force", "error"};
11169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011170 action = get_tv_string_chk(&argvars[2]);
11171 if (action == NULL)
11172 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 for (i = 0; i < 3; ++i)
11174 if (STRCMP(action, av[i]) == 0)
11175 break;
11176 if (i == 3)
11177 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011178 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011179 return;
11180 }
11181 }
11182 else
11183 action = (char_u *)"force";
11184
Bram Moolenaara9922d62013-05-30 13:01:18 +020011185 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011186
Bram Moolenaare9a41262005-01-15 22:18:47 +000011187 copy_tv(&argvars[0], rettv);
11188 }
11189 }
11190 else
11191 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011192}
11193
11194/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011195 * "feedkeys()" function
11196 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011198f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011199{
11200 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011201 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011202 char_u *keys, *flags;
11203 char_u nbuf[NUMBUFLEN];
11204 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011205 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011206 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011207
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011208 /* This is not allowed in the sandbox. If the commands would still be
11209 * executed in the sandbox it would be OK, but it probably happens later,
11210 * when "sandbox" is no longer set. */
11211 if (check_secure())
11212 return;
11213
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011214 keys = get_tv_string(&argvars[0]);
11215 if (*keys != NUL)
11216 {
11217 if (argvars[1].v_type != VAR_UNKNOWN)
11218 {
11219 flags = get_tv_string_buf(&argvars[1], nbuf);
11220 for ( ; *flags != NUL; ++flags)
11221 {
11222 switch (*flags)
11223 {
11224 case 'n': remap = FALSE; break;
11225 case 'm': remap = TRUE; break;
11226 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011227 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011228 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011229 }
11230 }
11231 }
11232
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011233 /* Need to escape K_SPECIAL and CSI before putting the string in the
11234 * typeahead buffer. */
11235 keys_esc = vim_strsave_escape_csi(keys);
11236 if (keys_esc != NULL)
11237 {
11238 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011239 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011240 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011241 if (vgetc_busy)
11242 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011243 if (execute)
11244 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011245 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011246 }
11247}
11248
11249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 * "filereadable()" function
11251 */
11252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011253f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011255 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 char_u *p;
11257 int n;
11258
Bram Moolenaarc236c162008-07-13 17:41:49 +000011259#ifndef O_NONBLOCK
11260# define O_NONBLOCK 0
11261#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011263 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11264 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 {
11266 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011267 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 }
11269 else
11270 n = FALSE;
11271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273}
11274
11275/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011276 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 * rights to write into.
11278 */
11279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011280f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011282 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283}
11284
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011285 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011286findfilendir(
11287 typval_T *argvars UNUSED,
11288 typval_T *rettv,
11289 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011290{
11291#ifdef FEAT_SEARCHPATH
11292 char_u *fname;
11293 char_u *fresult = NULL;
11294 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11295 char_u *p;
11296 char_u pathbuf[NUMBUFLEN];
11297 int count = 1;
11298 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011299 int error = FALSE;
11300#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011301
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011302 rettv->vval.v_string = NULL;
11303 rettv->v_type = VAR_STRING;
11304
11305#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011307
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011308 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011310 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11311 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011312 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011313 else
11314 {
11315 if (*p != NUL)
11316 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011319 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011321 }
11322
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011323 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11324 error = TRUE;
11325
11326 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328 do
11329 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011330 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011331 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 fresult = find_file_in_path_option(first ? fname : NULL,
11333 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011334 0, first, path,
11335 find_what,
11336 curbuf->b_ffname,
11337 find_what == FINDFILE_DIR
11338 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011340
11341 if (fresult != NULL && rettv->v_type == VAR_LIST)
11342 list_append_string(rettv->vval.v_list, fresult, -1);
11343
11344 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011345 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011346
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011347 if (rettv->v_type == VAR_STRING)
11348 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011349#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011350}
11351
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011352static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11353static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011354
11355/*
11356 * Implementation of map() and filter().
11357 */
11358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011359filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011360{
11361 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011362 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 listitem_T *li, *nli;
11364 list_T *l = NULL;
11365 dictitem_T *di;
11366 hashtab_T *ht;
11367 hashitem_T *hi;
11368 dict_T *d = NULL;
11369 typval_T save_val;
11370 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011372 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011373 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011374 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011375 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011376 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011377 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011378
Bram Moolenaare9a41262005-01-15 22:18:47 +000011379 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011380 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011381 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011382 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011383 return;
11384 }
11385 else if (argvars[0].v_type == VAR_DICT)
11386 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011387 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011388 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011389 return;
11390 }
11391 else
11392 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011393 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011394 return;
11395 }
11396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 expr = get_tv_string_buf_chk(&argvars[1], buf);
11398 /* On type errors, the preceding call has already displayed an error
11399 * message. Avoid a misleading error message for an empty string that
11400 * was not passed as argument. */
11401 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403 prepare_vimvar(VV_VAL, &save_val);
11404 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011405
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011406 /* We reset "did_emsg" to be able to detect whether an error
11407 * occurred during evaluation of the expression. */
11408 save_did_emsg = did_emsg;
11409 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011410
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011411 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 vimvars[VV_KEY].vv_type = VAR_STRING;
11415
11416 ht = &d->dv_hashtab;
11417 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011418 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011419 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 if (!HASHITEM_EMPTY(hi))
11422 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011423 int r;
11424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011425 --todo;
11426 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011427 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011428 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11429 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 break;
11431 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011432 r = filter_map_one(&di->di_tv, expr, map, &rem);
11433 clear_tv(&vimvars[VV_KEY].vv_tv);
11434 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 break;
11436 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011437 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011438 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11439 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011440 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011441 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011442 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 }
11444 }
11445 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 }
11447 else
11448 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011449 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011451 for (li = l->lv_first; li != NULL; li = nli)
11452 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011453 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011454 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011455 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011456 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011457 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011458 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011459 break;
11460 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011461 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011462 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011463 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011464 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011465
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011466 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011467 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011468
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011469 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011470 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011471
11472 copy_tv(&argvars[0], rettv);
11473}
11474
11475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011476filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011477{
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011479 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011480 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011481
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011483 s = expr;
11484 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011485 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011486 if (*s != NUL) /* check for trailing chars after expr */
11487 {
11488 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011489 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011490 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011491 }
11492 if (map)
11493 {
11494 /* map(): replace the list item value */
11495 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011496 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 *tv = rettv;
11498 }
11499 else
11500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 int error = FALSE;
11502
Bram Moolenaare9a41262005-01-15 22:18:47 +000011503 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011505 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 /* On type error, nothing has been removed; return FAIL to stop the
11507 * loop. The error message was given by get_tv_number_chk(). */
11508 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011509 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011511 retval = OK;
11512theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011514 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011515}
11516
11517/*
11518 * "filter()" function
11519 */
11520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011521f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011522{
11523 filter_map(argvars, rettv, FALSE);
11524}
11525
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011526/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527 * "finddir({fname}[, {path}[, {count}]])" function
11528 */
11529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011530f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011531{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011532 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011533}
11534
11535/*
11536 * "findfile({fname}[, {path}[, {count}]])" function
11537 */
11538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011539f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011541 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011542}
11543
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011544#ifdef FEAT_FLOAT
11545/*
11546 * "float2nr({float})" function
11547 */
11548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011549f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011550{
11551 float_T f;
11552
11553 if (get_float_arg(argvars, &f) == OK)
11554 {
11555 if (f < -0x7fffffff)
11556 rettv->vval.v_number = -0x7fffffff;
11557 else if (f > 0x7fffffff)
11558 rettv->vval.v_number = 0x7fffffff;
11559 else
11560 rettv->vval.v_number = (varnumber_T)f;
11561 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011562}
11563
11564/*
11565 * "floor({float})" function
11566 */
11567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011568f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011569{
11570 float_T f;
11571
11572 rettv->v_type = VAR_FLOAT;
11573 if (get_float_arg(argvars, &f) == OK)
11574 rettv->vval.v_float = floor(f);
11575 else
11576 rettv->vval.v_float = 0.0;
11577}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011578
11579/*
11580 * "fmod()" function
11581 */
11582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011583f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011584{
11585 float_T fx, fy;
11586
11587 rettv->v_type = VAR_FLOAT;
11588 if (get_float_arg(argvars, &fx) == OK
11589 && get_float_arg(&argvars[1], &fy) == OK)
11590 rettv->vval.v_float = fmod(fx, fy);
11591 else
11592 rettv->vval.v_float = 0.0;
11593}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011594#endif
11595
Bram Moolenaar0d660222005-01-07 21:51:51 +000011596/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011597 * "fnameescape({string})" function
11598 */
11599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011600f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011601{
11602 rettv->vval.v_string = vim_strsave_fnameescape(
11603 get_tv_string(&argvars[0]), FALSE);
11604 rettv->v_type = VAR_STRING;
11605}
11606
11607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 * "fnamemodify({fname}, {mods})" function
11609 */
11610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011611f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612{
11613 char_u *fname;
11614 char_u *mods;
11615 int usedlen = 0;
11616 int len;
11617 char_u *fbuf = NULL;
11618 char_u buf[NUMBUFLEN];
11619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620 fname = get_tv_string_chk(&argvars[0]);
11621 mods = get_tv_string_buf_chk(&argvars[1], buf);
11622 if (fname == NULL || mods == NULL)
11623 fname = NULL;
11624 else
11625 {
11626 len = (int)STRLEN(fname);
11627 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011634 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 vim_free(fbuf);
11636}
11637
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011638static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639
11640/*
11641 * "foldclosed()" function
11642 */
11643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011644foldclosed_both(
11645 typval_T *argvars UNUSED,
11646 typval_T *rettv,
11647 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648{
11649#ifdef FEAT_FOLDING
11650 linenr_T lnum;
11651 linenr_T first, last;
11652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11655 {
11656 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11657 {
11658 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 return;
11663 }
11664 }
11665#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667}
11668
11669/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011670 * "foldclosed()" function
11671 */
11672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011673f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011674{
11675 foldclosed_both(argvars, rettv, FALSE);
11676}
11677
11678/*
11679 * "foldclosedend()" function
11680 */
11681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011682f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011683{
11684 foldclosed_both(argvars, rettv, TRUE);
11685}
11686
11687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 * "foldlevel()" function
11689 */
11690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011691f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692{
11693#ifdef FEAT_FOLDING
11694 linenr_T lnum;
11695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011698 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700}
11701
11702/*
11703 * "foldtext()" function
11704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011706f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707{
11708#ifdef FEAT_FOLDING
11709 linenr_T lnum;
11710 char_u *s;
11711 char_u *r;
11712 int len;
11713 char *txt;
11714#endif
11715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716 rettv->v_type = VAR_STRING;
11717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011719 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11720 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11721 <= curbuf->b_ml.ml_line_count
11722 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 {
11724 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011725 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11726 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 {
11728 if (!linewhite(lnum))
11729 break;
11730 ++lnum;
11731 }
11732
11733 /* Find interesting text in this line. */
11734 s = skipwhite(ml_get(lnum));
11735 /* skip C comment-start */
11736 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011739 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011741 {
11742 s = skipwhite(ml_get(lnum + 1));
11743 if (*s == '*')
11744 s = skipwhite(s + 1);
11745 }
11746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 txt = _("+-%s%3ld lines: ");
11748 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011749 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 + 20 /* for %3ld */
11751 + STRLEN(s))); /* concatenated */
11752 if (r != NULL)
11753 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011754 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11755 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11756 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 len = (int)STRLEN(r);
11758 STRCAT(r, s);
11759 /* remove 'foldmarker' and 'commentstring' */
11760 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 }
11763 }
11764#endif
11765}
11766
11767/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011768 * "foldtextresult(lnum)" function
11769 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011771f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011772{
11773#ifdef FEAT_FOLDING
11774 linenr_T lnum;
11775 char_u *text;
11776 char_u buf[51];
11777 foldinfo_T foldinfo;
11778 int fold_count;
11779#endif
11780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->v_type = VAR_STRING;
11782 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011783#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 /* treat illegal types and illegal string values for {lnum} the same */
11786 if (lnum < 0)
11787 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011788 fold_count = foldedCount(curwin, lnum, &foldinfo);
11789 if (fold_count > 0)
11790 {
11791 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11792 &foldinfo, buf);
11793 if (text == buf)
11794 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011796 }
11797#endif
11798}
11799
11800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 * "foreground()" function
11802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011804f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806#ifdef FEAT_GUI
11807 if (gui.in_use)
11808 gui_mch_set_foreground();
11809#else
11810# ifdef WIN32
11811 win32_set_foreground();
11812# endif
11813#endif
11814}
11815
11816/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011817 * "function()" function
11818 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011820f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011821{
11822 char_u *s;
11823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011826 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011827 /* Don't check an autoload name for existence here. */
11828 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011829 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011830 else
11831 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011832 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011833 {
11834 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011835 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011836
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011837 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11838 * also be called from another script. Using trans_function_name()
11839 * would also work, but some plugins depend on the name being
11840 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011841 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011842 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011843 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011844 if (rettv->vval.v_string != NULL)
11845 {
11846 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011847 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011848 }
11849 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011850 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011851 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011853 }
11854}
11855
11856/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011857 * "garbagecollect()" function
11858 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011860f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011861{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011862 /* This is postponed until we are back at the toplevel, because we may be
11863 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11864 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011865
11866 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11867 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011868}
11869
11870/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011871 * "get()" function
11872 */
11873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011874f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011875{
Bram Moolenaar33570922005-01-25 22:26:29 +000011876 listitem_T *li;
11877 list_T *l;
11878 dictitem_T *di;
11879 dict_T *d;
11880 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011881
Bram Moolenaare9a41262005-01-15 22:18:47 +000011882 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011883 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011884 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011886 int error = FALSE;
11887
11888 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11889 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011890 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011893 else if (argvars[0].v_type == VAR_DICT)
11894 {
11895 if ((d = argvars[0].vval.v_dict) != NULL)
11896 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011897 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011898 if (di != NULL)
11899 tv = &di->di_tv;
11900 }
11901 }
11902 else
11903 EMSG2(_(e_listdictarg), "get()");
11904
11905 if (tv == NULL)
11906 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011907 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011908 copy_tv(&argvars[2], rettv);
11909 }
11910 else
11911 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011912}
11913
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011914static 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 +000011915
11916/*
11917 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011918 * Return a range (from start to end) of lines in rettv from the specified
11919 * buffer.
11920 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011921 */
11922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011923get_buffer_lines(
11924 buf_T *buf,
11925 linenr_T start,
11926 linenr_T end,
11927 int retlist,
11928 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011929{
11930 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011931
Bram Moolenaar959a1432013-12-14 12:17:38 +010011932 rettv->v_type = VAR_STRING;
11933 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011934 if (retlist && rettv_list_alloc(rettv) == FAIL)
11935 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011936
11937 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11938 return;
11939
11940 if (!retlist)
11941 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011942 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11943 p = ml_get_buf(buf, start, FALSE);
11944 else
11945 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011946 rettv->vval.v_string = vim_strsave(p);
11947 }
11948 else
11949 {
11950 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011951 return;
11952
11953 if (start < 1)
11954 start = 1;
11955 if (end > buf->b_ml.ml_line_count)
11956 end = buf->b_ml.ml_line_count;
11957 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011958 if (list_append_string(rettv->vval.v_list,
11959 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011960 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011961 }
11962}
11963
11964/*
11965 * "getbufline()" function
11966 */
11967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011968f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011969{
11970 linenr_T lnum;
11971 linenr_T end;
11972 buf_T *buf;
11973
11974 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11975 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011976 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011977 --emsg_off;
11978
Bram Moolenaar661b1822005-07-28 22:36:45 +000011979 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011980 if (argvars[2].v_type == VAR_UNKNOWN)
11981 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011982 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011983 end = get_tv_lnum_buf(&argvars[2], buf);
11984
Bram Moolenaar342337a2005-07-21 21:11:17 +000011985 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011986}
11987
Bram Moolenaar0d660222005-01-07 21:51:51 +000011988/*
11989 * "getbufvar()" function
11990 */
11991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011992f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011993{
11994 buf_T *buf;
11995 buf_T *save_curbuf;
11996 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011997 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011998 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012000 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12001 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012002 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012003 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012004
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012005 rettv->v_type = VAR_STRING;
12006 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012007
12008 if (buf != NULL && varname != NULL)
12009 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012010 /* set curbuf to be our buf, temporarily */
12011 save_curbuf = curbuf;
12012 curbuf = buf;
12013
Bram Moolenaar0d660222005-01-07 21:51:51 +000012014 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012015 {
12016 if (get_option_tv(&varname, rettv, TRUE) == OK)
12017 done = TRUE;
12018 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012019 else if (STRCMP(varname, "changedtick") == 0)
12020 {
12021 rettv->v_type = VAR_NUMBER;
12022 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012023 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012025 else
12026 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012027 /* Look up the variable. */
12028 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12029 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12030 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012032 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012033 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012034 done = TRUE;
12035 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012036 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012037
12038 /* restore previous notion of curbuf */
12039 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012040 }
12041
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012042 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12043 /* use the default value */
12044 copy_tv(&argvars[2], rettv);
12045
Bram Moolenaar0d660222005-01-07 21:51:51 +000012046 --emsg_off;
12047}
12048
12049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 * "getchar()" function
12051 */
12052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012053f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
12055 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012056 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012058 /* Position the cursor. Needed after a message that ends in a space. */
12059 windgoto(msg_row, msg_col);
12060
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 ++no_mapping;
12062 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012063 for (;;)
12064 {
12065 if (argvars[0].v_type == VAR_UNKNOWN)
12066 /* getchar(): blocking wait. */
12067 n = safe_vgetc();
12068 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12069 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012070 n = vpeekc_any();
12071 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012072 /* illegal argument or getchar(0) and no char avail: return zero */
12073 n = 0;
12074 else
12075 /* getchar(0) and char avail: return char */
12076 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012077
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012078 if (n == K_IGNORE)
12079 continue;
12080 break;
12081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 --no_mapping;
12083 --allow_keys;
12084
Bram Moolenaar219b8702006-11-01 14:32:36 +000012085 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12086 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12087 vimvars[VV_MOUSE_COL].vv_nr = 0;
12088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012089 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090 if (IS_SPECIAL(n) || mod_mask != 0)
12091 {
12092 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12093 int i = 0;
12094
12095 /* Turn a special key into three bytes, plus modifier. */
12096 if (mod_mask != 0)
12097 {
12098 temp[i++] = K_SPECIAL;
12099 temp[i++] = KS_MODIFIER;
12100 temp[i++] = mod_mask;
12101 }
12102 if (IS_SPECIAL(n))
12103 {
12104 temp[i++] = K_SPECIAL;
12105 temp[i++] = K_SECOND(n);
12106 temp[i++] = K_THIRD(n);
12107 }
12108#ifdef FEAT_MBYTE
12109 else if (has_mbyte)
12110 i += (*mb_char2bytes)(n, temp + i);
12111#endif
12112 else
12113 temp[i++] = n;
12114 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012115 rettv->v_type = VAR_STRING;
12116 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012117
12118#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012119 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012120 {
12121 int row = mouse_row;
12122 int col = mouse_col;
12123 win_T *win;
12124 linenr_T lnum;
12125# ifdef FEAT_WINDOWS
12126 win_T *wp;
12127# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012128 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012129
12130 if (row >= 0 && col >= 0)
12131 {
12132 /* Find the window at the mouse coordinates and compute the
12133 * text position. */
12134 win = mouse_find_win(&row, &col);
12135 (void)mouse_comp_pos(win, &row, &col, &lnum);
12136# ifdef FEAT_WINDOWS
12137 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012138 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012139# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012140 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012141 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12142 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12143 }
12144 }
12145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 }
12147}
12148
12149/*
12150 * "getcharmod()" function
12151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012153f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156}
12157
12158/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012159 * "getcharsearch()" function
12160 */
12161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012162f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012163{
12164 if (rettv_dict_alloc(rettv) != FAIL)
12165 {
12166 dict_T *dict = rettv->vval.v_dict;
12167
12168 dict_add_nr_str(dict, "char", 0L, last_csearch());
12169 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12170 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12171 }
12172}
12173
12174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 * "getcmdline()" function
12176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012178f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->v_type = VAR_STRING;
12181 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182}
12183
12184/*
12185 * "getcmdpos()" function
12186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012188f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191}
12192
12193/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012194 * "getcmdtype()" function
12195 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012197f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012198{
12199 rettv->v_type = VAR_STRING;
12200 rettv->vval.v_string = alloc(2);
12201 if (rettv->vval.v_string != NULL)
12202 {
12203 rettv->vval.v_string[0] = get_cmdline_type();
12204 rettv->vval.v_string[1] = NUL;
12205 }
12206}
12207
12208/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012209 * "getcmdwintype()" function
12210 */
12211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012212f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012213{
12214 rettv->v_type = VAR_STRING;
12215 rettv->vval.v_string = NULL;
12216#ifdef FEAT_CMDWIN
12217 rettv->vval.v_string = alloc(2);
12218 if (rettv->vval.v_string != NULL)
12219 {
12220 rettv->vval.v_string[0] = cmdwin_type;
12221 rettv->vval.v_string[1] = NUL;
12222 }
12223#endif
12224}
12225
12226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 * "getcwd()" function
12228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012230f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012232 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012233 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012236 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012237
12238 wp = find_tabwin(&argvars[0], &argvars[1]);
12239 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012241 if (wp->w_localdir != NULL)
12242 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12243 else if(globaldir != NULL)
12244 rettv->vval.v_string = vim_strsave(globaldir);
12245 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012246 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012247 cwd = alloc(MAXPATHL);
12248 if (cwd != NULL)
12249 {
12250 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12251 rettv->vval.v_string = vim_strsave(cwd);
12252 vim_free(cwd);
12253 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012254 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012255#ifdef BACKSLASH_IN_FILENAME
12256 if (rettv->vval.v_string != NULL)
12257 slash_adjust(rettv->vval.v_string);
12258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 }
12260}
12261
12262/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012263 * "getfontname()" function
12264 */
12265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012266f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012267{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 rettv->v_type = VAR_STRING;
12269 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012270#ifdef FEAT_GUI
12271 if (gui.in_use)
12272 {
12273 GuiFont font;
12274 char_u *name = NULL;
12275
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012276 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012277 {
12278 /* Get the "Normal" font. Either the name saved by
12279 * hl_set_font_name() or from the font ID. */
12280 font = gui.norm_font;
12281 name = hl_get_font_name();
12282 }
12283 else
12284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012285 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012286 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12287 return;
12288 font = gui_mch_get_font(name, FALSE);
12289 if (font == NOFONT)
12290 return; /* Invalid font name, return empty string. */
12291 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012293 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012294 gui_mch_free_font(font);
12295 }
12296#endif
12297}
12298
12299/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012300 * "getfperm({fname})" function
12301 */
12302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012303f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012304{
12305 char_u *fname;
12306 struct stat st;
12307 char_u *perm = NULL;
12308 char_u flags[] = "rwx";
12309 int i;
12310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012314 if (mch_stat((char *)fname, &st) >= 0)
12315 {
12316 perm = vim_strsave((char_u *)"---------");
12317 if (perm != NULL)
12318 {
12319 for (i = 0; i < 9; i++)
12320 {
12321 if (st.st_mode & (1 << (8 - i)))
12322 perm[i] = flags[i % 3];
12323 }
12324 }
12325 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012327}
12328
12329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 * "getfsize({fname})" function
12331 */
12332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012333f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
12335 char_u *fname;
12336 struct stat st;
12337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341
12342 if (mch_stat((char *)fname, &st) >= 0)
12343 {
12344 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012349
12350 /* non-perfect check for overflow */
12351 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12352 rettv->vval.v_number = -2;
12353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 }
12355 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012356 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357}
12358
12359/*
12360 * "getftime({fname})" function
12361 */
12362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012363f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
12365 char_u *fname;
12366 struct stat st;
12367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369
12370 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375
12376/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012377 * "getftype({fname})" function
12378 */
12379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012380f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012381{
12382 char_u *fname;
12383 struct stat st;
12384 char_u *type = NULL;
12385 char *t;
12386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012390 if (mch_lstat((char *)fname, &st) >= 0)
12391 {
12392#ifdef S_ISREG
12393 if (S_ISREG(st.st_mode))
12394 t = "file";
12395 else if (S_ISDIR(st.st_mode))
12396 t = "dir";
12397# ifdef S_ISLNK
12398 else if (S_ISLNK(st.st_mode))
12399 t = "link";
12400# endif
12401# ifdef S_ISBLK
12402 else if (S_ISBLK(st.st_mode))
12403 t = "bdev";
12404# endif
12405# ifdef S_ISCHR
12406 else if (S_ISCHR(st.st_mode))
12407 t = "cdev";
12408# endif
12409# ifdef S_ISFIFO
12410 else if (S_ISFIFO(st.st_mode))
12411 t = "fifo";
12412# endif
12413# ifdef S_ISSOCK
12414 else if (S_ISSOCK(st.st_mode))
12415 t = "fifo";
12416# endif
12417 else
12418 t = "other";
12419#else
12420# ifdef S_IFMT
12421 switch (st.st_mode & S_IFMT)
12422 {
12423 case S_IFREG: t = "file"; break;
12424 case S_IFDIR: t = "dir"; break;
12425# ifdef S_IFLNK
12426 case S_IFLNK: t = "link"; break;
12427# endif
12428# ifdef S_IFBLK
12429 case S_IFBLK: t = "bdev"; break;
12430# endif
12431# ifdef S_IFCHR
12432 case S_IFCHR: t = "cdev"; break;
12433# endif
12434# ifdef S_IFIFO
12435 case S_IFIFO: t = "fifo"; break;
12436# endif
12437# ifdef S_IFSOCK
12438 case S_IFSOCK: t = "socket"; break;
12439# endif
12440 default: t = "other";
12441 }
12442# else
12443 if (mch_isdir(fname))
12444 t = "dir";
12445 else
12446 t = "file";
12447# endif
12448#endif
12449 type = vim_strsave((char_u *)t);
12450 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012452}
12453
12454/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012455 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012456 */
12457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012458f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012459{
12460 linenr_T lnum;
12461 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012462 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012463
12464 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012465 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012466 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012467 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012468 retlist = FALSE;
12469 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012470 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012471 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012472 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012473 retlist = TRUE;
12474 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012475
Bram Moolenaar342337a2005-07-21 21:11:17 +000012476 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012477}
12478
12479/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012480 * "getmatches()" function
12481 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012483f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012484{
12485#ifdef FEAT_SEARCH_EXTRA
12486 dict_T *dict;
12487 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012488 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012489
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012490 if (rettv_list_alloc(rettv) == OK)
12491 {
12492 while (cur != NULL)
12493 {
12494 dict = dict_alloc();
12495 if (dict == NULL)
12496 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012497 if (cur->match.regprog == NULL)
12498 {
12499 /* match added with matchaddpos() */
12500 for (i = 0; i < MAXPOSMATCH; ++i)
12501 {
12502 llpos_T *llpos;
12503 char buf[6];
12504 list_T *l;
12505
12506 llpos = &cur->pos.pos[i];
12507 if (llpos->lnum == 0)
12508 break;
12509 l = list_alloc();
12510 if (l == NULL)
12511 break;
12512 list_append_number(l, (varnumber_T)llpos->lnum);
12513 if (llpos->col > 0)
12514 {
12515 list_append_number(l, (varnumber_T)llpos->col);
12516 list_append_number(l, (varnumber_T)llpos->len);
12517 }
12518 sprintf(buf, "pos%d", i + 1);
12519 dict_add_list(dict, buf, l);
12520 }
12521 }
12522 else
12523 {
12524 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12525 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012526 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012527 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12528 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012529# ifdef FEAT_CONCEAL
12530 if (cur->conceal_char)
12531 {
12532 char_u buf[MB_MAXBYTES + 1];
12533
12534 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12535 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12536 }
12537# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012538 list_append_dict(rettv->vval.v_list, dict);
12539 cur = cur->next;
12540 }
12541 }
12542#endif
12543}
12544
12545/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012546 * "getpid()" function
12547 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012549f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012550{
12551 rettv->vval.v_number = mch_get_pid();
12552}
12553
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012554static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012555
12556/*
12557 * "getcurpos()" function
12558 */
12559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012560f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012561{
12562 getpos_both(argvars, rettv, TRUE);
12563}
12564
Bram Moolenaar18081e32008-02-20 19:11:07 +000012565/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012566 * "getpos(string)" function
12567 */
12568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012569f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012570{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012571 getpos_both(argvars, rettv, FALSE);
12572}
12573
12574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012575getpos_both(
12576 typval_T *argvars,
12577 typval_T *rettv,
12578 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012579{
Bram Moolenaara5525202006-03-02 22:52:09 +000012580 pos_T *fp;
12581 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012582 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012583
12584 if (rettv_list_alloc(rettv) == OK)
12585 {
12586 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012587 if (getcurpos)
12588 fp = &curwin->w_cursor;
12589 else
12590 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012591 if (fnum != -1)
12592 list_append_number(l, (varnumber_T)fnum);
12593 else
12594 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012595 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12596 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012597 list_append_number(l, (fp != NULL)
12598 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012599 : (varnumber_T)0);
12600 list_append_number(l,
12601#ifdef FEAT_VIRTUALEDIT
12602 (fp != NULL) ? (varnumber_T)fp->coladd :
12603#endif
12604 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012605 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012606 {
12607 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012608 list_append_number(l, curwin->w_curswant == MAXCOL ?
12609 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012610 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012611 }
12612 else
12613 rettv->vval.v_number = FALSE;
12614}
12615
12616/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012617 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012618 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012620f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012621{
12622#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012623 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012624#endif
12625
Bram Moolenaar2641f772005-03-25 21:58:17 +000012626#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012627 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012628 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012629 wp = NULL;
12630 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12631 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012632 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012633 if (wp == NULL)
12634 return;
12635 }
12636
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012637 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012638 }
12639#endif
12640}
12641
12642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 * "getreg()" function
12644 */
12645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012646f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647{
12648 char_u *strregname;
12649 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012650 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012651 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012652 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012654 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012656 strregname = get_tv_string_chk(&argvars[0]);
12657 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012658 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012660 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012661 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12662 return_list = get_tv_number_chk(&argvars[2], &error);
12663 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012666 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012667
12668 if (error)
12669 return;
12670
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 regname = (strregname == NULL ? '"' : *strregname);
12672 if (regname == 0)
12673 regname = '"';
12674
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012675 if (return_list)
12676 {
12677 rettv->v_type = VAR_LIST;
12678 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12679 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012680 if (rettv->vval.v_list != NULL)
12681 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012682 }
12683 else
12684 {
12685 rettv->v_type = VAR_STRING;
12686 rettv->vval.v_string = get_reg_contents(regname,
12687 arg2 ? GREG_EXPR_SRC : 0);
12688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689}
12690
12691/*
12692 * "getregtype()" function
12693 */
12694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012695f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696{
12697 char_u *strregname;
12698 int regname;
12699 char_u buf[NUMBUFLEN + 2];
12700 long reglen = 0;
12701
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012702 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 {
12704 strregname = get_tv_string_chk(&argvars[0]);
12705 if (strregname == NULL) /* type error; errmsg already given */
12706 {
12707 rettv->v_type = VAR_STRING;
12708 rettv->vval.v_string = NULL;
12709 return;
12710 }
12711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 else
12713 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012714 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715
12716 regname = (strregname == NULL ? '"' : *strregname);
12717 if (regname == 0)
12718 regname = '"';
12719
12720 buf[0] = NUL;
12721 buf[1] = NUL;
12722 switch (get_reg_type(regname, &reglen))
12723 {
12724 case MLINE: buf[0] = 'V'; break;
12725 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 case MBLOCK:
12727 buf[0] = Ctrl_V;
12728 sprintf((char *)buf + 1, "%ld", reglen + 1);
12729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731 rettv->v_type = VAR_STRING;
12732 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733}
12734
12735/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012736 * "gettabvar()" function
12737 */
12738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012739f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012740{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012741 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012742 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012743 dictitem_T *v;
12744 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012745 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012746
12747 rettv->v_type = VAR_STRING;
12748 rettv->vval.v_string = NULL;
12749
12750 varname = get_tv_string_chk(&argvars[1]);
12751 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12752 if (tp != NULL && varname != NULL)
12753 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012754 /* Set tp to be our tabpage, temporarily. Also set the window to the
12755 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012756 if (switch_win(&oldcurwin, &oldtabpage,
12757 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012758 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012759 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012760 /* look up the variable */
12761 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12762 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12763 if (v != NULL)
12764 {
12765 copy_tv(&v->di_tv, rettv);
12766 done = TRUE;
12767 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012768 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012769
12770 /* restore previous notion of curwin */
12771 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012772 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012773
12774 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012775 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012776}
12777
12778/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012779 * "gettabwinvar()" function
12780 */
12781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012782f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012783{
12784 getwinvar(argvars, rettv, 1);
12785}
12786
12787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 * "getwinposx()" function
12789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012791f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794#ifdef FEAT_GUI
12795 if (gui.in_use)
12796 {
12797 int x, y;
12798
12799 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 }
12802#endif
12803}
12804
12805/*
12806 * "getwinposy()" function
12807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012809f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812#ifdef FEAT_GUI
12813 if (gui.in_use)
12814 {
12815 int x, y;
12816
12817 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 }
12820#endif
12821}
12822
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012823/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012824 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012825 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012826 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012827find_win_by_nr(
12828 typval_T *vp,
12829 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012830{
12831#ifdef FEAT_WINDOWS
12832 win_T *wp;
12833#endif
12834 int nr;
12835
12836 nr = get_tv_number_chk(vp, NULL);
12837
12838#ifdef FEAT_WINDOWS
12839 if (nr < 0)
12840 return NULL;
12841 if (nr == 0)
12842 return curwin;
12843
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012844 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12845 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012846 if (--nr <= 0)
12847 break;
12848 return wp;
12849#else
12850 if (nr == 0 || nr == 1)
12851 return curwin;
12852 return NULL;
12853#endif
12854}
12855
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012857 * Find window specified by "wvp" in tabpage "tvp".
12858 */
12859 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012860find_tabwin(
12861 typval_T *wvp, /* VAR_UNKNOWN for current window */
12862 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012863{
12864 win_T *wp = NULL;
12865 tabpage_T *tp = NULL;
12866 long n;
12867
12868 if (wvp->v_type != VAR_UNKNOWN)
12869 {
12870 if (tvp->v_type != VAR_UNKNOWN)
12871 {
12872 n = get_tv_number(tvp);
12873 if (n >= 0)
12874 tp = find_tabpage(n);
12875 }
12876 else
12877 tp = curtab;
12878
12879 if (tp != NULL)
12880 wp = find_win_by_nr(wvp, tp);
12881 }
12882 else
12883 wp = curwin;
12884
12885 return wp;
12886}
12887
12888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 * "getwinvar()" function
12890 */
12891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012892f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012894 getwinvar(argvars, rettv, 0);
12895}
12896
12897/*
12898 * getwinvar() and gettabwinvar()
12899 */
12900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012901getwinvar(
12902 typval_T *argvars,
12903 typval_T *rettv,
12904 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012905{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012906 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012908 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012909 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012910 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012911#ifdef FEAT_WINDOWS
12912 win_T *oldcurwin;
12913 tabpage_T *oldtabpage;
12914 int need_switch_win;
12915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012917#ifdef FEAT_WINDOWS
12918 if (off == 1)
12919 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12920 else
12921 tp = curtab;
12922#endif
12923 win = find_win_by_nr(&argvars[off], tp);
12924 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012925 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012927 rettv->v_type = VAR_STRING;
12928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929
12930 if (win != NULL && varname != NULL)
12931 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012932#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012933 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012934 * otherwise the window is not valid. Only do this when needed,
12935 * autocommands get blocked. */
12936 need_switch_win = !(tp == curtab && win == curwin);
12937 if (!need_switch_win
12938 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12939#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012940 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012941 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012942 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012943 if (get_option_tv(&varname, rettv, 1) == OK)
12944 done = TRUE;
12945 }
12946 else
12947 {
12948 /* Look up the variable. */
12949 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12950 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12951 varname, FALSE);
12952 if (v != NULL)
12953 {
12954 copy_tv(&v->di_tv, rettv);
12955 done = TRUE;
12956 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012959
Bram Moolenaarba117c22015-09-29 16:53:22 +020012960#ifdef FEAT_WINDOWS
12961 if (need_switch_win)
12962 /* restore previous notion of curwin */
12963 restore_win(oldcurwin, oldtabpage, TRUE);
12964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 }
12966
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012967 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12968 /* use the default return value */
12969 copy_tv(&argvars[off + 2], rettv);
12970
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 --emsg_off;
12972}
12973
12974/*
12975 * "glob()" function
12976 */
12977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012978f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012980 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012982 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012984 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012985 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012987 if (argvars[1].v_type != VAR_UNKNOWN)
12988 {
12989 if (get_tv_number_chk(&argvars[1], &error))
12990 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012991 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012992 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012993 if (get_tv_number_chk(&argvars[2], &error))
12994 {
12995 rettv->v_type = VAR_LIST;
12996 rettv->vval.v_list = NULL;
12997 }
12998 if (argvars[3].v_type != VAR_UNKNOWN
12999 && get_tv_number_chk(&argvars[3], &error))
13000 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013001 }
13002 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013003 if (!error)
13004 {
13005 ExpandInit(&xpc);
13006 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013007 if (p_wic)
13008 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013009 if (rettv->v_type == VAR_STRING)
13010 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013011 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013012 else if (rettv_list_alloc(rettv) != FAIL)
13013 {
13014 int i;
13015
13016 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13017 NULL, options, WILD_ALL_KEEP);
13018 for (i = 0; i < xpc.xp_numfiles; i++)
13019 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13020
13021 ExpandCleanup(&xpc);
13022 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013023 }
13024 else
13025 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026}
13027
13028/*
13029 * "globpath()" function
13030 */
13031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013032f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013034 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013036 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013037 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013038 garray_T ga;
13039 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013041 /* When the optional second argument is non-zero, don't remove matches
13042 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013044 if (argvars[2].v_type != VAR_UNKNOWN)
13045 {
13046 if (get_tv_number_chk(&argvars[2], &error))
13047 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013048 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013049 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013050 if (get_tv_number_chk(&argvars[3], &error))
13051 {
13052 rettv->v_type = VAR_LIST;
13053 rettv->vval.v_list = NULL;
13054 }
13055 if (argvars[4].v_type != VAR_UNKNOWN
13056 && get_tv_number_chk(&argvars[4], &error))
13057 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013058 }
13059 }
13060 if (file != NULL && !error)
13061 {
13062 ga_init2(&ga, (int)sizeof(char_u *), 10);
13063 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13064 if (rettv->v_type == VAR_STRING)
13065 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13066 else if (rettv_list_alloc(rettv) != FAIL)
13067 for (i = 0; i < ga.ga_len; ++i)
13068 list_append_string(rettv->vval.v_list,
13069 ((char_u **)(ga.ga_data))[i], -1);
13070 ga_clear_strings(&ga);
13071 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013072 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074}
13075
13076/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013077 * "glob2regpat()" function
13078 */
13079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013080f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013081{
13082 char_u *pat = get_tv_string_chk(&argvars[0]);
13083
13084 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013085 rettv->vval.v_string = (pat == NULL)
13086 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013087}
13088
13089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 * "has()" function
13091 */
13092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013093f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094{
13095 int i;
13096 char_u *name;
13097 int n = FALSE;
13098 static char *(has_list[]) =
13099 {
13100#ifdef AMIGA
13101 "amiga",
13102# ifdef FEAT_ARP
13103 "arp",
13104# endif
13105#endif
13106#ifdef __BEOS__
13107 "beos",
13108#endif
13109#ifdef MSDOS
13110# ifdef DJGPP
13111 "dos32",
13112# else
13113 "dos16",
13114# endif
13115#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013116#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117 "mac",
13118#endif
13119#if defined(MACOS_X_UNIX)
13120 "macunix",
13121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122#ifdef __QNX__
13123 "qnx",
13124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125#ifdef UNIX
13126 "unix",
13127#endif
13128#ifdef VMS
13129 "vms",
13130#endif
13131#ifdef WIN16
13132 "win16",
13133#endif
13134#ifdef WIN32
13135 "win32",
13136#endif
13137#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13138 "win32unix",
13139#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013140#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 "win64",
13142#endif
13143#ifdef EBCDIC
13144 "ebcdic",
13145#endif
13146#ifndef CASE_INSENSITIVE_FILENAME
13147 "fname_case",
13148#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013149#ifdef HAVE_ACL
13150 "acl",
13151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152#ifdef FEAT_ARABIC
13153 "arabic",
13154#endif
13155#ifdef FEAT_AUTOCMD
13156 "autocmd",
13157#endif
13158#ifdef FEAT_BEVAL
13159 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013160# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13161 "balloon_multiline",
13162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163#endif
13164#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13165 "builtin_terms",
13166# ifdef ALL_BUILTIN_TCAPS
13167 "all_builtin_terms",
13168# endif
13169#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013170#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13171 || defined(FEAT_GUI_W32) \
13172 || defined(FEAT_GUI_MOTIF))
13173 "browsefilter",
13174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175#ifdef FEAT_BYTEOFF
13176 "byte_offset",
13177#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013178#ifdef FEAT_CHANNEL
13179 "channel",
13180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181#ifdef FEAT_CINDENT
13182 "cindent",
13183#endif
13184#ifdef FEAT_CLIENTSERVER
13185 "clientserver",
13186#endif
13187#ifdef FEAT_CLIPBOARD
13188 "clipboard",
13189#endif
13190#ifdef FEAT_CMDL_COMPL
13191 "cmdline_compl",
13192#endif
13193#ifdef FEAT_CMDHIST
13194 "cmdline_hist",
13195#endif
13196#ifdef FEAT_COMMENTS
13197 "comments",
13198#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013199#ifdef FEAT_CONCEAL
13200 "conceal",
13201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202#ifdef FEAT_CRYPT
13203 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013204 "crypt-blowfish",
13205 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206#endif
13207#ifdef FEAT_CSCOPE
13208 "cscope",
13209#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013210#ifdef FEAT_CURSORBIND
13211 "cursorbind",
13212#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013213#ifdef CURSOR_SHAPE
13214 "cursorshape",
13215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216#ifdef DEBUG
13217 "debug",
13218#endif
13219#ifdef FEAT_CON_DIALOG
13220 "dialog_con",
13221#endif
13222#ifdef FEAT_GUI_DIALOG
13223 "dialog_gui",
13224#endif
13225#ifdef FEAT_DIFF
13226 "diff",
13227#endif
13228#ifdef FEAT_DIGRAPHS
13229 "digraphs",
13230#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013231#ifdef FEAT_DIRECTX
13232 "directx",
13233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234#ifdef FEAT_DND
13235 "dnd",
13236#endif
13237#ifdef FEAT_EMACS_TAGS
13238 "emacs_tags",
13239#endif
13240 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013241 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#ifdef FEAT_SEARCH_EXTRA
13243 "extra_search",
13244#endif
13245#ifdef FEAT_FKMAP
13246 "farsi",
13247#endif
13248#ifdef FEAT_SEARCHPATH
13249 "file_in_path",
13250#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013251#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013252 "filterpipe",
13253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254#ifdef FEAT_FIND_ID
13255 "find_in_path",
13256#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013257#ifdef FEAT_FLOAT
13258 "float",
13259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260#ifdef FEAT_FOLDING
13261 "folding",
13262#endif
13263#ifdef FEAT_FOOTER
13264 "footer",
13265#endif
13266#if !defined(USE_SYSTEM) && defined(UNIX)
13267 "fork",
13268#endif
13269#ifdef FEAT_GETTEXT
13270 "gettext",
13271#endif
13272#ifdef FEAT_GUI
13273 "gui",
13274#endif
13275#ifdef FEAT_GUI_ATHENA
13276# ifdef FEAT_GUI_NEXTAW
13277 "gui_neXtaw",
13278# else
13279 "gui_athena",
13280# endif
13281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282#ifdef FEAT_GUI_GTK
13283 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013286#ifdef FEAT_GUI_GNOME
13287 "gui_gnome",
13288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289#ifdef FEAT_GUI_MAC
13290 "gui_mac",
13291#endif
13292#ifdef FEAT_GUI_MOTIF
13293 "gui_motif",
13294#endif
13295#ifdef FEAT_GUI_PHOTON
13296 "gui_photon",
13297#endif
13298#ifdef FEAT_GUI_W16
13299 "gui_win16",
13300#endif
13301#ifdef FEAT_GUI_W32
13302 "gui_win32",
13303#endif
13304#ifdef FEAT_HANGULIN
13305 "hangul_input",
13306#endif
13307#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13308 "iconv",
13309#endif
13310#ifdef FEAT_INS_EXPAND
13311 "insert_expand",
13312#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013313#ifdef FEAT_JOB
13314 "job",
13315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316#ifdef FEAT_JUMPLIST
13317 "jumplist",
13318#endif
13319#ifdef FEAT_KEYMAP
13320 "keymap",
13321#endif
13322#ifdef FEAT_LANGMAP
13323 "langmap",
13324#endif
13325#ifdef FEAT_LIBCALL
13326 "libcall",
13327#endif
13328#ifdef FEAT_LINEBREAK
13329 "linebreak",
13330#endif
13331#ifdef FEAT_LISP
13332 "lispindent",
13333#endif
13334#ifdef FEAT_LISTCMDS
13335 "listcmds",
13336#endif
13337#ifdef FEAT_LOCALMAP
13338 "localmap",
13339#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013340#ifdef FEAT_LUA
13341# ifndef DYNAMIC_LUA
13342 "lua",
13343# endif
13344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345#ifdef FEAT_MENU
13346 "menu",
13347#endif
13348#ifdef FEAT_SESSION
13349 "mksession",
13350#endif
13351#ifdef FEAT_MODIFY_FNAME
13352 "modify_fname",
13353#endif
13354#ifdef FEAT_MOUSE
13355 "mouse",
13356#endif
13357#ifdef FEAT_MOUSESHAPE
13358 "mouseshape",
13359#endif
13360#if defined(UNIX) || defined(VMS)
13361# ifdef FEAT_MOUSE_DEC
13362 "mouse_dec",
13363# endif
13364# ifdef FEAT_MOUSE_GPM
13365 "mouse_gpm",
13366# endif
13367# ifdef FEAT_MOUSE_JSB
13368 "mouse_jsbterm",
13369# endif
13370# ifdef FEAT_MOUSE_NET
13371 "mouse_netterm",
13372# endif
13373# ifdef FEAT_MOUSE_PTERM
13374 "mouse_pterm",
13375# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013376# ifdef FEAT_MOUSE_SGR
13377 "mouse_sgr",
13378# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013379# ifdef FEAT_SYSMOUSE
13380 "mouse_sysmouse",
13381# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013382# ifdef FEAT_MOUSE_URXVT
13383 "mouse_urxvt",
13384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385# ifdef FEAT_MOUSE_XTERM
13386 "mouse_xterm",
13387# endif
13388#endif
13389#ifdef FEAT_MBYTE
13390 "multi_byte",
13391#endif
13392#ifdef FEAT_MBYTE_IME
13393 "multi_byte_ime",
13394#endif
13395#ifdef FEAT_MULTI_LANG
13396 "multi_lang",
13397#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013398#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013399#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013400 "mzscheme",
13401#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403#ifdef FEAT_OLE
13404 "ole",
13405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406#ifdef FEAT_PATH_EXTRA
13407 "path_extra",
13408#endif
13409#ifdef FEAT_PERL
13410#ifndef DYNAMIC_PERL
13411 "perl",
13412#endif
13413#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013414#ifdef FEAT_PERSISTENT_UNDO
13415 "persistent_undo",
13416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417#ifdef FEAT_PYTHON
13418#ifndef DYNAMIC_PYTHON
13419 "python",
13420#endif
13421#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013422#ifdef FEAT_PYTHON3
13423#ifndef DYNAMIC_PYTHON3
13424 "python3",
13425#endif
13426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427#ifdef FEAT_POSTSCRIPT
13428 "postscript",
13429#endif
13430#ifdef FEAT_PRINTER
13431 "printer",
13432#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013433#ifdef FEAT_PROFILE
13434 "profile",
13435#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013436#ifdef FEAT_RELTIME
13437 "reltime",
13438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439#ifdef FEAT_QUICKFIX
13440 "quickfix",
13441#endif
13442#ifdef FEAT_RIGHTLEFT
13443 "rightleft",
13444#endif
13445#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13446 "ruby",
13447#endif
13448#ifdef FEAT_SCROLLBIND
13449 "scrollbind",
13450#endif
13451#ifdef FEAT_CMDL_INFO
13452 "showcmd",
13453 "cmdline_info",
13454#endif
13455#ifdef FEAT_SIGNS
13456 "signs",
13457#endif
13458#ifdef FEAT_SMARTINDENT
13459 "smartindent",
13460#endif
13461#ifdef FEAT_SNIFF
13462 "sniff",
13463#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013464#ifdef STARTUPTIME
13465 "startuptime",
13466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467#ifdef FEAT_STL_OPT
13468 "statusline",
13469#endif
13470#ifdef FEAT_SUN_WORKSHOP
13471 "sun_workshop",
13472#endif
13473#ifdef FEAT_NETBEANS_INTG
13474 "netbeans_intg",
13475#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013476#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013477 "spell",
13478#endif
13479#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 "syntax",
13481#endif
13482#if defined(USE_SYSTEM) || !defined(UNIX)
13483 "system",
13484#endif
13485#ifdef FEAT_TAG_BINS
13486 "tag_binary",
13487#endif
13488#ifdef FEAT_TAG_OLDSTATIC
13489 "tag_old_static",
13490#endif
13491#ifdef FEAT_TAG_ANYWHITE
13492 "tag_any_white",
13493#endif
13494#ifdef FEAT_TCL
13495# ifndef DYNAMIC_TCL
13496 "tcl",
13497# endif
13498#endif
13499#ifdef TERMINFO
13500 "terminfo",
13501#endif
13502#ifdef FEAT_TERMRESPONSE
13503 "termresponse",
13504#endif
13505#ifdef FEAT_TEXTOBJ
13506 "textobjects",
13507#endif
13508#ifdef HAVE_TGETENT
13509 "tgetent",
13510#endif
13511#ifdef FEAT_TITLE
13512 "title",
13513#endif
13514#ifdef FEAT_TOOLBAR
13515 "toolbar",
13516#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013517#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13518 "unnamedplus",
13519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520#ifdef FEAT_USR_CMDS
13521 "user-commands", /* was accidentally included in 5.4 */
13522 "user_commands",
13523#endif
13524#ifdef FEAT_VIMINFO
13525 "viminfo",
13526#endif
13527#ifdef FEAT_VERTSPLIT
13528 "vertsplit",
13529#endif
13530#ifdef FEAT_VIRTUALEDIT
13531 "virtualedit",
13532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534#ifdef FEAT_VISUALEXTRA
13535 "visualextra",
13536#endif
13537#ifdef FEAT_VREPLACE
13538 "vreplace",
13539#endif
13540#ifdef FEAT_WILDIGN
13541 "wildignore",
13542#endif
13543#ifdef FEAT_WILDMENU
13544 "wildmenu",
13545#endif
13546#ifdef FEAT_WINDOWS
13547 "windows",
13548#endif
13549#ifdef FEAT_WAK
13550 "winaltkeys",
13551#endif
13552#ifdef FEAT_WRITEBACKUP
13553 "writebackup",
13554#endif
13555#ifdef FEAT_XIM
13556 "xim",
13557#endif
13558#ifdef FEAT_XFONTSET
13559 "xfontset",
13560#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013561#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013562 "xpm",
13563 "xpm_w32", /* for backward compatibility */
13564#else
13565# if defined(HAVE_XPM)
13566 "xpm",
13567# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569#ifdef USE_XSMP
13570 "xsmp",
13571#endif
13572#ifdef USE_XSMP_INTERACT
13573 "xsmp_interact",
13574#endif
13575#ifdef FEAT_XCLIPBOARD
13576 "xterm_clipboard",
13577#endif
13578#ifdef FEAT_XTERM_SAVE
13579 "xterm_save",
13580#endif
13581#if defined(UNIX) && defined(FEAT_X11)
13582 "X11",
13583#endif
13584 NULL
13585 };
13586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 for (i = 0; has_list[i] != NULL; ++i)
13589 if (STRICMP(name, has_list[i]) == 0)
13590 {
13591 n = TRUE;
13592 break;
13593 }
13594
13595 if (n == FALSE)
13596 {
13597 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013598 {
13599 if (name[5] == '-'
13600 && STRLEN(name) > 11
13601 && vim_isdigit(name[6])
13602 && vim_isdigit(name[8])
13603 && vim_isdigit(name[10]))
13604 {
13605 int major = atoi((char *)name + 6);
13606 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013607
13608 /* Expect "patch-9.9.01234". */
13609 n = (major < VIM_VERSION_MAJOR
13610 || (major == VIM_VERSION_MAJOR
13611 && (minor < VIM_VERSION_MINOR
13612 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013613 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013614 }
13615 else
13616 n = has_patch(atoi((char *)name + 5));
13617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 else if (STRICMP(name, "vim_starting") == 0)
13619 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013620#ifdef FEAT_MBYTE
13621 else if (STRICMP(name, "multi_byte_encoding") == 0)
13622 n = has_mbyte;
13623#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013624#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13625 else if (STRICMP(name, "balloon_multiline") == 0)
13626 n = multiline_balloon_available();
13627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628#ifdef DYNAMIC_TCL
13629 else if (STRICMP(name, "tcl") == 0)
13630 n = tcl_enabled(FALSE);
13631#endif
13632#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13633 else if (STRICMP(name, "iconv") == 0)
13634 n = iconv_enabled(FALSE);
13635#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013636#ifdef DYNAMIC_LUA
13637 else if (STRICMP(name, "lua") == 0)
13638 n = lua_enabled(FALSE);
13639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013640#ifdef DYNAMIC_MZSCHEME
13641 else if (STRICMP(name, "mzscheme") == 0)
13642 n = mzscheme_enabled(FALSE);
13643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644#ifdef DYNAMIC_RUBY
13645 else if (STRICMP(name, "ruby") == 0)
13646 n = ruby_enabled(FALSE);
13647#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013648#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649#ifdef DYNAMIC_PYTHON
13650 else if (STRICMP(name, "python") == 0)
13651 n = python_enabled(FALSE);
13652#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013653#endif
13654#ifdef FEAT_PYTHON3
13655#ifdef DYNAMIC_PYTHON3
13656 else if (STRICMP(name, "python3") == 0)
13657 n = python3_enabled(FALSE);
13658#endif
13659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660#ifdef DYNAMIC_PERL
13661 else if (STRICMP(name, "perl") == 0)
13662 n = perl_enabled(FALSE);
13663#endif
13664#ifdef FEAT_GUI
13665 else if (STRICMP(name, "gui_running") == 0)
13666 n = (gui.in_use || gui.starting);
13667# ifdef FEAT_GUI_W32
13668 else if (STRICMP(name, "gui_win32s") == 0)
13669 n = gui_is_win32s();
13670# endif
13671# ifdef FEAT_BROWSE
13672 else if (STRICMP(name, "browse") == 0)
13673 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13674# endif
13675#endif
13676#ifdef FEAT_SYN_HL
13677 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013678 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679#endif
13680#if defined(WIN3264)
13681 else if (STRICMP(name, "win95") == 0)
13682 n = mch_windows95();
13683#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013684#ifdef FEAT_NETBEANS_INTG
13685 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013686 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 }
13689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691}
13692
13693/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013694 * "has_key()" function
13695 */
13696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013697f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013698{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013699 if (argvars[0].v_type != VAR_DICT)
13700 {
13701 EMSG(_(e_dictreq));
13702 return;
13703 }
13704 if (argvars[0].vval.v_dict == NULL)
13705 return;
13706
13707 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013708 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013709}
13710
13711/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013712 * "haslocaldir()" function
13713 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013715f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013716{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013717 win_T *wp = NULL;
13718
13719 wp = find_tabwin(&argvars[0], &argvars[1]);
13720 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013721}
13722
13723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 * "hasmapto()" function
13725 */
13726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013727f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728{
13729 char_u *name;
13730 char_u *mode;
13731 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013732 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013734 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013735 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736 mode = (char_u *)"nvo";
13737 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013740 if (argvars[2].v_type != VAR_UNKNOWN)
13741 abbr = get_tv_number(&argvars[2]);
13742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743
Bram Moolenaar2c932302006-03-18 21:42:09 +000013744 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013745 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748}
13749
13750/*
13751 * "histadd()" function
13752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013754f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755{
13756#ifdef FEAT_CMDHIST
13757 int histype;
13758 char_u *str;
13759 char_u buf[NUMBUFLEN];
13760#endif
13761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763 if (check_restricted() || check_secure())
13764 return;
13765#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013766 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13767 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 if (histype >= 0)
13769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013770 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 if (*str != NUL)
13772 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013773 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013775 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 return;
13777 }
13778 }
13779#endif
13780}
13781
13782/*
13783 * "histdel()" function
13784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013786f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787{
13788#ifdef FEAT_CMDHIST
13789 int n;
13790 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013791 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013793 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13794 if (str == NULL)
13795 n = 0;
13796 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013798 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013799 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013801 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013802 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 else
13804 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013805 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806 get_tv_string_buf(&argvars[1], buf));
13807 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808#endif
13809}
13810
13811/*
13812 * "histget()" function
13813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013815f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816{
13817#ifdef FEAT_CMDHIST
13818 int type;
13819 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13823 if (str == NULL)
13824 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 {
13827 type = get_histtype(str);
13828 if (argvars[1].v_type == VAR_UNKNOWN)
13829 idx = get_history_idx(type);
13830 else
13831 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13832 /* -1 on type error */
13833 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013836 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013838 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839}
13840
13841/*
13842 * "histnr()" function
13843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013845f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846{
13847 int i;
13848
13849#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013850 char_u *history = get_tv_string_chk(&argvars[0]);
13851
13852 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 if (i >= HIST_CMD && i < HIST_COUNT)
13854 i = get_history_idx(i);
13855 else
13856#endif
13857 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013858 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859}
13860
13861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 * "highlightID(name)" function
13863 */
13864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013865f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013867 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868}
13869
13870/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013871 * "highlight_exists()" function
13872 */
13873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013874f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013875{
13876 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13877}
13878
13879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 * "hostname()" function
13881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013883f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884{
13885 char_u hostname[256];
13886
13887 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013888 rettv->v_type = VAR_STRING;
13889 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890}
13891
13892/*
13893 * iconv() function
13894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013896f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897{
13898#ifdef FEAT_MBYTE
13899 char_u buf1[NUMBUFLEN];
13900 char_u buf2[NUMBUFLEN];
13901 char_u *from, *to, *str;
13902 vimconv_T vimconv;
13903#endif
13904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013905 rettv->v_type = VAR_STRING;
13906 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907
13908#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909 str = get_tv_string(&argvars[0]);
13910 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13911 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 vimconv.vc_type = CONV_NONE;
13913 convert_setup(&vimconv, from, to);
13914
13915 /* If the encodings are equal, no conversion needed. */
13916 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013919 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920
13921 convert_setup(&vimconv, NULL, NULL);
13922 vim_free(from);
13923 vim_free(to);
13924#endif
13925}
13926
13927/*
13928 * "indent()" function
13929 */
13930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013931f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932{
13933 linenr_T lnum;
13934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013935 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013939 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940}
13941
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013942/*
13943 * "index()" function
13944 */
13945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013946f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013947{
Bram Moolenaar33570922005-01-25 22:26:29 +000013948 list_T *l;
13949 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013950 long idx = 0;
13951 int ic = FALSE;
13952
13953 rettv->vval.v_number = -1;
13954 if (argvars[0].v_type != VAR_LIST)
13955 {
13956 EMSG(_(e_listreq));
13957 return;
13958 }
13959 l = argvars[0].vval.v_list;
13960 if (l != NULL)
13961 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013962 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013965 int error = FALSE;
13966
Bram Moolenaar758711c2005-02-02 23:11:38 +000013967 /* Start at specified item. Use the cached index that list_find()
13968 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013969 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013970 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013971 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013972 ic = get_tv_number_chk(&argvars[3], &error);
13973 if (error)
13974 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013975 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013976
Bram Moolenaar758711c2005-02-02 23:11:38 +000013977 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013978 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013979 {
13980 rettv->vval.v_number = idx;
13981 break;
13982 }
13983 }
13984}
13985
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986static int inputsecret_flag = 0;
13987
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013988static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013989
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013991 * This function is used by f_input() and f_inputdialog() functions. The third
13992 * argument to f_input() specifies the type of completion to use at the
13993 * prompt. The third argument to f_inputdialog() specifies the value to return
13994 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 */
13996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013997get_user_input(
13998 typval_T *argvars,
13999 typval_T *rettv,
14000 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014002 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 char_u *p = NULL;
14004 int c;
14005 char_u buf[NUMBUFLEN];
14006 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014007 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014008 int xp_type = EXPAND_NOTHING;
14009 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014011 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014012 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013
14014#ifdef NO_CONSOLE_INPUT
14015 /* While starting up, there is no place to enter text. */
14016 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#endif
14019
14020 cmd_silent = FALSE; /* Want to see the prompt. */
14021 if (prompt != NULL)
14022 {
14023 /* Only the part of the message after the last NL is considered as
14024 * prompt for the command line */
14025 p = vim_strrchr(prompt, '\n');
14026 if (p == NULL)
14027 p = prompt;
14028 else
14029 {
14030 ++p;
14031 c = *p;
14032 *p = NUL;
14033 msg_start();
14034 msg_clr_eos();
14035 msg_puts_attr(prompt, echo_attr);
14036 msg_didout = FALSE;
14037 msg_starthere();
14038 *p = c;
14039 }
14040 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 if (argvars[1].v_type != VAR_UNKNOWN)
14043 {
14044 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14045 if (defstr != NULL)
14046 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014048 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014049 {
14050 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014051 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014052 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014053
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014054 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014055 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014056
Bram Moolenaar4463f292005-09-25 22:20:24 +000014057 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14058 if (xp_name == NULL)
14059 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014060
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014061 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014062
Bram Moolenaar4463f292005-09-25 22:20:24 +000014063 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14064 &xp_arg) == FAIL)
14065 return;
14066 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014067 }
14068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014069 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014070 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014071 int save_ex_normal_busy = ex_normal_busy;
14072 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014074 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14075 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014076 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014077 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014078 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014079 && argvars[1].v_type != VAR_UNKNOWN
14080 && argvars[2].v_type != VAR_UNKNOWN)
14081 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14082 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014083
14084 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 /* since the user typed this, no need to wait for return */
14087 need_wait_return = FALSE;
14088 msg_didout = FALSE;
14089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 cmd_silent = cmd_silent_save;
14091}
14092
14093/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014094 * "input()" function
14095 * Also handles inputsecret() when inputsecret is set.
14096 */
14097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014098f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014099{
14100 get_user_input(argvars, rettv, FALSE);
14101}
14102
14103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 * "inputdialog()" function
14105 */
14106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014107f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108{
14109#if defined(FEAT_GUI_TEXTDIALOG)
14110 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14111 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14112 {
14113 char_u *message;
14114 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014115 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014117 message = get_tv_string_chk(&argvars[0]);
14118 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014119 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014120 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 else
14122 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014123 if (message != NULL && defstr != NULL
14124 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014125 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014126 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 else
14128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014129 if (message != NULL && defstr != NULL
14130 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014131 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 rettv->vval.v_string = vim_strsave(
14133 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014135 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 }
14139 else
14140#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014141 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142}
14143
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014144/*
14145 * "inputlist()" function
14146 */
14147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014148f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014149{
14150 listitem_T *li;
14151 int selected;
14152 int mouse_used;
14153
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014154#ifdef NO_CONSOLE_INPUT
14155 /* While starting up, there is no place to enter text. */
14156 if (no_console_input())
14157 return;
14158#endif
14159 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14160 {
14161 EMSG2(_(e_listarg), "inputlist()");
14162 return;
14163 }
14164
14165 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014166 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014167 lines_left = Rows; /* avoid more prompt */
14168 msg_scroll = TRUE;
14169 msg_clr_eos();
14170
14171 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14172 {
14173 msg_puts(get_tv_string(&li->li_tv));
14174 msg_putchar('\n');
14175 }
14176
14177 /* Ask for choice. */
14178 selected = prompt_for_number(&mouse_used);
14179 if (mouse_used)
14180 selected -= lines_left;
14181
14182 rettv->vval.v_number = selected;
14183}
14184
14185
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14187
14188/*
14189 * "inputrestore()" function
14190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014192f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193{
14194 if (ga_userinput.ga_len > 0)
14195 {
14196 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14198 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014199 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 }
14201 else if (p_verbose > 1)
14202 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014203 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014204 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 }
14206}
14207
14208/*
14209 * "inputsave()" function
14210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014212f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014214 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 if (ga_grow(&ga_userinput, 1) == OK)
14216 {
14217 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14218 + ga_userinput.ga_len);
14219 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014220 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221 }
14222 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014223 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224}
14225
14226/*
14227 * "inputsecret()" function
14228 */
14229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014230f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231{
14232 ++cmdline_star;
14233 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014234 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 --cmdline_star;
14236 --inputsecret_flag;
14237}
14238
14239/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014240 * "insert()" function
14241 */
14242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014243f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014244{
14245 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014246 listitem_T *item;
14247 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014249
14250 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014251 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014252 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014253 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014254 {
14255 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 before = get_tv_number_chk(&argvars[2], &error);
14257 if (error)
14258 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014259
Bram Moolenaar758711c2005-02-02 23:11:38 +000014260 if (before == l->lv_len)
14261 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014262 else
14263 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014264 item = list_find(l, before);
14265 if (item == NULL)
14266 {
14267 EMSGN(_(e_listidx), before);
14268 l = NULL;
14269 }
14270 }
14271 if (l != NULL)
14272 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014273 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014274 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014275 }
14276 }
14277}
14278
14279/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014280 * "invert(expr)" function
14281 */
14282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014283f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014284{
14285 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14286}
14287
14288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 * "isdirectory()" function
14290 */
14291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014292f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014294 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295}
14296
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014297/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014298 * "islocked()" function
14299 */
14300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014301f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014302{
14303 lval_T lv;
14304 char_u *end;
14305 dictitem_T *di;
14306
14307 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014308 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14309 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014310 if (end != NULL && lv.ll_name != NULL)
14311 {
14312 if (*end != NUL)
14313 EMSG(_(e_trailing));
14314 else
14315 {
14316 if (lv.ll_tv == NULL)
14317 {
14318 if (check_changedtick(lv.ll_name))
14319 rettv->vval.v_number = 1; /* always locked */
14320 else
14321 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014322 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014323 if (di != NULL)
14324 {
14325 /* Consider a variable locked when:
14326 * 1. the variable itself is locked
14327 * 2. the value of the variable is locked.
14328 * 3. the List or Dict value is locked.
14329 */
14330 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14331 || tv_islocked(&di->di_tv));
14332 }
14333 }
14334 }
14335 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014336 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014337 else if (lv.ll_newkey != NULL)
14338 EMSG2(_(e_dictkey), lv.ll_newkey);
14339 else if (lv.ll_list != NULL)
14340 /* List item. */
14341 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14342 else
14343 /* Dictionary item. */
14344 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14345 }
14346 }
14347
14348 clear_lval(&lv);
14349}
14350
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014351static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014352
14353/*
14354 * Turn a dict into a list:
14355 * "what" == 0: list of keys
14356 * "what" == 1: list of values
14357 * "what" == 2: list of items
14358 */
14359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014360dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014361{
Bram Moolenaar33570922005-01-25 22:26:29 +000014362 list_T *l2;
14363 dictitem_T *di;
14364 hashitem_T *hi;
14365 listitem_T *li;
14366 listitem_T *li2;
14367 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014368 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014369
Bram Moolenaar8c711452005-01-14 21:53:12 +000014370 if (argvars[0].v_type != VAR_DICT)
14371 {
14372 EMSG(_(e_dictreq));
14373 return;
14374 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014375 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014376 return;
14377
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014378 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014379 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014380
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014381 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014384 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014385 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014386 --todo;
14387 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014388
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014389 li = listitem_alloc();
14390 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014391 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014392 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014393
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014394 if (what == 0)
14395 {
14396 /* keys() */
14397 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014398 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014399 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14400 }
14401 else if (what == 1)
14402 {
14403 /* values() */
14404 copy_tv(&di->di_tv, &li->li_tv);
14405 }
14406 else
14407 {
14408 /* items() */
14409 l2 = list_alloc();
14410 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014411 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014412 li->li_tv.vval.v_list = l2;
14413 if (l2 == NULL)
14414 break;
14415 ++l2->lv_refcount;
14416
14417 li2 = listitem_alloc();
14418 if (li2 == NULL)
14419 break;
14420 list_append(l2, li2);
14421 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014422 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014423 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14424
14425 li2 = listitem_alloc();
14426 if (li2 == NULL)
14427 break;
14428 list_append(l2, li2);
14429 copy_tv(&di->di_tv, &li2->li_tv);
14430 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014431 }
14432 }
14433}
14434
14435/*
14436 * "items(dict)" function
14437 */
14438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014439f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014440{
14441 dict_list(argvars, rettv, 2);
14442}
14443
Bram Moolenaar835dc632016-02-07 14:27:38 +010014444#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014445
14446# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014447/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014448 * "job_getchannel()" function
14449 */
14450 static void
14451f_job_getchannel(typval_T *argvars, typval_T *rettv)
14452{
14453 if (argvars[0].v_type != VAR_JOB)
14454 EMSG(_(e_invarg));
14455 else
14456 {
14457 job_T *job = argvars[0].vval.v_job;
14458
Bram Moolenaar77073442016-02-13 23:23:53 +010014459 rettv->v_type = VAR_CHANNEL;
14460 rettv->vval.v_channel = job->jv_channel;
14461 if (job->jv_channel != NULL)
14462 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014463 }
14464}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014465# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014466
14467/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014468 * "job_start()" function
14469 */
14470 static void
14471f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14472{
14473 job_T *job;
14474 char_u *cmd = NULL;
14475#if defined(UNIX)
14476# define USE_ARGV
14477 char **argv = NULL;
14478 int argc = 0;
14479#else
14480 garray_T ga;
14481#endif
14482
14483 rettv->v_type = VAR_JOB;
14484 job = job_alloc();
14485 rettv->vval.v_job = job;
14486 if (job == NULL)
14487 return;
14488
14489 rettv->vval.v_job->jv_status = JOB_FAILED;
14490#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014491 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014492#endif
14493
14494 if (argvars[0].v_type == VAR_STRING)
14495 {
14496 /* Command is a string. */
14497 cmd = argvars[0].vval.v_string;
14498#ifdef USE_ARGV
14499 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14500 return;
14501 argv[argc] = NULL;
14502#endif
14503 }
14504 else if (argvars[0].v_type != VAR_LIST
14505 || argvars[0].vval.v_list == NULL
14506 || argvars[0].vval.v_list->lv_len < 1)
14507 {
14508 EMSG(_(e_invarg));
14509 return;
14510 }
14511 else
14512 {
14513 list_T *l = argvars[0].vval.v_list;
14514 listitem_T *li;
14515 char_u *s;
14516
14517#ifdef USE_ARGV
14518 /* Pass argv[] to mch_call_shell(). */
14519 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14520 if (argv == NULL)
14521 return;
14522#endif
14523 for (li = l->lv_first; li != NULL; li = li->li_next)
14524 {
14525 s = get_tv_string_chk(&li->li_tv);
14526 if (s == NULL)
14527 goto theend;
14528#ifdef USE_ARGV
14529 argv[argc++] = (char *)s;
14530#else
14531 if (li != l->lv_first)
14532 {
14533 s = vim_strsave_shellescape(s, FALSE, TRUE);
14534 if (s == NULL)
14535 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014536 ga_concat(&ga, s);
14537 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014538 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014539 else
14540 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014541 if (li->li_next != NULL)
14542 ga_append(&ga, ' ');
14543#endif
14544 }
14545#ifdef USE_ARGV
14546 argv[argc] = NULL;
14547#else
14548 cmd = ga.ga_data;
14549#endif
14550 }
14551#ifdef USE_ARGV
14552 mch_start_job(argv, job);
14553#else
14554 mch_start_job(cmd, job);
14555#endif
14556
14557theend:
14558#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014559 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014560#else
14561 vim_free(ga.ga_data);
14562#endif
14563}
14564
14565/*
14566 * "job_status()" function
14567 */
14568 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014569f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014570{
14571 char *result;
14572
14573 if (argvars[0].v_type != VAR_JOB)
14574 EMSG(_(e_invarg));
14575 else
14576 {
14577 job_T *job = argvars[0].vval.v_job;
14578
14579 if (job->jv_status == JOB_ENDED)
14580 /* No need to check, dead is dead. */
14581 result = "dead";
14582 else if (job->jv_status == JOB_FAILED)
14583 result = "fail";
14584 else
14585 result = mch_job_status(job);
14586 rettv->v_type = VAR_STRING;
14587 rettv->vval.v_string = vim_strsave((char_u *)result);
14588 }
14589}
14590
14591/*
14592 * "job_stop()" function
14593 */
14594 static void
14595f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14596{
14597 if (argvars[0].v_type != VAR_JOB)
14598 EMSG(_(e_invarg));
14599 else
14600 {
14601 char_u *arg;
14602
14603 if (argvars[1].v_type == VAR_UNKNOWN)
14604 arg = (char_u *)"";
14605 else
14606 {
14607 arg = get_tv_string_chk(&argvars[1]);
14608 if (arg == NULL)
14609 {
14610 EMSG(_(e_invarg));
14611 return;
14612 }
14613 }
14614 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14615 rettv->vval.v_number = 0;
14616 else
14617 rettv->vval.v_number = 1;
14618 }
14619}
14620#endif
14621
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014623 * "join()" function
14624 */
14625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014626f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014627{
14628 garray_T ga;
14629 char_u *sep;
14630
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014631 if (argvars[0].v_type != VAR_LIST)
14632 {
14633 EMSG(_(e_listreq));
14634 return;
14635 }
14636 if (argvars[0].vval.v_list == NULL)
14637 return;
14638 if (argvars[1].v_type == VAR_UNKNOWN)
14639 sep = (char_u *)" ";
14640 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014641 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014642
14643 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014644
14645 if (sep != NULL)
14646 {
14647 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014648 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014649 ga_append(&ga, NUL);
14650 rettv->vval.v_string = (char_u *)ga.ga_data;
14651 }
14652 else
14653 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014654}
14655
14656/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014657 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014658 */
14659 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014660f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014661{
14662 js_read_T reader;
14663
14664 reader.js_buf = get_tv_string(&argvars[0]);
14665 reader.js_fill = NULL;
14666 reader.js_used = 0;
14667 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14668 EMSG(_(e_invarg));
14669}
14670
14671/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014672 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014673 */
14674 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014675f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014676{
14677 rettv->v_type = VAR_STRING;
14678 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14679}
14680
14681/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014682 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014683 */
14684 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014685f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014686{
14687 js_read_T reader;
14688
14689 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014690 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014691 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014692 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014693 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014694}
14695
14696/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014697 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014698 */
14699 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014700f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014701{
14702 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014703 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014704}
14705
14706/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014707 * "keys()" function
14708 */
14709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014710f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014711{
14712 dict_list(argvars, rettv, 0);
14713}
14714
14715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 * "last_buffer_nr()" function.
14717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014719f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720{
14721 int n = 0;
14722 buf_T *buf;
14723
14724 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14725 if (n < buf->b_fnum)
14726 n = buf->b_fnum;
14727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014728 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014729}
14730
14731/*
14732 * "len()" function
14733 */
14734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014735f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014736{
14737 switch (argvars[0].v_type)
14738 {
14739 case VAR_STRING:
14740 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014741 rettv->vval.v_number = (varnumber_T)STRLEN(
14742 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014743 break;
14744 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014745 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014746 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014747 case VAR_DICT:
14748 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14749 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014750 case VAR_UNKNOWN:
14751 case VAR_SPECIAL:
14752 case VAR_FLOAT:
14753 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014754 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014755 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014756 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014757 break;
14758 }
14759}
14760
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014761static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014762
14763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014764libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014765{
14766#ifdef FEAT_LIBCALL
14767 char_u *string_in;
14768 char_u **string_result;
14769 int nr_result;
14770#endif
14771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014772 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014773 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014775
14776 if (check_restricted() || check_secure())
14777 return;
14778
14779#ifdef FEAT_LIBCALL
14780 /* The first two args must be strings, otherwise its meaningless */
14781 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14782 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014783 string_in = NULL;
14784 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014785 string_in = argvars[2].vval.v_string;
14786 if (type == VAR_NUMBER)
14787 string_result = NULL;
14788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014789 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014790 if (mch_libcall(argvars[0].vval.v_string,
14791 argvars[1].vval.v_string,
14792 string_in,
14793 argvars[2].vval.v_number,
14794 string_result,
14795 &nr_result) == OK
14796 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014797 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014798 }
14799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800}
14801
14802/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014803 * "libcall()" function
14804 */
14805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014806f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014807{
14808 libcall_common(argvars, rettv, VAR_STRING);
14809}
14810
14811/*
14812 * "libcallnr()" function
14813 */
14814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014815f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014816{
14817 libcall_common(argvars, rettv, VAR_NUMBER);
14818}
14819
14820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 * "line(string)" function
14822 */
14823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014824f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825{
14826 linenr_T lnum = 0;
14827 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014828 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014830 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831 if (fp != NULL)
14832 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834}
14835
14836/*
14837 * "line2byte(lnum)" function
14838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014840f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841{
14842#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014843 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844#else
14845 linenr_T lnum;
14846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014847 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014849 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014851 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14852 if (rettv->vval.v_number >= 0)
14853 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854#endif
14855}
14856
14857/*
14858 * "lispindent(lnum)" function
14859 */
14860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014861f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862{
14863#ifdef FEAT_LISP
14864 pos_T pos;
14865 linenr_T lnum;
14866
14867 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14870 {
14871 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014872 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 curwin->w_cursor = pos;
14874 }
14875 else
14876#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878}
14879
14880/*
14881 * "localtime()" function
14882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014884f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014886 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887}
14888
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014889static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890
14891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014892get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893{
14894 char_u *keys;
14895 char_u *which;
14896 char_u buf[NUMBUFLEN];
14897 char_u *keys_buf = NULL;
14898 char_u *rhs;
14899 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014900 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014901 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014902 mapblock_T *mp;
14903 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904
14905 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014906 rettv->v_type = VAR_STRING;
14907 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 if (*keys == NUL)
14911 return;
14912
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014913 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014915 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014916 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014917 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014918 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014919 if (argvars[3].v_type != VAR_UNKNOWN)
14920 get_dict = get_tv_number(&argvars[3]);
14921 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923 else
14924 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014925 if (which == NULL)
14926 return;
14927
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 mode = get_map_mode(&which, 0);
14929
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014930 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014931 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014933
14934 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014936 /* Return a string. */
14937 if (rhs != NULL)
14938 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939
Bram Moolenaarbd743252010-10-20 21:23:33 +020014940 }
14941 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14942 {
14943 /* Return a dictionary. */
14944 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14945 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14946 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947
Bram Moolenaarbd743252010-10-20 21:23:33 +020014948 dict_add_nr_str(dict, "lhs", 0L, lhs);
14949 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14950 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14951 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14952 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14953 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14954 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014955 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014956 dict_add_nr_str(dict, "mode", 0L, mapmode);
14957
14958 vim_free(lhs);
14959 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 }
14961}
14962
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014963#ifdef FEAT_FLOAT
14964/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014965 * "log()" function
14966 */
14967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014968f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014969{
14970 float_T f;
14971
14972 rettv->v_type = VAR_FLOAT;
14973 if (get_float_arg(argvars, &f) == OK)
14974 rettv->vval.v_float = log(f);
14975 else
14976 rettv->vval.v_float = 0.0;
14977}
14978
14979/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014980 * "log10()" function
14981 */
14982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014983f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014984{
14985 float_T f;
14986
14987 rettv->v_type = VAR_FLOAT;
14988 if (get_float_arg(argvars, &f) == OK)
14989 rettv->vval.v_float = log10(f);
14990 else
14991 rettv->vval.v_float = 0.0;
14992}
14993#endif
14994
Bram Moolenaar1dced572012-04-05 16:54:08 +020014995#ifdef FEAT_LUA
14996/*
14997 * "luaeval()" function
14998 */
14999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015000f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015001{
15002 char_u *str;
15003 char_u buf[NUMBUFLEN];
15004
15005 str = get_tv_string_buf(&argvars[0], buf);
15006 do_luaeval(str, argvars + 1, rettv);
15007}
15008#endif
15009
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015011 * "map()" function
15012 */
15013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015014f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015015{
15016 filter_map(argvars, rettv, TRUE);
15017}
15018
15019/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015020 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 */
15022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015023f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026}
15027
15028/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015029 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 */
15031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015032f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015034 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035}
15036
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015037static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038
15039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015040find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015042 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015043 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015044 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 char_u *pat;
15046 regmatch_T regmatch;
15047 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015048 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 char_u *save_cpo;
15050 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015051 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015052 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015053 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015054 list_T *l = NULL;
15055 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015056 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015057 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058
15059 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15060 save_cpo = p_cpo;
15061 p_cpo = (char_u *)"";
15062
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015063 rettv->vval.v_number = -1;
15064 if (type == 3)
15065 {
15066 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015067 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015068 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015069 }
15070 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015072 rettv->v_type = VAR_STRING;
15073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015076 if (argvars[0].v_type == VAR_LIST)
15077 {
15078 if ((l = argvars[0].vval.v_list) == NULL)
15079 goto theend;
15080 li = l->lv_first;
15081 }
15082 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015083 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015084 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015085 len = (long)STRLEN(str);
15086 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15089 if (pat == NULL)
15090 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015091
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015094 int error = FALSE;
15095
15096 start = get_tv_number_chk(&argvars[2], &error);
15097 if (error)
15098 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015099 if (l != NULL)
15100 {
15101 li = list_find(l, start);
15102 if (li == NULL)
15103 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015104 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015105 }
15106 else
15107 {
15108 if (start < 0)
15109 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015110 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015111 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015112 /* When "count" argument is there ignore matches before "start",
15113 * otherwise skip part of the string. Differs when pattern is "^"
15114 * or "\<". */
15115 if (argvars[3].v_type != VAR_UNKNOWN)
15116 startcol = start;
15117 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015118 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015119 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015120 len -= start;
15121 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015122 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015124 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015125 nth = get_tv_number_chk(&argvars[3], &error);
15126 if (error)
15127 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 }
15129
15130 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15131 if (regmatch.regprog != NULL)
15132 {
15133 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015134
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015135 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015136 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015137 if (l != NULL)
15138 {
15139 if (li == NULL)
15140 {
15141 match = FALSE;
15142 break;
15143 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015144 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015145 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015146 if (str == NULL)
15147 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015148 }
15149
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015150 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015151
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015152 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015153 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015154 if (l == NULL && !match)
15155 break;
15156
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015157 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015158 if (l != NULL)
15159 {
15160 li = li->li_next;
15161 ++idx;
15162 }
15163 else
15164 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015165#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015166 startcol = (colnr_T)(regmatch.startp[0]
15167 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015168#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015169 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015170#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015171 if (startcol > (colnr_T)len
15172 || str + startcol <= regmatch.startp[0])
15173 {
15174 match = FALSE;
15175 break;
15176 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015177 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015178 }
15179
15180 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015182 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015183 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015184 int i;
15185
15186 /* return list with matched string and submatches */
15187 for (i = 0; i < NSUBEXP; ++i)
15188 {
15189 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015190 {
15191 if (list_append_string(rettv->vval.v_list,
15192 (char_u *)"", 0) == FAIL)
15193 break;
15194 }
15195 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015196 regmatch.startp[i],
15197 (int)(regmatch.endp[i] - regmatch.startp[i]))
15198 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015199 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015200 }
15201 }
15202 else if (type == 2)
15203 {
15204 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015205 if (l != NULL)
15206 copy_tv(&li->li_tv, rettv);
15207 else
15208 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015210 }
15211 else if (l != NULL)
15212 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213 else
15214 {
15215 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015216 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217 (varnumber_T)(regmatch.startp[0] - str);
15218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015219 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015221 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222 }
15223 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015224 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225 }
15226
15227theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015228 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 p_cpo = save_cpo;
15230}
15231
15232/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015233 * "match()" function
15234 */
15235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015236f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237{
15238 find_some_match(argvars, rettv, 1);
15239}
15240
15241/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015242 * "matchadd()" function
15243 */
15244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015245f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015246{
15247#ifdef FEAT_SEARCH_EXTRA
15248 char_u buf[NUMBUFLEN];
15249 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15250 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15251 int prio = 10; /* default priority */
15252 int id = -1;
15253 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015254 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015255
15256 rettv->vval.v_number = -1;
15257
15258 if (grp == NULL || pat == NULL)
15259 return;
15260 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015261 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015262 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015263 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015264 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015265 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015266 if (argvars[4].v_type != VAR_UNKNOWN)
15267 {
15268 if (argvars[4].v_type != VAR_DICT)
15269 {
15270 EMSG(_(e_dictreq));
15271 return;
15272 }
15273 if (dict_find(argvars[4].vval.v_dict,
15274 (char_u *)"conceal", -1) != NULL)
15275 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15276 (char_u *)"conceal", FALSE);
15277 }
15278 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015279 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015280 if (error == TRUE)
15281 return;
15282 if (id >= 1 && id <= 3)
15283 {
15284 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15285 return;
15286 }
15287
Bram Moolenaar6561d522015-07-21 15:48:27 +020015288 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15289 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015290#endif
15291}
15292
15293/*
15294 * "matchaddpos()" function
15295 */
15296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015297f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015298{
15299#ifdef FEAT_SEARCH_EXTRA
15300 char_u buf[NUMBUFLEN];
15301 char_u *group;
15302 int prio = 10;
15303 int id = -1;
15304 int error = FALSE;
15305 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015306 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015307
15308 rettv->vval.v_number = -1;
15309
15310 group = get_tv_string_buf_chk(&argvars[0], buf);
15311 if (group == NULL)
15312 return;
15313
15314 if (argvars[1].v_type != VAR_LIST)
15315 {
15316 EMSG2(_(e_listarg), "matchaddpos()");
15317 return;
15318 }
15319 l = argvars[1].vval.v_list;
15320 if (l == NULL)
15321 return;
15322
15323 if (argvars[2].v_type != VAR_UNKNOWN)
15324 {
15325 prio = get_tv_number_chk(&argvars[2], &error);
15326 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015327 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015328 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015329 if (argvars[4].v_type != VAR_UNKNOWN)
15330 {
15331 if (argvars[4].v_type != VAR_DICT)
15332 {
15333 EMSG(_(e_dictreq));
15334 return;
15335 }
15336 if (dict_find(argvars[4].vval.v_dict,
15337 (char_u *)"conceal", -1) != NULL)
15338 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15339 (char_u *)"conceal", FALSE);
15340 }
15341 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015342 }
15343 if (error == TRUE)
15344 return;
15345
15346 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15347 if (id == 1 || id == 2)
15348 {
15349 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15350 return;
15351 }
15352
Bram Moolenaar6561d522015-07-21 15:48:27 +020015353 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15354 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015355#endif
15356}
15357
15358/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015359 * "matcharg()" function
15360 */
15361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015362f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015363{
15364 if (rettv_list_alloc(rettv) == OK)
15365 {
15366#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015367 int id = get_tv_number(&argvars[0]);
15368 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015369
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015370 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015371 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015372 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15373 {
15374 list_append_string(rettv->vval.v_list,
15375 syn_id2name(m->hlg_id), -1);
15376 list_append_string(rettv->vval.v_list, m->pattern, -1);
15377 }
15378 else
15379 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015380 list_append_string(rettv->vval.v_list, NULL, -1);
15381 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015382 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015383 }
15384#endif
15385 }
15386}
15387
15388/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015389 * "matchdelete()" function
15390 */
15391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015392f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015393{
15394#ifdef FEAT_SEARCH_EXTRA
15395 rettv->vval.v_number = match_delete(curwin,
15396 (int)get_tv_number(&argvars[0]), TRUE);
15397#endif
15398}
15399
15400/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015401 * "matchend()" function
15402 */
15403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015404f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015405{
15406 find_some_match(argvars, rettv, 0);
15407}
15408
15409/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015410 * "matchlist()" function
15411 */
15412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015413f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015414{
15415 find_some_match(argvars, rettv, 3);
15416}
15417
15418/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015419 * "matchstr()" function
15420 */
15421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015422f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015423{
15424 find_some_match(argvars, rettv, 2);
15425}
15426
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015427static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015428
15429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015430max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015431{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015432 long n = 0;
15433 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015434 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015435
15436 if (argvars[0].v_type == VAR_LIST)
15437 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015438 list_T *l;
15439 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015440
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015441 l = argvars[0].vval.v_list;
15442 if (l != NULL)
15443 {
15444 li = l->lv_first;
15445 if (li != NULL)
15446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015447 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015448 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015449 {
15450 li = li->li_next;
15451 if (li == NULL)
15452 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015453 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015454 if (domax ? i > n : i < n)
15455 n = i;
15456 }
15457 }
15458 }
15459 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015460 else if (argvars[0].v_type == VAR_DICT)
15461 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015462 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015463 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015464 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015465 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015466
15467 d = argvars[0].vval.v_dict;
15468 if (d != NULL)
15469 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015470 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015471 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015472 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015473 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015474 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015475 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015476 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015477 if (first)
15478 {
15479 n = i;
15480 first = FALSE;
15481 }
15482 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015483 n = i;
15484 }
15485 }
15486 }
15487 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015488 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015489 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015490 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015491}
15492
15493/*
15494 * "max()" function
15495 */
15496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015497f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015498{
15499 max_min(argvars, rettv, TRUE);
15500}
15501
15502/*
15503 * "min()" function
15504 */
15505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015506f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015507{
15508 max_min(argvars, rettv, FALSE);
15509}
15510
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015511static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015512
15513/*
15514 * Create the directory in which "dir" is located, and higher levels when
15515 * needed.
15516 */
15517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015518mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015519{
15520 char_u *p;
15521 char_u *updir;
15522 int r = FAIL;
15523
15524 /* Get end of directory name in "dir".
15525 * We're done when it's "/" or "c:/". */
15526 p = gettail_sep(dir);
15527 if (p <= get_past_head(dir))
15528 return OK;
15529
15530 /* If the directory exists we're done. Otherwise: create it.*/
15531 updir = vim_strnsave(dir, (int)(p - dir));
15532 if (updir == NULL)
15533 return FAIL;
15534 if (mch_isdir(updir))
15535 r = OK;
15536 else if (mkdir_recurse(updir, prot) == OK)
15537 r = vim_mkdir_emsg(updir, prot);
15538 vim_free(updir);
15539 return r;
15540}
15541
15542#ifdef vim_mkdir
15543/*
15544 * "mkdir()" function
15545 */
15546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015547f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015548{
15549 char_u *dir;
15550 char_u buf[NUMBUFLEN];
15551 int prot = 0755;
15552
15553 rettv->vval.v_number = FAIL;
15554 if (check_restricted() || check_secure())
15555 return;
15556
15557 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015558 if (*dir == NUL)
15559 rettv->vval.v_number = FAIL;
15560 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015561 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015562 if (*gettail(dir) == NUL)
15563 /* remove trailing slashes */
15564 *gettail_sep(dir) = NUL;
15565
15566 if (argvars[1].v_type != VAR_UNKNOWN)
15567 {
15568 if (argvars[2].v_type != VAR_UNKNOWN)
15569 prot = get_tv_number_chk(&argvars[2], NULL);
15570 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15571 mkdir_recurse(dir, prot);
15572 }
15573 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015574 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015575}
15576#endif
15577
Bram Moolenaar0d660222005-01-07 21:51:51 +000015578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 * "mode()" function
15580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015582f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015584 char_u buf[3];
15585
15586 buf[1] = NUL;
15587 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 if (VIsual_active)
15590 {
15591 if (VIsual_select)
15592 buf[0] = VIsual_mode + 's' - 'v';
15593 else
15594 buf[0] = VIsual_mode;
15595 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015596 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015597 || State == CONFIRM)
15598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015600 if (State == ASKMORE)
15601 buf[1] = 'm';
15602 else if (State == CONFIRM)
15603 buf[1] = '?';
15604 }
15605 else if (State == EXTERNCMD)
15606 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 else if (State & INSERT)
15608 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015609#ifdef FEAT_VREPLACE
15610 if (State & VREPLACE_FLAG)
15611 {
15612 buf[0] = 'R';
15613 buf[1] = 'v';
15614 }
15615 else
15616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 if (State & REPLACE_FLAG)
15618 buf[0] = 'R';
15619 else
15620 buf[0] = 'i';
15621 }
15622 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015625 if (exmode_active)
15626 buf[1] = 'v';
15627 }
15628 else if (exmode_active)
15629 {
15630 buf[0] = 'c';
15631 buf[1] = 'e';
15632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015636 if (finish_op)
15637 buf[1] = 'o';
15638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015640 /* Clear out the minor mode when the argument is not a non-zero number or
15641 * non-empty string. */
15642 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015643 buf[1] = NUL;
15644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015645 rettv->vval.v_string = vim_strsave(buf);
15646 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647}
15648
Bram Moolenaar429fa852013-04-15 12:27:36 +020015649#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015650/*
15651 * "mzeval()" function
15652 */
15653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015654f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015655{
15656 char_u *str;
15657 char_u buf[NUMBUFLEN];
15658
15659 str = get_tv_string_buf(&argvars[0], buf);
15660 do_mzeval(str, rettv);
15661}
Bram Moolenaar75676462013-01-30 14:55:42 +010015662
15663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015664mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015665{
15666 typval_T argvars[3];
15667
15668 argvars[0].v_type = VAR_STRING;
15669 argvars[0].vval.v_string = name;
15670 copy_tv(args, &argvars[1]);
15671 argvars[2].v_type = VAR_UNKNOWN;
15672 f_call(argvars, rettv);
15673 clear_tv(&argvars[1]);
15674}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015675#endif
15676
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015678 * "nextnonblank()" function
15679 */
15680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015681f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015682{
15683 linenr_T lnum;
15684
15685 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015687 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688 {
15689 lnum = 0;
15690 break;
15691 }
15692 if (*skipwhite(ml_get(lnum)) != NUL)
15693 break;
15694 }
15695 rettv->vval.v_number = lnum;
15696}
15697
15698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 * "nr2char()" function
15700 */
15701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015702f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703{
15704 char_u buf[NUMBUFLEN];
15705
15706#ifdef FEAT_MBYTE
15707 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015708 {
15709 int utf8 = 0;
15710
15711 if (argvars[1].v_type != VAR_UNKNOWN)
15712 utf8 = get_tv_number_chk(&argvars[1], NULL);
15713 if (utf8)
15714 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15715 else
15716 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 else
15719#endif
15720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015721 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 buf[1] = NUL;
15723 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015724 rettv->v_type = VAR_STRING;
15725 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015726}
15727
15728/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015729 * "or(expr, expr)" function
15730 */
15731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015732f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015733{
15734 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15735 | get_tv_number_chk(&argvars[1], NULL);
15736}
15737
15738/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015739 * "pathshorten()" function
15740 */
15741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015742f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015743{
15744 char_u *p;
15745
15746 rettv->v_type = VAR_STRING;
15747 p = get_tv_string_chk(&argvars[0]);
15748 if (p == NULL)
15749 rettv->vval.v_string = NULL;
15750 else
15751 {
15752 p = vim_strsave(p);
15753 rettv->vval.v_string = p;
15754 if (p != NULL)
15755 shorten_dir(p);
15756 }
15757}
15758
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015759#ifdef FEAT_PERL
15760/*
15761 * "perleval()" function
15762 */
15763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015764f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015765{
15766 char_u *str;
15767 char_u buf[NUMBUFLEN];
15768
15769 str = get_tv_string_buf(&argvars[0], buf);
15770 do_perleval(str, rettv);
15771}
15772#endif
15773
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015774#ifdef FEAT_FLOAT
15775/*
15776 * "pow()" function
15777 */
15778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015779f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015780{
15781 float_T fx, fy;
15782
15783 rettv->v_type = VAR_FLOAT;
15784 if (get_float_arg(argvars, &fx) == OK
15785 && get_float_arg(&argvars[1], &fy) == OK)
15786 rettv->vval.v_float = pow(fx, fy);
15787 else
15788 rettv->vval.v_float = 0.0;
15789}
15790#endif
15791
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015792/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793 * "prevnonblank()" function
15794 */
15795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015796f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797{
15798 linenr_T lnum;
15799
15800 lnum = get_tv_lnum(argvars);
15801 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15802 lnum = 0;
15803 else
15804 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15805 --lnum;
15806 rettv->vval.v_number = lnum;
15807}
15808
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015809/* This dummy va_list is here because:
15810 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15811 * - locally in the function results in a "used before set" warning
15812 * - using va_start() to initialize it gives "function with fixed args" error */
15813static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015814
Bram Moolenaar8c711452005-01-14 21:53:12 +000015815/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015816 * "printf()" function
15817 */
15818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015819f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015820{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015821 char_u buf[NUMBUFLEN];
15822 int len;
15823 char_u *s;
15824 int saved_did_emsg = did_emsg;
15825 char *fmt;
15826
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015827 rettv->v_type = VAR_STRING;
15828 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015829
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015830 /* Get the required length, allocate the buffer and do it for real. */
15831 did_emsg = FALSE;
15832 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15833 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15834 if (!did_emsg)
15835 {
15836 s = alloc(len + 1);
15837 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015838 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015839 rettv->vval.v_string = s;
15840 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015841 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015842 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015843 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015844}
15845
15846/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015847 * "pumvisible()" function
15848 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015850f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015851{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015852#ifdef FEAT_INS_EXPAND
15853 if (pum_visible())
15854 rettv->vval.v_number = 1;
15855#endif
15856}
15857
Bram Moolenaardb913952012-06-29 12:54:53 +020015858#ifdef FEAT_PYTHON3
15859/*
15860 * "py3eval()" function
15861 */
15862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015863f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015864{
15865 char_u *str;
15866 char_u buf[NUMBUFLEN];
15867
15868 str = get_tv_string_buf(&argvars[0], buf);
15869 do_py3eval(str, rettv);
15870}
15871#endif
15872
15873#ifdef FEAT_PYTHON
15874/*
15875 * "pyeval()" function
15876 */
15877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015878f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015879{
15880 char_u *str;
15881 char_u buf[NUMBUFLEN];
15882
15883 str = get_tv_string_buf(&argvars[0], buf);
15884 do_pyeval(str, rettv);
15885}
15886#endif
15887
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015888/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015889 * "range()" function
15890 */
15891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015892f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015893{
15894 long start;
15895 long end;
15896 long stride = 1;
15897 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015898 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015900 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015901 if (argvars[1].v_type == VAR_UNKNOWN)
15902 {
15903 end = start - 1;
15904 start = 0;
15905 }
15906 else
15907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015908 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015909 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015910 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015911 }
15912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015913 if (error)
15914 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015915 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015916 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015917 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015918 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015919 else
15920 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015921 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015922 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015923 if (list_append_number(rettv->vval.v_list,
15924 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015925 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015926 }
15927}
15928
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015929/*
15930 * "readfile()" function
15931 */
15932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015933f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015934{
15935 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015936 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015937 char_u *fname;
15938 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015939 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15940 int io_size = sizeof(buf);
15941 int readlen; /* size of last fread() */
15942 char_u *prev = NULL; /* previously read bytes, if any */
15943 long prevlen = 0; /* length of data in prev */
15944 long prevsize = 0; /* size of prev buffer */
15945 long maxline = MAXLNUM;
15946 long cnt = 0;
15947 char_u *p; /* position in buf */
15948 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015949
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015950 if (argvars[1].v_type != VAR_UNKNOWN)
15951 {
15952 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15953 binary = TRUE;
15954 if (argvars[2].v_type != VAR_UNKNOWN)
15955 maxline = get_tv_number(&argvars[2]);
15956 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015957
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015958 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015959 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015960
15961 /* Always open the file in binary mode, library functions have a mind of
15962 * their own about CR-LF conversion. */
15963 fname = get_tv_string(&argvars[0]);
15964 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15965 {
15966 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15967 return;
15968 }
15969
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015970 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015971 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015972 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015973
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015974 /* This for loop processes what was read, but is also entered at end
15975 * of file so that either:
15976 * - an incomplete line gets written
15977 * - a "binary" file gets an empty line at the end if it ends in a
15978 * newline. */
15979 for (p = buf, start = buf;
15980 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15981 ++p)
15982 {
15983 if (*p == '\n' || readlen <= 0)
15984 {
15985 listitem_T *li;
15986 char_u *s = NULL;
15987 long_u len = p - start;
15988
15989 /* Finished a line. Remove CRs before NL. */
15990 if (readlen > 0 && !binary)
15991 {
15992 while (len > 0 && start[len - 1] == '\r')
15993 --len;
15994 /* removal may cross back to the "prev" string */
15995 if (len == 0)
15996 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15997 --prevlen;
15998 }
15999 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016000 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016001 else
16002 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016003 /* Change "prev" buffer to be the right size. This way
16004 * the bytes are only copied once, and very long lines are
16005 * allocated only once. */
16006 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016007 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016008 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016009 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016010 prev = NULL; /* the list will own the string */
16011 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016012 }
16013 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016014 if (s == NULL)
16015 {
16016 do_outofmem_msg((long_u) prevlen + len + 1);
16017 failed = TRUE;
16018 break;
16019 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016020
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016021 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016022 {
16023 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016024 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016025 break;
16026 }
16027 li->li_tv.v_type = VAR_STRING;
16028 li->li_tv.v_lock = 0;
16029 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016030 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016031
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016032 start = p + 1; /* step over newline */
16033 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016034 break;
16035 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016036 else if (*p == NUL)
16037 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016038#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016039 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16040 * when finding the BF and check the previous two bytes. */
16041 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016042 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016043 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16044 * + 1, these may be in the "prev" string. */
16045 char_u back1 = p >= buf + 1 ? p[-1]
16046 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16047 char_u back2 = p >= buf + 2 ? p[-2]
16048 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16049 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016050
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016051 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016052 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016053 char_u *dest = p - 2;
16054
16055 /* Usually a BOM is at the beginning of a file, and so at
16056 * the beginning of a line; then we can just step over it.
16057 */
16058 if (start == dest)
16059 start = p + 1;
16060 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016061 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016062 /* have to shuffle buf to close gap */
16063 int adjust_prevlen = 0;
16064
16065 if (dest < buf)
16066 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016067 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016068 dest = buf;
16069 }
16070 if (readlen > p - buf + 1)
16071 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16072 readlen -= 3 - adjust_prevlen;
16073 prevlen -= adjust_prevlen;
16074 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016075 }
16076 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016077 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016078#endif
16079 } /* for */
16080
16081 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16082 break;
16083 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016084 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016085 /* There's part of a line in buf, store it in "prev". */
16086 if (p - start + prevlen >= prevsize)
16087 {
16088 /* need bigger "prev" buffer */
16089 char_u *newprev;
16090
16091 /* A common use case is ordinary text files and "prev" gets a
16092 * fragment of a line, so the first allocation is made
16093 * small, to avoid repeatedly 'allocing' large and
16094 * 'reallocing' small. */
16095 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016096 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016097 else
16098 {
16099 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016100 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016101 prevsize = grow50pc > growmin ? grow50pc : growmin;
16102 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016103 newprev = prev == NULL ? alloc(prevsize)
16104 : vim_realloc(prev, prevsize);
16105 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016106 {
16107 do_outofmem_msg((long_u)prevsize);
16108 failed = TRUE;
16109 break;
16110 }
16111 prev = newprev;
16112 }
16113 /* Add the line part to end of "prev". */
16114 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016115 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016116 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016117 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016118
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016119 /*
16120 * For a negative line count use only the lines at the end of the file,
16121 * free the rest.
16122 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016123 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016124 while (cnt > -maxline)
16125 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016126 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016127 --cnt;
16128 }
16129
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016130 if (failed)
16131 {
16132 list_free(rettv->vval.v_list, TRUE);
16133 /* readfile doc says an empty list is returned on error */
16134 rettv->vval.v_list = list_alloc();
16135 }
16136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016137 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016138 fclose(fd);
16139}
16140
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016141#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016142static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016143
16144/*
16145 * Convert a List to proftime_T.
16146 * Return FAIL when there is something wrong.
16147 */
16148 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016149list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016150{
16151 long n1, n2;
16152 int error = FALSE;
16153
16154 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16155 || arg->vval.v_list->lv_len != 2)
16156 return FAIL;
16157 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16158 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16159# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016160 tm->HighPart = n1;
16161 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016162# else
16163 tm->tv_sec = n1;
16164 tm->tv_usec = n2;
16165# endif
16166 return error ? FAIL : OK;
16167}
16168#endif /* FEAT_RELTIME */
16169
16170/*
16171 * "reltime()" function
16172 */
16173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016174f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016175{
16176#ifdef FEAT_RELTIME
16177 proftime_T res;
16178 proftime_T start;
16179
16180 if (argvars[0].v_type == VAR_UNKNOWN)
16181 {
16182 /* No arguments: get current time. */
16183 profile_start(&res);
16184 }
16185 else if (argvars[1].v_type == VAR_UNKNOWN)
16186 {
16187 if (list2proftime(&argvars[0], &res) == FAIL)
16188 return;
16189 profile_end(&res);
16190 }
16191 else
16192 {
16193 /* Two arguments: compute the difference. */
16194 if (list2proftime(&argvars[0], &start) == FAIL
16195 || list2proftime(&argvars[1], &res) == FAIL)
16196 return;
16197 profile_sub(&res, &start);
16198 }
16199
16200 if (rettv_list_alloc(rettv) == OK)
16201 {
16202 long n1, n2;
16203
16204# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016205 n1 = res.HighPart;
16206 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016207# else
16208 n1 = res.tv_sec;
16209 n2 = res.tv_usec;
16210# endif
16211 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16212 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16213 }
16214#endif
16215}
16216
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016217#ifdef FEAT_FLOAT
16218/*
16219 * "reltimefloat()" function
16220 */
16221 static void
16222f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16223{
16224# ifdef FEAT_RELTIME
16225 proftime_T tm;
16226# endif
16227
16228 rettv->v_type = VAR_FLOAT;
16229 rettv->vval.v_float = 0;
16230# ifdef FEAT_RELTIME
16231 if (list2proftime(&argvars[0], &tm) == OK)
16232 rettv->vval.v_float = profile_float(&tm);
16233# endif
16234}
16235#endif
16236
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016237/*
16238 * "reltimestr()" function
16239 */
16240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016241f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016242{
16243#ifdef FEAT_RELTIME
16244 proftime_T tm;
16245#endif
16246
16247 rettv->v_type = VAR_STRING;
16248 rettv->vval.v_string = NULL;
16249#ifdef FEAT_RELTIME
16250 if (list2proftime(&argvars[0], &tm) == OK)
16251 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16252#endif
16253}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016254
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016256static void make_connection(void);
16257static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016258
16259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016260make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261{
16262 if (X_DISPLAY == NULL
16263# ifdef FEAT_GUI
16264 && !gui.in_use
16265# endif
16266 )
16267 {
16268 x_force_connect = TRUE;
16269 setup_term_clip();
16270 x_force_connect = FALSE;
16271 }
16272}
16273
16274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016275check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016276{
16277 make_connection();
16278 if (X_DISPLAY == NULL)
16279 {
16280 EMSG(_("E240: No connection to Vim server"));
16281 return FAIL;
16282 }
16283 return OK;
16284}
16285#endif
16286
16287#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016288static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016289
16290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016291remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016292{
16293 char_u *server_name;
16294 char_u *keys;
16295 char_u *r = NULL;
16296 char_u buf[NUMBUFLEN];
16297# ifdef WIN32
16298 HWND w;
16299# else
16300 Window w;
16301# endif
16302
16303 if (check_restricted() || check_secure())
16304 return;
16305
16306# ifdef FEAT_X11
16307 if (check_connection() == FAIL)
16308 return;
16309# endif
16310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016311 server_name = get_tv_string_chk(&argvars[0]);
16312 if (server_name == NULL)
16313 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314 keys = get_tv_string_buf(&argvars[1], buf);
16315# ifdef WIN32
16316 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16317# else
16318 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16319 < 0)
16320# endif
16321 {
16322 if (r != NULL)
16323 EMSG(r); /* sending worked but evaluation failed */
16324 else
16325 EMSG2(_("E241: Unable to send to %s"), server_name);
16326 return;
16327 }
16328
16329 rettv->vval.v_string = r;
16330
16331 if (argvars[2].v_type != VAR_UNKNOWN)
16332 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016333 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016334 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016335 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016336
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016337 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016338 v.di_tv.v_type = VAR_STRING;
16339 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016340 idvar = get_tv_string_chk(&argvars[2]);
16341 if (idvar != NULL)
16342 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344 }
16345}
16346#endif
16347
16348/*
16349 * "remote_expr()" function
16350 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016352f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353{
16354 rettv->v_type = VAR_STRING;
16355 rettv->vval.v_string = NULL;
16356#ifdef FEAT_CLIENTSERVER
16357 remote_common(argvars, rettv, TRUE);
16358#endif
16359}
16360
16361/*
16362 * "remote_foreground()" function
16363 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016365f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016366{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016367#ifdef FEAT_CLIENTSERVER
16368# ifdef WIN32
16369 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016370 {
16371 char_u *server_name = get_tv_string_chk(&argvars[0]);
16372
16373 if (server_name != NULL)
16374 serverForeground(server_name);
16375 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376# else
16377 /* Send a foreground() expression to the server. */
16378 argvars[1].v_type = VAR_STRING;
16379 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16380 argvars[2].v_type = VAR_UNKNOWN;
16381 remote_common(argvars, rettv, TRUE);
16382 vim_free(argvars[1].vval.v_string);
16383# endif
16384#endif
16385}
16386
Bram Moolenaar0d660222005-01-07 21:51:51 +000016387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016388f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389{
16390#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392 char_u *s = NULL;
16393# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016394 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016396 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397
16398 if (check_restricted() || check_secure())
16399 {
16400 rettv->vval.v_number = -1;
16401 return;
16402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016403 serverid = get_tv_string_chk(&argvars[0]);
16404 if (serverid == NULL)
16405 {
16406 rettv->vval.v_number = -1;
16407 return; /* type error; errmsg already given */
16408 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016410 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411 if (n == 0)
16412 rettv->vval.v_number = -1;
16413 else
16414 {
16415 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16416 rettv->vval.v_number = (s != NULL);
16417 }
16418# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016419 if (check_connection() == FAIL)
16420 return;
16421
16422 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016423 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424# endif
16425
16426 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016428 char_u *retvar;
16429
Bram Moolenaar33570922005-01-25 22:26:29 +000016430 v.di_tv.v_type = VAR_STRING;
16431 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016432 retvar = get_tv_string_chk(&argvars[1]);
16433 if (retvar != NULL)
16434 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016435 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436 }
16437#else
16438 rettv->vval.v_number = -1;
16439#endif
16440}
16441
Bram Moolenaar0d660222005-01-07 21:51:51 +000016442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016443f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016444{
16445 char_u *r = NULL;
16446
16447#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016448 char_u *serverid = get_tv_string_chk(&argvars[0]);
16449
16450 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016451 {
16452# ifdef WIN32
16453 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016454 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016456 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457 if (n != 0)
16458 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16459 if (r == NULL)
16460# else
16461 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016462 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016463# endif
16464 EMSG(_("E277: Unable to read a server reply"));
16465 }
16466#endif
16467 rettv->v_type = VAR_STRING;
16468 rettv->vval.v_string = r;
16469}
16470
16471/*
16472 * "remote_send()" function
16473 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016475f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016476{
16477 rettv->v_type = VAR_STRING;
16478 rettv->vval.v_string = NULL;
16479#ifdef FEAT_CLIENTSERVER
16480 remote_common(argvars, rettv, FALSE);
16481#endif
16482}
16483
16484/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016485 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016486 */
16487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016488f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016489{
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 list_T *l;
16491 listitem_T *item, *item2;
16492 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016493 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016494 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016495 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016496 dict_T *d;
16497 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016498 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016499
Bram Moolenaar8c711452005-01-14 21:53:12 +000016500 if (argvars[0].v_type == VAR_DICT)
16501 {
16502 if (argvars[2].v_type != VAR_UNKNOWN)
16503 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016504 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016505 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016507 key = get_tv_string_chk(&argvars[1]);
16508 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016510 di = dict_find(d, key, -1);
16511 if (di == NULL)
16512 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016513 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16514 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016515 {
16516 *rettv = di->di_tv;
16517 init_tv(&di->di_tv);
16518 dictitem_remove(d, di);
16519 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016520 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016521 }
16522 }
16523 else if (argvars[0].v_type != VAR_LIST)
16524 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016525 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016526 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016528 int error = FALSE;
16529
16530 idx = get_tv_number_chk(&argvars[1], &error);
16531 if (error)
16532 ; /* type error: do nothing, errmsg already given */
16533 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016534 EMSGN(_(e_listidx), idx);
16535 else
16536 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016537 if (argvars[2].v_type == VAR_UNKNOWN)
16538 {
16539 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016540 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016541 *rettv = item->li_tv;
16542 vim_free(item);
16543 }
16544 else
16545 {
16546 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016547 end = get_tv_number_chk(&argvars[2], &error);
16548 if (error)
16549 ; /* type error: do nothing */
16550 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016551 EMSGN(_(e_listidx), end);
16552 else
16553 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016554 int cnt = 0;
16555
16556 for (li = item; li != NULL; li = li->li_next)
16557 {
16558 ++cnt;
16559 if (li == item2)
16560 break;
16561 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016562 if (li == NULL) /* didn't find "item2" after "item" */
16563 EMSG(_(e_invrange));
16564 else
16565 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016566 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016567 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016568 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016569 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016570 l->lv_first = item;
16571 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016572 item->li_prev = NULL;
16573 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016574 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016575 }
16576 }
16577 }
16578 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016579 }
16580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581}
16582
16583/*
16584 * "rename({from}, {to})" function
16585 */
16586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016587f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588{
16589 char_u buf[NUMBUFLEN];
16590
16591 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016592 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016594 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16595 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596}
16597
16598/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016599 * "repeat()" function
16600 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016602f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016603{
16604 char_u *p;
16605 int n;
16606 int slen;
16607 int len;
16608 char_u *r;
16609 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016610
16611 n = get_tv_number(&argvars[1]);
16612 if (argvars[0].v_type == VAR_LIST)
16613 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016614 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016615 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016616 if (list_extend(rettv->vval.v_list,
16617 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016618 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016619 }
16620 else
16621 {
16622 p = get_tv_string(&argvars[0]);
16623 rettv->v_type = VAR_STRING;
16624 rettv->vval.v_string = NULL;
16625
16626 slen = (int)STRLEN(p);
16627 len = slen * n;
16628 if (len <= 0)
16629 return;
16630
16631 r = alloc(len + 1);
16632 if (r != NULL)
16633 {
16634 for (i = 0; i < n; i++)
16635 mch_memmove(r + i * slen, p, (size_t)slen);
16636 r[len] = NUL;
16637 }
16638
16639 rettv->vval.v_string = r;
16640 }
16641}
16642
16643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 * "resolve()" function
16645 */
16646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016647f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648{
16649 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016650#ifdef HAVE_READLINK
16651 char_u *buf = NULL;
16652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016654 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655#ifdef FEAT_SHORTCUT
16656 {
16657 char_u *v = NULL;
16658
16659 v = mch_resolve_shortcut(p);
16660 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016661 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016663 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 }
16665#else
16666# ifdef HAVE_READLINK
16667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 char_u *cpy;
16669 int len;
16670 char_u *remain = NULL;
16671 char_u *q;
16672 int is_relative_to_current = FALSE;
16673 int has_trailing_pathsep = FALSE;
16674 int limit = 100;
16675
16676 p = vim_strsave(p);
16677
16678 if (p[0] == '.' && (vim_ispathsep(p[1])
16679 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16680 is_relative_to_current = TRUE;
16681
16682 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016683 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016686 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688
16689 q = getnextcomp(p);
16690 if (*q != NUL)
16691 {
16692 /* Separate the first path component in "p", and keep the
16693 * remainder (beginning with the path separator). */
16694 remain = vim_strsave(q - 1);
16695 q[-1] = NUL;
16696 }
16697
Bram Moolenaard9462e32011-04-11 21:35:11 +020016698 buf = alloc(MAXPATHL + 1);
16699 if (buf == NULL)
16700 goto fail;
16701
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 for (;;)
16703 {
16704 for (;;)
16705 {
16706 len = readlink((char *)p, (char *)buf, MAXPATHL);
16707 if (len <= 0)
16708 break;
16709 buf[len] = NUL;
16710
16711 if (limit-- == 0)
16712 {
16713 vim_free(p);
16714 vim_free(remain);
16715 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 goto fail;
16718 }
16719
16720 /* Ensure that the result will have a trailing path separator
16721 * if the argument has one. */
16722 if (remain == NULL && has_trailing_pathsep)
16723 add_pathsep(buf);
16724
16725 /* Separate the first path component in the link value and
16726 * concatenate the remainders. */
16727 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16728 if (*q != NUL)
16729 {
16730 if (remain == NULL)
16731 remain = vim_strsave(q - 1);
16732 else
16733 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016734 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735 if (cpy != NULL)
16736 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 vim_free(remain);
16738 remain = cpy;
16739 }
16740 }
16741 q[-1] = NUL;
16742 }
16743
16744 q = gettail(p);
16745 if (q > p && *q == NUL)
16746 {
16747 /* Ignore trailing path separator. */
16748 q[-1] = NUL;
16749 q = gettail(p);
16750 }
16751 if (q > p && !mch_isFullName(buf))
16752 {
16753 /* symlink is relative to directory of argument */
16754 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16755 if (cpy != NULL)
16756 {
16757 STRCPY(cpy, p);
16758 STRCPY(gettail(cpy), buf);
16759 vim_free(p);
16760 p = cpy;
16761 }
16762 }
16763 else
16764 {
16765 vim_free(p);
16766 p = vim_strsave(buf);
16767 }
16768 }
16769
16770 if (remain == NULL)
16771 break;
16772
16773 /* Append the first path component of "remain" to "p". */
16774 q = getnextcomp(remain + 1);
16775 len = q - remain - (*q != NUL);
16776 cpy = vim_strnsave(p, STRLEN(p) + len);
16777 if (cpy != NULL)
16778 {
16779 STRNCAT(cpy, remain, len);
16780 vim_free(p);
16781 p = cpy;
16782 }
16783 /* Shorten "remain". */
16784 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016785 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 else
16787 {
16788 vim_free(remain);
16789 remain = NULL;
16790 }
16791 }
16792
16793 /* If the result is a relative path name, make it explicitly relative to
16794 * the current directory if and only if the argument had this form. */
16795 if (!vim_ispathsep(*p))
16796 {
16797 if (is_relative_to_current
16798 && *p != NUL
16799 && !(p[0] == '.'
16800 && (p[1] == NUL
16801 || vim_ispathsep(p[1])
16802 || (p[1] == '.'
16803 && (p[2] == NUL
16804 || vim_ispathsep(p[2]))))))
16805 {
16806 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016807 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 if (cpy != NULL)
16809 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 vim_free(p);
16811 p = cpy;
16812 }
16813 }
16814 else if (!is_relative_to_current)
16815 {
16816 /* Strip leading "./". */
16817 q = p;
16818 while (q[0] == '.' && vim_ispathsep(q[1]))
16819 q += 2;
16820 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016821 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 }
16823 }
16824
16825 /* Ensure that the result will have no trailing path separator
16826 * if the argument had none. But keep "/" or "//". */
16827 if (!has_trailing_pathsep)
16828 {
16829 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016830 if (after_pathsep(p, q))
16831 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 }
16833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016834 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 }
16836# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016837 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838# endif
16839#endif
16840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016841 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842
16843#ifdef HAVE_READLINK
16844fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016845 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016847 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848}
16849
16850/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016851 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 */
16853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016854f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855{
Bram Moolenaar33570922005-01-25 22:26:29 +000016856 list_T *l;
16857 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859 if (argvars[0].v_type != VAR_LIST)
16860 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016861 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016862 && !tv_check_lock(l->lv_lock,
16863 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016864 {
16865 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016866 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016867 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016868 while (li != NULL)
16869 {
16870 ni = li->li_prev;
16871 list_append(l, li);
16872 li = ni;
16873 }
16874 rettv->vval.v_list = l;
16875 rettv->v_type = VAR_LIST;
16876 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016877 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879}
16880
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016881#define SP_NOMOVE 0x01 /* don't move cursor */
16882#define SP_REPEAT 0x02 /* repeat to find outer pair */
16883#define SP_RETCOUNT 0x04 /* return matchcount */
16884#define SP_SETPCMARK 0x08 /* set previous context mark */
16885#define SP_START 0x10 /* accept match at start position */
16886#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16887#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016888#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016889
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016890static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891
16892/*
16893 * Get flags for a search function.
16894 * Possibly sets "p_ws".
16895 * Returns BACKWARD, FORWARD or zero (for an error).
16896 */
16897 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016898get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899{
16900 int dir = FORWARD;
16901 char_u *flags;
16902 char_u nbuf[NUMBUFLEN];
16903 int mask;
16904
16905 if (varp->v_type != VAR_UNKNOWN)
16906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016907 flags = get_tv_string_buf_chk(varp, nbuf);
16908 if (flags == NULL)
16909 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016910 while (*flags != NUL)
16911 {
16912 switch (*flags)
16913 {
16914 case 'b': dir = BACKWARD; break;
16915 case 'w': p_ws = TRUE; break;
16916 case 'W': p_ws = FALSE; break;
16917 default: mask = 0;
16918 if (flagsp != NULL)
16919 switch (*flags)
16920 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016921 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016922 case 'e': mask = SP_END; break;
16923 case 'm': mask = SP_RETCOUNT; break;
16924 case 'n': mask = SP_NOMOVE; break;
16925 case 'p': mask = SP_SUBPAT; break;
16926 case 'r': mask = SP_REPEAT; break;
16927 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016928 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929 }
16930 if (mask == 0)
16931 {
16932 EMSG2(_(e_invarg2), flags);
16933 dir = 0;
16934 }
16935 else
16936 *flagsp |= mask;
16937 }
16938 if (dir == 0)
16939 break;
16940 ++flags;
16941 }
16942 }
16943 return dir;
16944}
16945
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016947 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016950search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016952 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 char_u *pat;
16954 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016955 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 int save_p_ws = p_ws;
16957 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016958 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016959 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016960 proftime_T tm;
16961#ifdef FEAT_RELTIME
16962 long time_limit = 0;
16963#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016964 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016965 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016967 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016968 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016969 if (dir == 0)
16970 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016971 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016972 if (flags & SP_START)
16973 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016974 if (flags & SP_END)
16975 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016976 if (flags & SP_COLUMN)
16977 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016978
Bram Moolenaar76929292008-01-06 19:07:36 +000016979 /* Optional arguments: line number to stop searching and timeout. */
16980 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016981 {
16982 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16983 if (lnum_stop < 0)
16984 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016985#ifdef FEAT_RELTIME
16986 if (argvars[3].v_type != VAR_UNKNOWN)
16987 {
16988 time_limit = get_tv_number_chk(&argvars[3], NULL);
16989 if (time_limit < 0)
16990 goto theend;
16991 }
16992#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016993 }
16994
Bram Moolenaar76929292008-01-06 19:07:36 +000016995#ifdef FEAT_RELTIME
16996 /* Set the time limit, if there is one. */
16997 profile_setlimit(time_limit, &tm);
16998#endif
16999
Bram Moolenaar231334e2005-07-25 20:46:57 +000017000 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017001 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017002 * Check to make sure only those flags are set.
17003 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17004 * flags cannot be set. Check for that condition also.
17005 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017006 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017007 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017009 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017010 goto theend;
17011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017013 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017014 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017015 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017016 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017018 if (flags & SP_SUBPAT)
17019 retval = subpatnum;
17020 else
17021 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017022 if (flags & SP_SETPCMARK)
17023 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017025 if (match_pos != NULL)
17026 {
17027 /* Store the match cursor position */
17028 match_pos->lnum = pos.lnum;
17029 match_pos->col = pos.col + 1;
17030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 /* "/$" will put the cursor after the end of the line, may need to
17032 * correct that here */
17033 check_cursor();
17034 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017035
17036 /* If 'n' flag is used: restore cursor position. */
17037 if (flags & SP_NOMOVE)
17038 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017039 else
17040 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017041theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017043
17044 return retval;
17045}
17046
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017047#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017048
17049/*
17050 * round() is not in C90, use ceil() or floor() instead.
17051 */
17052 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017053vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017054{
17055 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17056}
17057
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017058/*
17059 * "round({float})" function
17060 */
17061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017062f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017063{
17064 float_T f;
17065
17066 rettv->v_type = VAR_FLOAT;
17067 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017068 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017069 else
17070 rettv->vval.v_float = 0.0;
17071}
17072#endif
17073
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017074/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017075 * "screenattr()" function
17076 */
17077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017078f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017079{
17080 int row;
17081 int col;
17082 int c;
17083
17084 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17085 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17086 if (row < 0 || row >= screen_Rows
17087 || col < 0 || col >= screen_Columns)
17088 c = -1;
17089 else
17090 c = ScreenAttrs[LineOffset[row] + col];
17091 rettv->vval.v_number = c;
17092}
17093
17094/*
17095 * "screenchar()" function
17096 */
17097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017098f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017099{
17100 int row;
17101 int col;
17102 int off;
17103 int c;
17104
17105 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17106 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17107 if (row < 0 || row >= screen_Rows
17108 || col < 0 || col >= screen_Columns)
17109 c = -1;
17110 else
17111 {
17112 off = LineOffset[row] + col;
17113#ifdef FEAT_MBYTE
17114 if (enc_utf8 && ScreenLinesUC[off] != 0)
17115 c = ScreenLinesUC[off];
17116 else
17117#endif
17118 c = ScreenLines[off];
17119 }
17120 rettv->vval.v_number = c;
17121}
17122
17123/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017124 * "screencol()" function
17125 *
17126 * First column is 1 to be consistent with virtcol().
17127 */
17128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017129f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017130{
17131 rettv->vval.v_number = screen_screencol() + 1;
17132}
17133
17134/*
17135 * "screenrow()" function
17136 */
17137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017138f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017139{
17140 rettv->vval.v_number = screen_screenrow() + 1;
17141}
17142
17143/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017144 * "search()" function
17145 */
17146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017147f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017148{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017149 int flags = 0;
17150
17151 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152}
17153
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017155 * "searchdecl()" function
17156 */
17157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017158f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017159{
17160 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017161 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017162 int error = FALSE;
17163 char_u *name;
17164
17165 rettv->vval.v_number = 1; /* default: FAIL */
17166
17167 name = get_tv_string_chk(&argvars[0]);
17168 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017169 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017170 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017171 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17172 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17173 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017174 if (!error && name != NULL)
17175 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017176 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017177}
17178
17179/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017180 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017183searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184{
17185 char_u *spat, *mpat, *epat;
17186 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 int dir;
17189 int flags = 0;
17190 char_u nbuf1[NUMBUFLEN];
17191 char_u nbuf2[NUMBUFLEN];
17192 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017193 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017194 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017195 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017198 spat = get_tv_string_chk(&argvars[0]);
17199 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17200 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17201 if (spat == NULL || mpat == NULL || epat == NULL)
17202 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 /* Handle the optional fourth argument: flags */
17205 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017206 if (dir == 0)
17207 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017208
17209 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017210 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17211 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017212 if ((flags & (SP_END | SP_SUBPAT)) != 0
17213 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017214 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017215 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017216 goto theend;
17217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017219 /* Using 'r' implies 'W', otherwise it doesn't work. */
17220 if (flags & SP_REPEAT)
17221 p_ws = FALSE;
17222
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017223 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017224 if (argvars[3].v_type == VAR_UNKNOWN
17225 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 skip = (char_u *)"";
17227 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017229 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017230 if (argvars[5].v_type != VAR_UNKNOWN)
17231 {
17232 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17233 if (lnum_stop < 0)
17234 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017235#ifdef FEAT_RELTIME
17236 if (argvars[6].v_type != VAR_UNKNOWN)
17237 {
17238 time_limit = get_tv_number_chk(&argvars[6], NULL);
17239 if (time_limit < 0)
17240 goto theend;
17241 }
17242#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017243 }
17244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017245 if (skip == NULL)
17246 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017248 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017249 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017250
17251theend:
17252 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017253
17254 return retval;
17255}
17256
17257/*
17258 * "searchpair()" function
17259 */
17260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017261f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017262{
17263 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17264}
17265
17266/*
17267 * "searchpairpos()" function
17268 */
17269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017270f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017271{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017272 pos_T match_pos;
17273 int lnum = 0;
17274 int col = 0;
17275
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017276 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017277 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017278
17279 if (searchpair_cmn(argvars, &match_pos) > 0)
17280 {
17281 lnum = match_pos.lnum;
17282 col = match_pos.col;
17283 }
17284
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017285 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17286 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017287}
17288
17289/*
17290 * Search for a start/middle/end thing.
17291 * Used by searchpair(), see its documentation for the details.
17292 * Returns 0 or -1 for no match,
17293 */
17294 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017295do_searchpair(
17296 char_u *spat, /* start pattern */
17297 char_u *mpat, /* middle pattern */
17298 char_u *epat, /* end pattern */
17299 int dir, /* BACKWARD or FORWARD */
17300 char_u *skip, /* skip expression */
17301 int flags, /* SP_SETPCMARK and other SP_ values */
17302 pos_T *match_pos,
17303 linenr_T lnum_stop, /* stop at this line if not zero */
17304 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017305{
17306 char_u *save_cpo;
17307 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17308 long retval = 0;
17309 pos_T pos;
17310 pos_T firstpos;
17311 pos_T foundpos;
17312 pos_T save_cursor;
17313 pos_T save_pos;
17314 int n;
17315 int r;
17316 int nest = 1;
17317 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017318 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017319 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017320
17321 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17322 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017323 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017324
Bram Moolenaar76929292008-01-06 19:07:36 +000017325#ifdef FEAT_RELTIME
17326 /* Set the time limit, if there is one. */
17327 profile_setlimit(time_limit, &tm);
17328#endif
17329
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017330 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17331 * start/middle/end (pat3, for the top pair). */
17332 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17333 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17334 if (pat2 == NULL || pat3 == NULL)
17335 goto theend;
17336 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17337 if (*mpat == NUL)
17338 STRCPY(pat3, pat2);
17339 else
17340 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17341 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017342 if (flags & SP_START)
17343 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017344
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 save_cursor = curwin->w_cursor;
17346 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017347 clearpos(&firstpos);
17348 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 pat = pat3;
17350 for (;;)
17351 {
17352 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017353 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17355 /* didn't find it or found the first match again: FAIL */
17356 break;
17357
17358 if (firstpos.lnum == 0)
17359 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017360 if (equalpos(pos, foundpos))
17361 {
17362 /* Found the same position again. Can happen with a pattern that
17363 * has "\zs" at the end and searching backwards. Advance one
17364 * character and try again. */
17365 if (dir == BACKWARD)
17366 decl(&pos);
17367 else
17368 incl(&pos);
17369 }
17370 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017372 /* clear the start flag to avoid getting stuck here */
17373 options &= ~SEARCH_START;
17374
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 /* If the skip pattern matches, ignore this match. */
17376 if (*skip != NUL)
17377 {
17378 save_pos = curwin->w_cursor;
17379 curwin->w_cursor = pos;
17380 r = eval_to_bool(skip, &err, NULL, FALSE);
17381 curwin->w_cursor = save_pos;
17382 if (err)
17383 {
17384 /* Evaluating {skip} caused an error, break here. */
17385 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017386 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 break;
17388 }
17389 if (r)
17390 continue;
17391 }
17392
17393 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17394 {
17395 /* Found end when searching backwards or start when searching
17396 * forward: nested pair. */
17397 ++nest;
17398 pat = pat2; /* nested, don't search for middle */
17399 }
17400 else
17401 {
17402 /* Found end when searching forward or start when searching
17403 * backward: end of (nested) pair; or found middle in outer pair. */
17404 if (--nest == 1)
17405 pat = pat3; /* outer level, search for middle */
17406 }
17407
17408 if (nest == 0)
17409 {
17410 /* Found the match: return matchcount or line number. */
17411 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017412 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017414 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017415 if (flags & SP_SETPCMARK)
17416 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 curwin->w_cursor = pos;
17418 if (!(flags & SP_REPEAT))
17419 break;
17420 nest = 1; /* search for next unmatched */
17421 }
17422 }
17423
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017424 if (match_pos != NULL)
17425 {
17426 /* Store the match cursor position */
17427 match_pos->lnum = curwin->w_cursor.lnum;
17428 match_pos->col = curwin->w_cursor.col + 1;
17429 }
17430
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017432 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433 curwin->w_cursor = save_cursor;
17434
17435theend:
17436 vim_free(pat2);
17437 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017438 if (p_cpo == empty_option)
17439 p_cpo = save_cpo;
17440 else
17441 /* Darn, evaluating the {skip} expression changed the value. */
17442 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017443
17444 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445}
17446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017447/*
17448 * "searchpos()" function
17449 */
17450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017451f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017452{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017453 pos_T match_pos;
17454 int lnum = 0;
17455 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017456 int n;
17457 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017458
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017459 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017460 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017461
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017462 n = search_cmn(argvars, &match_pos, &flags);
17463 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017464 {
17465 lnum = match_pos.lnum;
17466 col = match_pos.col;
17467 }
17468
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017469 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17470 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017471 if (flags & SP_SUBPAT)
17472 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017473}
17474
Bram Moolenaar0d660222005-01-07 21:51:51 +000017475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017476f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017478#ifdef FEAT_CLIENTSERVER
17479 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017480 char_u *server = get_tv_string_chk(&argvars[0]);
17481 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482
Bram Moolenaar0d660222005-01-07 21:51:51 +000017483 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017484 if (server == NULL || reply == NULL)
17485 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017486 if (check_restricted() || check_secure())
17487 return;
17488# ifdef FEAT_X11
17489 if (check_connection() == FAIL)
17490 return;
17491# endif
17492
17493 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017495 EMSG(_("E258: Unable to send to client"));
17496 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017498 rettv->vval.v_number = 0;
17499#else
17500 rettv->vval.v_number = -1;
17501#endif
17502}
17503
Bram Moolenaar0d660222005-01-07 21:51:51 +000017504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017505f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017506{
17507 char_u *r = NULL;
17508
17509#ifdef FEAT_CLIENTSERVER
17510# ifdef WIN32
17511 r = serverGetVimNames();
17512# else
17513 make_connection();
17514 if (X_DISPLAY != NULL)
17515 r = serverGetVimNames(X_DISPLAY);
17516# endif
17517#endif
17518 rettv->v_type = VAR_STRING;
17519 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520}
17521
17522/*
17523 * "setbufvar()" function
17524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017526f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527{
17528 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 char_u nbuf[NUMBUFLEN];
17533
17534 if (check_restricted() || check_secure())
17535 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017536 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17537 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017538 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 varp = &argvars[2];
17540
17541 if (buf != NULL && varname != NULL && varp != NULL)
17542 {
17543 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545
17546 if (*varname == '&')
17547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017548 long numval;
17549 char_u *strval;
17550 int error = FALSE;
17551
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017553 numval = get_tv_number_chk(varp, &error);
17554 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017555 if (!error && strval != NULL)
17556 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 }
17558 else
17559 {
17560 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17561 if (bufvarname != NULL)
17562 {
17563 STRCPY(bufvarname, "b:");
17564 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017565 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 vim_free(bufvarname);
17567 }
17568 }
17569
17570 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573}
17574
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017576f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017577{
17578 dict_T *d;
17579 dictitem_T *di;
17580 char_u *csearch;
17581
17582 if (argvars[0].v_type != VAR_DICT)
17583 {
17584 EMSG(_(e_dictreq));
17585 return;
17586 }
17587
17588 if ((d = argvars[0].vval.v_dict) != NULL)
17589 {
17590 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17591 if (csearch != NULL)
17592 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017593#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017594 if (enc_utf8)
17595 {
17596 int pcc[MAX_MCO];
17597 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017598
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017599 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17600 }
17601 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017602#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017603 set_last_csearch(PTR2CHAR(csearch),
17604 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017605 }
17606
17607 di = dict_find(d, (char_u *)"forward", -1);
17608 if (di != NULL)
17609 set_csearch_direction(get_tv_number(&di->di_tv)
17610 ? FORWARD : BACKWARD);
17611
17612 di = dict_find(d, (char_u *)"until", -1);
17613 if (di != NULL)
17614 set_csearch_until(!!get_tv_number(&di->di_tv));
17615 }
17616}
17617
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618/*
17619 * "setcmdpos()" function
17620 */
17621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017622f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017624 int pos = (int)get_tv_number(&argvars[0]) - 1;
17625
17626 if (pos >= 0)
17627 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628}
17629
17630/*
17631 * "setline()" function
17632 */
17633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017634f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635{
17636 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017637 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017638 list_T *l = NULL;
17639 listitem_T *li = NULL;
17640 long added = 0;
17641 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017643 lnum = get_tv_lnum(&argvars[0]);
17644 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017646 l = argvars[1].vval.v_list;
17647 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017649 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017650 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017651
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017652 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017653 for (;;)
17654 {
17655 if (l != NULL)
17656 {
17657 /* list argument, get next string */
17658 if (li == NULL)
17659 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017660 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017661 li = li->li_next;
17662 }
17663
17664 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017665 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017666 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017667
17668 /* When coming here from Insert mode, sync undo, so that this can be
17669 * undone separately from what was previously inserted. */
17670 if (u_sync_once == 2)
17671 {
17672 u_sync_once = 1; /* notify that u_sync() was called */
17673 u_sync(TRUE);
17674 }
17675
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017676 if (lnum <= curbuf->b_ml.ml_line_count)
17677 {
17678 /* existing line, replace it */
17679 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17680 {
17681 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017682 if (lnum == curwin->w_cursor.lnum)
17683 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017684 rettv->vval.v_number = 0; /* OK */
17685 }
17686 }
17687 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17688 {
17689 /* lnum is one past the last line, append the line */
17690 ++added;
17691 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17692 rettv->vval.v_number = 0; /* OK */
17693 }
17694
17695 if (l == NULL) /* only one string argument */
17696 break;
17697 ++lnum;
17698 }
17699
17700 if (added > 0)
17701 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702}
17703
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017704static 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 +000017705
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017707 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017708 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017710set_qf_ll_list(
17711 win_T *wp UNUSED,
17712 typval_T *list_arg UNUSED,
17713 typval_T *action_arg UNUSED,
17714 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017715{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017716#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017717 char_u *act;
17718 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017719#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017720
Bram Moolenaar2641f772005-03-25 21:58:17 +000017721 rettv->vval.v_number = -1;
17722
17723#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017724 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017725 EMSG(_(e_listreq));
17726 else
17727 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017728 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017729
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017730 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017731 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017732 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017733 if (act == NULL)
17734 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017735 if (*act == 'a' || *act == 'r')
17736 action = *act;
17737 }
17738
Bram Moolenaar81484f42012-12-05 15:16:47 +010017739 if (l != NULL && set_errorlist(wp, l, action,
17740 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017741 rettv->vval.v_number = 0;
17742 }
17743#endif
17744}
17745
17746/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017747 * "setloclist()" function
17748 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017750f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017751{
17752 win_T *win;
17753
17754 rettv->vval.v_number = -1;
17755
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017756 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017757 if (win != NULL)
17758 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17759}
17760
17761/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017762 * "setmatches()" function
17763 */
17764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017765f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017766{
17767#ifdef FEAT_SEARCH_EXTRA
17768 list_T *l;
17769 listitem_T *li;
17770 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017771 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017772
17773 rettv->vval.v_number = -1;
17774 if (argvars[0].v_type != VAR_LIST)
17775 {
17776 EMSG(_(e_listreq));
17777 return;
17778 }
17779 if ((l = argvars[0].vval.v_list) != NULL)
17780 {
17781
17782 /* To some extent make sure that we are dealing with a list from
17783 * "getmatches()". */
17784 li = l->lv_first;
17785 while (li != NULL)
17786 {
17787 if (li->li_tv.v_type != VAR_DICT
17788 || (d = li->li_tv.vval.v_dict) == NULL)
17789 {
17790 EMSG(_(e_invarg));
17791 return;
17792 }
17793 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017794 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17795 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017796 && dict_find(d, (char_u *)"priority", -1) != NULL
17797 && dict_find(d, (char_u *)"id", -1) != NULL))
17798 {
17799 EMSG(_(e_invarg));
17800 return;
17801 }
17802 li = li->li_next;
17803 }
17804
17805 clear_matches(curwin);
17806 li = l->lv_first;
17807 while (li != NULL)
17808 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017809 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017810 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017811 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017812 char_u *group;
17813 int priority;
17814 int id;
17815 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017816
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017817 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017818 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17819 {
17820 if (s == NULL)
17821 {
17822 s = list_alloc();
17823 if (s == NULL)
17824 return;
17825 }
17826
17827 /* match from matchaddpos() */
17828 for (i = 1; i < 9; i++)
17829 {
17830 sprintf((char *)buf, (char *)"pos%d", i);
17831 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17832 {
17833 if (di->di_tv.v_type != VAR_LIST)
17834 return;
17835
17836 list_append_tv(s, &di->di_tv);
17837 s->lv_refcount++;
17838 }
17839 else
17840 break;
17841 }
17842 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017843
17844 group = get_dict_string(d, (char_u *)"group", FALSE);
17845 priority = (int)get_dict_number(d, (char_u *)"priority");
17846 id = (int)get_dict_number(d, (char_u *)"id");
17847 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17848 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17849 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017850 if (i == 0)
17851 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017852 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017853 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017854 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017855 }
17856 else
17857 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017858 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017859 list_unref(s);
17860 s = NULL;
17861 }
17862
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017863 li = li->li_next;
17864 }
17865 rettv->vval.v_number = 0;
17866 }
17867#endif
17868}
17869
17870/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017871 * "setpos()" function
17872 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017874f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017875{
17876 pos_T pos;
17877 int fnum;
17878 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017879 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017880
Bram Moolenaar08250432008-02-13 11:42:46 +000017881 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017882 name = get_tv_string_chk(argvars);
17883 if (name != NULL)
17884 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017885 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017886 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017887 if (--pos.col < 0)
17888 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017889 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017890 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017891 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017892 if (fnum == curbuf->b_fnum)
17893 {
17894 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017895 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017896 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017897 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017898 curwin->w_set_curswant = FALSE;
17899 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017900 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017901 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017902 }
17903 else
17904 EMSG(_(e_invarg));
17905 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017906 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17907 {
17908 /* set mark */
17909 if (setmark_pos(name[1], &pos, fnum) == OK)
17910 rettv->vval.v_number = 0;
17911 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017912 else
17913 EMSG(_(e_invarg));
17914 }
17915 }
17916}
17917
17918/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017919 * "setqflist()" function
17920 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017922f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017923{
17924 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17925}
17926
17927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 * "setreg()" function
17929 */
17930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017931f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932{
17933 int regname;
17934 char_u *strregname;
17935 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017936 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 int append;
17938 char_u yank_type;
17939 long block_len;
17940
17941 block_len = -1;
17942 yank_type = MAUTO;
17943 append = FALSE;
17944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017945 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017946 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017948 if (strregname == NULL)
17949 return; /* type error; errmsg already given */
17950 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 if (regname == 0 || regname == '@')
17952 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017954 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017956 stropt = get_tv_string_chk(&argvars[2]);
17957 if (stropt == NULL)
17958 return; /* type error */
17959 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 switch (*stropt)
17961 {
17962 case 'a': case 'A': /* append */
17963 append = TRUE;
17964 break;
17965 case 'v': case 'c': /* character-wise selection */
17966 yank_type = MCHAR;
17967 break;
17968 case 'V': case 'l': /* line-wise selection */
17969 yank_type = MLINE;
17970 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 case 'b': case Ctrl_V: /* block-wise selection */
17972 yank_type = MBLOCK;
17973 if (VIM_ISDIGIT(stropt[1]))
17974 {
17975 ++stropt;
17976 block_len = getdigits(&stropt) - 1;
17977 --stropt;
17978 }
17979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 }
17981 }
17982
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017983 if (argvars[1].v_type == VAR_LIST)
17984 {
17985 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017986 char_u **allocval;
17987 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017988 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017989 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017990 int len = argvars[1].vval.v_list->lv_len;
17991 listitem_T *li;
17992
Bram Moolenaar7d647822014-04-05 21:28:56 +020017993 /* First half: use for pointers to result lines; second half: use for
17994 * pointers to allocated copies. */
17995 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017996 if (lstval == NULL)
17997 return;
17998 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017999 allocval = lstval + len + 2;
18000 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018001
18002 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18003 li = li->li_next)
18004 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018005 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018006 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018007 goto free_lstval;
18008 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018009 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018010 /* Need to make a copy, next get_tv_string_buf_chk() will
18011 * overwrite the string. */
18012 strval = vim_strsave(buf);
18013 if (strval == NULL)
18014 goto free_lstval;
18015 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018016 }
18017 *curval++ = strval;
18018 }
18019 *curval++ = NULL;
18020
18021 write_reg_contents_lst(regname, lstval, -1,
18022 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018023free_lstval:
18024 while (curallocval > allocval)
18025 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018026 vim_free(lstval);
18027 }
18028 else
18029 {
18030 strval = get_tv_string_chk(&argvars[1]);
18031 if (strval == NULL)
18032 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018033 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018036 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037}
18038
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018039/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018040 * "settabvar()" function
18041 */
18042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018043f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018044{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018045#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018046 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018047 tabpage_T *tp;
18048#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018049 char_u *varname, *tabvarname;
18050 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018051
18052 rettv->vval.v_number = 0;
18053
18054 if (check_restricted() || check_secure())
18055 return;
18056
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018057#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018058 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018059#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018060 varname = get_tv_string_chk(&argvars[1]);
18061 varp = &argvars[2];
18062
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018063 if (varname != NULL && varp != NULL
18064#ifdef FEAT_WINDOWS
18065 && tp != NULL
18066#endif
18067 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018068 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018069#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018070 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018071 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018072#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018073
18074 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18075 if (tabvarname != NULL)
18076 {
18077 STRCPY(tabvarname, "t:");
18078 STRCPY(tabvarname + 2, varname);
18079 set_var(tabvarname, varp, TRUE);
18080 vim_free(tabvarname);
18081 }
18082
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018083#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018084 /* Restore current tabpage */
18085 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018086 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018087#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018088 }
18089}
18090
18091/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018092 * "settabwinvar()" function
18093 */
18094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018095f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018096{
18097 setwinvar(argvars, rettv, 1);
18098}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099
18100/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018101 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018104f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018106 setwinvar(argvars, rettv, 0);
18107}
18108
18109/*
18110 * "setwinvar()" and "settabwinvar()" functions
18111 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018112
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018114setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018115{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 win_T *win;
18117#ifdef FEAT_WINDOWS
18118 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018119 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018120 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121#endif
18122 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018123 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018125 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126
18127 if (check_restricted() || check_secure())
18128 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018129
18130#ifdef FEAT_WINDOWS
18131 if (off == 1)
18132 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18133 else
18134 tp = curtab;
18135#endif
18136 win = find_win_by_nr(&argvars[off], tp);
18137 varname = get_tv_string_chk(&argvars[off + 1]);
18138 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139
18140 if (win != NULL && varname != NULL && varp != NULL)
18141 {
18142#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018143 need_switch_win = !(tp == curtab && win == curwin);
18144 if (!need_switch_win
18145 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018148 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018150 long numval;
18151 char_u *strval;
18152 int error = FALSE;
18153
18154 ++varname;
18155 numval = get_tv_number_chk(varp, &error);
18156 strval = get_tv_string_buf_chk(varp, nbuf);
18157 if (!error && strval != NULL)
18158 set_option_value(varname, numval, strval, OPT_LOCAL);
18159 }
18160 else
18161 {
18162 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18163 if (winvarname != NULL)
18164 {
18165 STRCPY(winvarname, "w:");
18166 STRCPY(winvarname + 2, varname);
18167 set_var(winvarname, varp, TRUE);
18168 vim_free(winvarname);
18169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 }
18171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018173 if (need_switch_win)
18174 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175#endif
18176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177}
18178
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018179#ifdef FEAT_CRYPT
18180/*
18181 * "sha256({string})" function
18182 */
18183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018184f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018185{
18186 char_u *p;
18187
18188 p = get_tv_string(&argvars[0]);
18189 rettv->vval.v_string = vim_strsave(
18190 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18191 rettv->v_type = VAR_STRING;
18192}
18193#endif /* FEAT_CRYPT */
18194
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018196 * "shellescape({string})" function
18197 */
18198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018199f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018200{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018201 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018202 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018203 rettv->v_type = VAR_STRING;
18204}
18205
18206/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018207 * shiftwidth() function
18208 */
18209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018210f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018211{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018212 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018213}
18214
18215/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018216 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 */
18218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018219f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222
Bram Moolenaar0d660222005-01-07 21:51:51 +000018223 p = get_tv_string(&argvars[0]);
18224 rettv->vval.v_string = vim_strsave(p);
18225 simplify_filename(rettv->vval.v_string); /* simplify in place */
18226 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227}
18228
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018229#ifdef FEAT_FLOAT
18230/*
18231 * "sin()" function
18232 */
18233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018234f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018235{
18236 float_T f;
18237
18238 rettv->v_type = VAR_FLOAT;
18239 if (get_float_arg(argvars, &f) == OK)
18240 rettv->vval.v_float = sin(f);
18241 else
18242 rettv->vval.v_float = 0.0;
18243}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018244
18245/*
18246 * "sinh()" function
18247 */
18248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018249f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018250{
18251 float_T f;
18252
18253 rettv->v_type = VAR_FLOAT;
18254 if (get_float_arg(argvars, &f) == OK)
18255 rettv->vval.v_float = sinh(f);
18256 else
18257 rettv->vval.v_float = 0.0;
18258}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018259#endif
18260
Bram Moolenaar0d660222005-01-07 21:51:51 +000018261static int
18262#ifdef __BORLANDC__
18263 _RTLENTRYF
18264#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018265 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018266static int
18267#ifdef __BORLANDC__
18268 _RTLENTRYF
18269#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018270 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018271
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018272/* struct used in the array that's given to qsort() */
18273typedef struct
18274{
18275 listitem_T *item;
18276 int idx;
18277} sortItem_T;
18278
Bram Moolenaar0d660222005-01-07 21:51:51 +000018279static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018280static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018281static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018282#ifdef FEAT_FLOAT
18283static int item_compare_float;
18284#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018285static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018286static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018287static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018288static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018289static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018290#define ITEM_COMPARE_FAIL 999
18291
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018293 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018295 static int
18296#ifdef __BORLANDC__
18297_RTLENTRYF
18298#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018299item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018301 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018302 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018303 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018304 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018305 int res;
18306 char_u numbuf1[NUMBUFLEN];
18307 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018309 si1 = (sortItem_T *)s1;
18310 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018311 tv1 = &si1->item->li_tv;
18312 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018313
18314 if (item_compare_numbers)
18315 {
18316 long v1 = get_tv_number(tv1);
18317 long v2 = get_tv_number(tv2);
18318
18319 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18320 }
18321
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018322#ifdef FEAT_FLOAT
18323 if (item_compare_float)
18324 {
18325 float_T v1 = get_tv_float(tv1);
18326 float_T v2 = get_tv_float(tv2);
18327
18328 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18329 }
18330#endif
18331
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018332 /* tv2string() puts quotes around a string and allocates memory. Don't do
18333 * that for string variables. Use a single quote when comparing with a
18334 * non-string to do what the docs promise. */
18335 if (tv1->v_type == VAR_STRING)
18336 {
18337 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18338 p1 = (char_u *)"'";
18339 else
18340 p1 = tv1->vval.v_string;
18341 }
18342 else
18343 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18344 if (tv2->v_type == VAR_STRING)
18345 {
18346 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18347 p2 = (char_u *)"'";
18348 else
18349 p2 = tv2->vval.v_string;
18350 }
18351 else
18352 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018353 if (p1 == NULL)
18354 p1 = (char_u *)"";
18355 if (p2 == NULL)
18356 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018357 if (!item_compare_numeric)
18358 {
18359 if (item_compare_ic)
18360 res = STRICMP(p1, p2);
18361 else
18362 res = STRCMP(p1, p2);
18363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018365 {
18366 double n1, n2;
18367 n1 = strtod((char *)p1, (char **)&p1);
18368 n2 = strtod((char *)p2, (char **)&p2);
18369 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18370 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018371
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018372 /* When the result would be zero, compare the item indexes. Makes the
18373 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018374 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018375 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018376
Bram Moolenaar0d660222005-01-07 21:51:51 +000018377 vim_free(tofree1);
18378 vim_free(tofree2);
18379 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380}
18381
18382 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018383#ifdef __BORLANDC__
18384_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018386item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018387{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018388 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018389 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018390 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018391 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018392 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018394 /* shortcut after failure in previous call; compare all items equal */
18395 if (item_compare_func_err)
18396 return 0;
18397
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018398 si1 = (sortItem_T *)s1;
18399 si2 = (sortItem_T *)s2;
18400
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018401 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018402 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018403 copy_tv(&si1->item->li_tv, &argv[0]);
18404 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018405
18406 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018407 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018408 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18409 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018410 clear_tv(&argv[0]);
18411 clear_tv(&argv[1]);
18412
18413 if (res == FAIL)
18414 res = ITEM_COMPARE_FAIL;
18415 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018416 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18417 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018418 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018419 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018420
18421 /* When the result would be zero, compare the pointers themselves. Makes
18422 * the sort stable. */
18423 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018424 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018425
Bram Moolenaar0d660222005-01-07 21:51:51 +000018426 return res;
18427}
18428
18429/*
18430 * "sort({list})" function
18431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018433do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434{
Bram Moolenaar33570922005-01-25 22:26:29 +000018435 list_T *l;
18436 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018437 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018438 long len;
18439 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440
Bram Moolenaar0d660222005-01-07 21:51:51 +000018441 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018442 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443 else
18444 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018445 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018446 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018447 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18448 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018449 return;
18450 rettv->vval.v_list = l;
18451 rettv->v_type = VAR_LIST;
18452 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453
Bram Moolenaar0d660222005-01-07 21:51:51 +000018454 len = list_len(l);
18455 if (len <= 1)
18456 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457
Bram Moolenaar0d660222005-01-07 21:51:51 +000018458 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018459 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018460 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018461#ifdef FEAT_FLOAT
18462 item_compare_float = FALSE;
18463#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018464 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018465 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018466 if (argvars[1].v_type != VAR_UNKNOWN)
18467 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018468 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018469 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018470 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018471 else
18472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018473 int error = FALSE;
18474
18475 i = get_tv_number_chk(&argvars[1], &error);
18476 if (error)
18477 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018478 if (i == 1)
18479 item_compare_ic = TRUE;
18480 else
18481 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018482 if (item_compare_func != NULL)
18483 {
18484 if (STRCMP(item_compare_func, "n") == 0)
18485 {
18486 item_compare_func = NULL;
18487 item_compare_numeric = TRUE;
18488 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018489 else if (STRCMP(item_compare_func, "N") == 0)
18490 {
18491 item_compare_func = NULL;
18492 item_compare_numbers = TRUE;
18493 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018494#ifdef FEAT_FLOAT
18495 else if (STRCMP(item_compare_func, "f") == 0)
18496 {
18497 item_compare_func = NULL;
18498 item_compare_float = TRUE;
18499 }
18500#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018501 else if (STRCMP(item_compare_func, "i") == 0)
18502 {
18503 item_compare_func = NULL;
18504 item_compare_ic = TRUE;
18505 }
18506 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018507 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018508
18509 if (argvars[2].v_type != VAR_UNKNOWN)
18510 {
18511 /* optional third argument: {dict} */
18512 if (argvars[2].v_type != VAR_DICT)
18513 {
18514 EMSG(_(e_dictreq));
18515 return;
18516 }
18517 item_compare_selfdict = argvars[2].vval.v_dict;
18518 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520
Bram Moolenaar0d660222005-01-07 21:51:51 +000018521 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018522 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018523 if (ptrs == NULL)
18524 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525
Bram Moolenaar327aa022014-03-25 18:24:23 +010018526 i = 0;
18527 if (sort)
18528 {
18529 /* sort(): ptrs will be the list to sort */
18530 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018531 {
18532 ptrs[i].item = li;
18533 ptrs[i].idx = i;
18534 ++i;
18535 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018536
18537 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018538 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018539 /* test the compare function */
18540 if (item_compare_func != NULL
18541 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018542 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018543 EMSG(_("E702: Sort compare function failed"));
18544 else
18545 {
18546 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018547 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018548 item_compare_func == NULL ? item_compare : item_compare2);
18549
18550 if (!item_compare_func_err)
18551 {
18552 /* Clear the List and append the items in sorted order. */
18553 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18554 l->lv_len = 0;
18555 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018556 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018557 }
18558 }
18559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018561 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018562 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018563
18564 /* f_uniq(): ptrs will be a stack of items to remove */
18565 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018566 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018567 item_compare_func_ptr = item_compare_func
18568 ? item_compare2 : item_compare;
18569
18570 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18571 li = li->li_next)
18572 {
18573 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18574 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018575 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018576 if (item_compare_func_err)
18577 {
18578 EMSG(_("E882: Uniq compare function failed"));
18579 break;
18580 }
18581 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018583 if (!item_compare_func_err)
18584 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018585 while (--i >= 0)
18586 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018587 li = ptrs[i].item->li_next;
18588 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018589 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018590 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018591 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018592 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018593 list_fix_watch(l, li);
18594 listitem_free(li);
18595 l->lv_len--;
18596 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018597 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018598 }
18599
18600 vim_free(ptrs);
18601 }
18602}
18603
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018604/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018605 * "sort({list})" function
18606 */
18607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018608f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018609{
18610 do_sort_uniq(argvars, rettv, TRUE);
18611}
18612
18613/*
18614 * "uniq({list})" function
18615 */
18616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018617f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018618{
18619 do_sort_uniq(argvars, rettv, FALSE);
18620}
18621
18622/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018623 * "soundfold({word})" function
18624 */
18625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018626f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018627{
18628 char_u *s;
18629
18630 rettv->v_type = VAR_STRING;
18631 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018632#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018633 rettv->vval.v_string = eval_soundfold(s);
18634#else
18635 rettv->vval.v_string = vim_strsave(s);
18636#endif
18637}
18638
18639/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018640 * "spellbadword()" function
18641 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018643f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018644{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018645 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018646 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018647 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018648
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018649 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018650 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018651
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018652#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018653 if (argvars[0].v_type == VAR_UNKNOWN)
18654 {
18655 /* Find the start and length of the badly spelled word. */
18656 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18657 if (len != 0)
18658 word = ml_get_cursor();
18659 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018660 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018661 {
18662 char_u *str = get_tv_string_chk(&argvars[0]);
18663 int capcol = -1;
18664
18665 if (str != NULL)
18666 {
18667 /* Check the argument for spelling. */
18668 while (*str != NUL)
18669 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018670 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018671 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018672 {
18673 word = str;
18674 break;
18675 }
18676 str += len;
18677 }
18678 }
18679 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018680#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018681
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018682 list_append_string(rettv->vval.v_list, word, len);
18683 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018684 attr == HLF_SPB ? "bad" :
18685 attr == HLF_SPR ? "rare" :
18686 attr == HLF_SPL ? "local" :
18687 attr == HLF_SPC ? "caps" :
18688 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018689}
18690
18691/*
18692 * "spellsuggest()" function
18693 */
18694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018695f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018696{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018697#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018698 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018699 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018700 int maxcount;
18701 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018702 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018703 listitem_T *li;
18704 int need_capital = FALSE;
18705#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018706
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018707 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018708 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018709
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018710#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018711 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018712 {
18713 str = get_tv_string(&argvars[0]);
18714 if (argvars[1].v_type != VAR_UNKNOWN)
18715 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018716 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018717 if (maxcount <= 0)
18718 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018719 if (argvars[2].v_type != VAR_UNKNOWN)
18720 {
18721 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18722 if (typeerr)
18723 return;
18724 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018725 }
18726 else
18727 maxcount = 25;
18728
Bram Moolenaar4770d092006-01-12 23:22:24 +000018729 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018730
18731 for (i = 0; i < ga.ga_len; ++i)
18732 {
18733 str = ((char_u **)ga.ga_data)[i];
18734
18735 li = listitem_alloc();
18736 if (li == NULL)
18737 vim_free(str);
18738 else
18739 {
18740 li->li_tv.v_type = VAR_STRING;
18741 li->li_tv.v_lock = 0;
18742 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018743 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018744 }
18745 }
18746 ga_clear(&ga);
18747 }
18748#endif
18749}
18750
Bram Moolenaar0d660222005-01-07 21:51:51 +000018751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018752f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018753{
18754 char_u *str;
18755 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018756 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018757 regmatch_T regmatch;
18758 char_u patbuf[NUMBUFLEN];
18759 char_u *save_cpo;
18760 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018761 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018762 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018763 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018764
18765 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18766 save_cpo = p_cpo;
18767 p_cpo = (char_u *)"";
18768
18769 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018770 if (argvars[1].v_type != VAR_UNKNOWN)
18771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018772 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18773 if (pat == NULL)
18774 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018775 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018776 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018777 }
18778 if (pat == NULL || *pat == NUL)
18779 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018780
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018781 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018783 if (typeerr)
18784 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785
Bram Moolenaar0d660222005-01-07 21:51:51 +000018786 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18787 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018789 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018790 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018791 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018792 if (*str == NUL)
18793 match = FALSE; /* empty item at the end */
18794 else
18795 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018796 if (match)
18797 end = regmatch.startp[0];
18798 else
18799 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018800 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18801 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018802 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018803 if (list_append_string(rettv->vval.v_list, str,
18804 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018805 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018806 }
18807 if (!match)
18808 break;
18809 /* Advance to just after the match. */
18810 if (regmatch.endp[0] > str)
18811 col = 0;
18812 else
18813 {
18814 /* Don't get stuck at the same match. */
18815#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018816 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018817#else
18818 col = 1;
18819#endif
18820 }
18821 str = regmatch.endp[0];
18822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823
Bram Moolenaar473de612013-06-08 18:19:48 +020018824 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826
Bram Moolenaar0d660222005-01-07 21:51:51 +000018827 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828}
18829
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018830#ifdef FEAT_FLOAT
18831/*
18832 * "sqrt()" function
18833 */
18834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018835f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018836{
18837 float_T f;
18838
18839 rettv->v_type = VAR_FLOAT;
18840 if (get_float_arg(argvars, &f) == OK)
18841 rettv->vval.v_float = sqrt(f);
18842 else
18843 rettv->vval.v_float = 0.0;
18844}
18845
18846/*
18847 * "str2float()" function
18848 */
18849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018850f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018851{
18852 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18853
18854 if (*p == '+')
18855 p = skipwhite(p + 1);
18856 (void)string2float(p, &rettv->vval.v_float);
18857 rettv->v_type = VAR_FLOAT;
18858}
18859#endif
18860
Bram Moolenaar2c932302006-03-18 21:42:09 +000018861/*
18862 * "str2nr()" function
18863 */
18864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018865f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018866{
18867 int base = 10;
18868 char_u *p;
18869 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018870 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018871
18872 if (argvars[1].v_type != VAR_UNKNOWN)
18873 {
18874 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018875 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018876 {
18877 EMSG(_(e_invarg));
18878 return;
18879 }
18880 }
18881
18882 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018883 if (*p == '+')
18884 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018885 switch (base)
18886 {
18887 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18888 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18889 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18890 default: what = 0;
18891 }
18892 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018893 rettv->vval.v_number = n;
18894}
18895
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896#ifdef HAVE_STRFTIME
18897/*
18898 * "strftime({format}[, {time}])" function
18899 */
18900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018901f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902{
18903 char_u result_buf[256];
18904 struct tm *curtime;
18905 time_t seconds;
18906 char_u *p;
18907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018908 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018910 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018911 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 seconds = time(NULL);
18913 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018914 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 curtime = localtime(&seconds);
18916 /* MSVC returns NULL for an invalid value of seconds. */
18917 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018918 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919 else
18920 {
18921# ifdef FEAT_MBYTE
18922 vimconv_T conv;
18923 char_u *enc;
18924
18925 conv.vc_type = CONV_NONE;
18926 enc = enc_locale();
18927 convert_setup(&conv, p_enc, enc);
18928 if (conv.vc_type != CONV_NONE)
18929 p = string_convert(&conv, p, NULL);
18930# endif
18931 if (p != NULL)
18932 (void)strftime((char *)result_buf, sizeof(result_buf),
18933 (char *)p, curtime);
18934 else
18935 result_buf[0] = NUL;
18936
18937# ifdef FEAT_MBYTE
18938 if (conv.vc_type != CONV_NONE)
18939 vim_free(p);
18940 convert_setup(&conv, enc, p_enc);
18941 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018942 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 else
18944# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018945 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946
18947# ifdef FEAT_MBYTE
18948 /* Release conversion descriptors */
18949 convert_setup(&conv, NULL, NULL);
18950 vim_free(enc);
18951# endif
18952 }
18953}
18954#endif
18955
18956/*
18957 * "stridx()" function
18958 */
18959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018960f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961{
18962 char_u buf[NUMBUFLEN];
18963 char_u *needle;
18964 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018965 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018967 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018969 needle = get_tv_string_chk(&argvars[1]);
18970 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018971 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018972 if (needle == NULL || haystack == NULL)
18973 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974
Bram Moolenaar33570922005-01-25 22:26:29 +000018975 if (argvars[2].v_type != VAR_UNKNOWN)
18976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018977 int error = FALSE;
18978
18979 start_idx = get_tv_number_chk(&argvars[2], &error);
18980 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018981 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018982 if (start_idx >= 0)
18983 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018984 }
18985
18986 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18987 if (pos != NULL)
18988 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989}
18990
18991/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018992 * "string()" function
18993 */
18994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018995f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018996{
18997 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018998 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019000 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019001 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019002 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019003 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019004 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005}
19006
19007/*
19008 * "strlen()" function
19009 */
19010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019011f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019013 rettv->vval.v_number = (varnumber_T)(STRLEN(
19014 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015}
19016
19017/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019018 * "strchars()" function
19019 */
19020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019021f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019022{
19023 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019024 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019025#ifdef FEAT_MBYTE
19026 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019027 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019028#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019029
19030 if (argvars[1].v_type != VAR_UNKNOWN)
19031 skipcc = get_tv_number_chk(&argvars[1], NULL);
19032 if (skipcc < 0 || skipcc > 1)
19033 EMSG(_(e_invarg));
19034 else
19035 {
19036#ifdef FEAT_MBYTE
19037 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19038 while (*s != NUL)
19039 {
19040 func_mb_ptr2char_adv(&s);
19041 ++len;
19042 }
19043 rettv->vval.v_number = len;
19044#else
19045 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19046#endif
19047 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019048}
19049
19050/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019051 * "strdisplaywidth()" function
19052 */
19053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019054f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019055{
19056 char_u *s = get_tv_string(&argvars[0]);
19057 int col = 0;
19058
19059 if (argvars[1].v_type != VAR_UNKNOWN)
19060 col = get_tv_number(&argvars[1]);
19061
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019062 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019063}
19064
19065/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019066 * "strwidth()" function
19067 */
19068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019069f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019070{
19071 char_u *s = get_tv_string(&argvars[0]);
19072
19073 rettv->vval.v_number = (varnumber_T)(
19074#ifdef FEAT_MBYTE
19075 mb_string2cells(s, -1)
19076#else
19077 STRLEN(s)
19078#endif
19079 );
19080}
19081
19082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 * "strpart()" function
19084 */
19085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019086f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087{
19088 char_u *p;
19089 int n;
19090 int len;
19091 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019092 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019094 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 slen = (int)STRLEN(p);
19096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019097 n = get_tv_number_chk(&argvars[1], &error);
19098 if (error)
19099 len = 0;
19100 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019101 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 else
19103 len = slen - n; /* default len: all bytes that are available. */
19104
19105 /*
19106 * Only return the overlap between the specified part and the actual
19107 * string.
19108 */
19109 if (n < 0)
19110 {
19111 len += n;
19112 n = 0;
19113 }
19114 else if (n > slen)
19115 n = slen;
19116 if (len < 0)
19117 len = 0;
19118 else if (n + len > slen)
19119 len = slen - n;
19120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019121 rettv->v_type = VAR_STRING;
19122 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123}
19124
19125/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019126 * "strridx()" function
19127 */
19128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019129f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019130{
19131 char_u buf[NUMBUFLEN];
19132 char_u *needle;
19133 char_u *haystack;
19134 char_u *rest;
19135 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019136 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019138 needle = get_tv_string_chk(&argvars[1]);
19139 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019140
19141 rettv->vval.v_number = -1;
19142 if (needle == NULL || haystack == NULL)
19143 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019144
19145 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019146 if (argvars[2].v_type != VAR_UNKNOWN)
19147 {
19148 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019149 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019150 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019151 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019152 }
19153 else
19154 end_idx = haystack_len;
19155
Bram Moolenaar0d660222005-01-07 21:51:51 +000019156 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019157 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019158 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019159 lastmatch = haystack + end_idx;
19160 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019161 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019162 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019163 for (rest = haystack; *rest != '\0'; ++rest)
19164 {
19165 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019166 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019167 break;
19168 lastmatch = rest;
19169 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019170 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019171
19172 if (lastmatch == NULL)
19173 rettv->vval.v_number = -1;
19174 else
19175 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19176}
19177
19178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 * "strtrans()" function
19180 */
19181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019182f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019184 rettv->v_type = VAR_STRING;
19185 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186}
19187
19188/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019189 * "submatch()" function
19190 */
19191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019192f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019193{
Bram Moolenaar41571762014-04-02 19:00:58 +020019194 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019195 int no;
19196 int retList = 0;
19197
19198 no = (int)get_tv_number_chk(&argvars[0], &error);
19199 if (error)
19200 return;
19201 error = FALSE;
19202 if (argvars[1].v_type != VAR_UNKNOWN)
19203 retList = get_tv_number_chk(&argvars[1], &error);
19204 if (error)
19205 return;
19206
19207 if (retList == 0)
19208 {
19209 rettv->v_type = VAR_STRING;
19210 rettv->vval.v_string = reg_submatch(no);
19211 }
19212 else
19213 {
19214 rettv->v_type = VAR_LIST;
19215 rettv->vval.v_list = reg_submatch_list(no);
19216 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019217}
19218
19219/*
19220 * "substitute()" function
19221 */
19222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019223f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019224{
19225 char_u patbuf[NUMBUFLEN];
19226 char_u subbuf[NUMBUFLEN];
19227 char_u flagsbuf[NUMBUFLEN];
19228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019229 char_u *str = get_tv_string_chk(&argvars[0]);
19230 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19231 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19232 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19233
Bram Moolenaar0d660222005-01-07 21:51:51 +000019234 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019235 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19236 rettv->vval.v_string = NULL;
19237 else
19238 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019239}
19240
19241/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019242 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019245f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246{
19247 int id = 0;
19248#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019249 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 long col;
19251 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019252 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019254 lnum = get_tv_lnum(argvars); /* -1 on type error */
19255 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19256 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019258 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019259 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019260 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261#endif
19262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019263 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264}
19265
19266/*
19267 * "synIDattr(id, what [, mode])" function
19268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019270f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271{
19272 char_u *p = NULL;
19273#ifdef FEAT_SYN_HL
19274 int id;
19275 char_u *what;
19276 char_u *mode;
19277 char_u modebuf[NUMBUFLEN];
19278 int modec;
19279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019280 id = get_tv_number(&argvars[0]);
19281 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019282 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019284 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019286 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 modec = 0; /* replace invalid with current */
19288 }
19289 else
19290 {
19291#ifdef FEAT_GUI
19292 if (gui.in_use)
19293 modec = 'g';
19294 else
19295#endif
19296 if (t_colors > 1)
19297 modec = 'c';
19298 else
19299 modec = 't';
19300 }
19301
19302
19303 switch (TOLOWER_ASC(what[0]))
19304 {
19305 case 'b':
19306 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19307 p = highlight_color(id, what, modec);
19308 else /* bold */
19309 p = highlight_has_attr(id, HL_BOLD, modec);
19310 break;
19311
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019312 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 p = highlight_color(id, what, modec);
19314 break;
19315
19316 case 'i':
19317 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19318 p = highlight_has_attr(id, HL_INVERSE, modec);
19319 else /* italic */
19320 p = highlight_has_attr(id, HL_ITALIC, modec);
19321 break;
19322
19323 case 'n': /* name */
19324 p = get_highlight_name(NULL, id - 1);
19325 break;
19326
19327 case 'r': /* reverse */
19328 p = highlight_has_attr(id, HL_INVERSE, modec);
19329 break;
19330
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019331 case 's':
19332 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19333 p = highlight_color(id, what, modec);
19334 else /* standout */
19335 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 break;
19337
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019338 case 'u':
19339 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19340 /* underline */
19341 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19342 else
19343 /* undercurl */
19344 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 break;
19346 }
19347
19348 if (p != NULL)
19349 p = vim_strsave(p);
19350#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019351 rettv->v_type = VAR_STRING;
19352 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353}
19354
19355/*
19356 * "synIDtrans(id)" function
19357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019359f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360{
19361 int id;
19362
19363#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019364 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365
19366 if (id > 0)
19367 id = syn_get_final_id(id);
19368 else
19369#endif
19370 id = 0;
19371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019372 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373}
19374
19375/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019376 * "synconcealed(lnum, col)" function
19377 */
19378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019379f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019380{
19381#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19382 long lnum;
19383 long col;
19384 int syntax_flags = 0;
19385 int cchar;
19386 int matchid = 0;
19387 char_u str[NUMBUFLEN];
19388#endif
19389
19390 rettv->v_type = VAR_LIST;
19391 rettv->vval.v_list = NULL;
19392
19393#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19394 lnum = get_tv_lnum(argvars); /* -1 on type error */
19395 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19396
19397 vim_memset(str, NUL, sizeof(str));
19398
19399 if (rettv_list_alloc(rettv) != FAIL)
19400 {
19401 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19402 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19403 && curwin->w_p_cole > 0)
19404 {
19405 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19406 syntax_flags = get_syntax_info(&matchid);
19407
19408 /* get the conceal character */
19409 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19410 {
19411 cchar = syn_get_sub_char();
19412 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19413 cchar = lcs_conceal;
19414 if (cchar != NUL)
19415 {
19416# ifdef FEAT_MBYTE
19417 if (has_mbyte)
19418 (*mb_char2bytes)(cchar, str);
19419 else
19420# endif
19421 str[0] = cchar;
19422 }
19423 }
19424 }
19425
19426 list_append_number(rettv->vval.v_list,
19427 (syntax_flags & HL_CONCEAL) != 0);
19428 /* -1 to auto-determine strlen */
19429 list_append_string(rettv->vval.v_list, str, -1);
19430 list_append_number(rettv->vval.v_list, matchid);
19431 }
19432#endif
19433}
19434
19435/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019436 * "synstack(lnum, col)" function
19437 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019439f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019440{
19441#ifdef FEAT_SYN_HL
19442 long lnum;
19443 long col;
19444 int i;
19445 int id;
19446#endif
19447
19448 rettv->v_type = VAR_LIST;
19449 rettv->vval.v_list = NULL;
19450
19451#ifdef FEAT_SYN_HL
19452 lnum = get_tv_lnum(argvars); /* -1 on type error */
19453 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19454
19455 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019456 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019457 && rettv_list_alloc(rettv) != FAIL)
19458 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019459 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019460 for (i = 0; ; ++i)
19461 {
19462 id = syn_get_stack_item(i);
19463 if (id < 0)
19464 break;
19465 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19466 break;
19467 }
19468 }
19469#endif
19470}
19471
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019473get_cmd_output_as_rettv(
19474 typval_T *argvars,
19475 typval_T *rettv,
19476 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019478 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019480 char_u *infile = NULL;
19481 char_u buf[NUMBUFLEN];
19482 int err = FALSE;
19483 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019484 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019485 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019487 rettv->v_type = VAR_STRING;
19488 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019489 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019490 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019491
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019492 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019493 {
19494 /*
19495 * Write the string to a temp file, to be used for input of the shell
19496 * command.
19497 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019498 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019499 {
19500 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019501 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019502 }
19503
19504 fd = mch_fopen((char *)infile, WRITEBIN);
19505 if (fd == NULL)
19506 {
19507 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019508 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019509 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019510 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019511 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019512 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19513 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019514 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019515 else
19516 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019517 size_t len;
19518
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019519 p = get_tv_string_buf_chk(&argvars[1], buf);
19520 if (p == NULL)
19521 {
19522 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019523 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019524 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019525 len = STRLEN(p);
19526 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019527 err = TRUE;
19528 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019529 if (fclose(fd) != 0)
19530 err = TRUE;
19531 if (err)
19532 {
19533 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019534 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019535 }
19536 }
19537
Bram Moolenaar52a72462014-08-29 15:53:52 +020019538 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19539 * echoes typeahead, that messes up the display. */
19540 if (!msg_silent)
19541 flags += SHELL_COOKED;
19542
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019543 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019545 int len;
19546 listitem_T *li;
19547 char_u *s = NULL;
19548 char_u *start;
19549 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019550 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551
Bram Moolenaar52a72462014-08-29 15:53:52 +020019552 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019553 if (res == NULL)
19554 goto errret;
19555
19556 list = list_alloc();
19557 if (list == NULL)
19558 goto errret;
19559
19560 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019562 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019563 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019564 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019565 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019566
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019567 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019568 if (s == NULL)
19569 goto errret;
19570
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019571 for (p = s; start < end; ++p, ++start)
19572 *p = *start == NUL ? NL : *start;
19573 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019574
19575 li = listitem_alloc();
19576 if (li == NULL)
19577 {
19578 vim_free(s);
19579 goto errret;
19580 }
19581 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019582 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019583 li->li_tv.vval.v_string = s;
19584 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019586
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019587 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019588 rettv->v_type = VAR_LIST;
19589 rettv->vval.v_list = list;
19590 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019592 else
19593 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019594 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019595#ifdef USE_CR
19596 /* translate <CR> into <NL> */
19597 if (res != NULL)
19598 {
19599 char_u *s;
19600
19601 for (s = res; *s; ++s)
19602 {
19603 if (*s == CAR)
19604 *s = NL;
19605 }
19606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607#else
19608# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019609 /* translate <CR><NL> into <NL> */
19610 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019612 char_u *s, *d;
19613
19614 d = res;
19615 for (s = res; *s; ++s)
19616 {
19617 if (s[0] == CAR && s[1] == NL)
19618 ++s;
19619 *d++ = *s;
19620 }
19621 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623# endif
19624#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019625 rettv->vval.v_string = res;
19626 res = NULL;
19627 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019628
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019629errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019630 if (infile != NULL)
19631 {
19632 mch_remove(infile);
19633 vim_free(infile);
19634 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019635 if (res != NULL)
19636 vim_free(res);
19637 if (list != NULL)
19638 list_free(list, TRUE);
19639}
19640
19641/*
19642 * "system()" function
19643 */
19644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019645f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019646{
19647 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19648}
19649
19650/*
19651 * "systemlist()" function
19652 */
19653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019654f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019655{
19656 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657}
19658
19659/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019660 * "tabpagebuflist()" function
19661 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019663f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019664{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019665#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019666 tabpage_T *tp;
19667 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019668
19669 if (argvars[0].v_type == VAR_UNKNOWN)
19670 wp = firstwin;
19671 else
19672 {
19673 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19674 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019675 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019676 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019677 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019678 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019679 for (; wp != NULL; wp = wp->w_next)
19680 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019681 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019682 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019683 }
19684#endif
19685}
19686
19687
19688/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019689 * "tabpagenr()" function
19690 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019692f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019693{
19694 int nr = 1;
19695#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019696 char_u *arg;
19697
19698 if (argvars[0].v_type != VAR_UNKNOWN)
19699 {
19700 arg = get_tv_string_chk(&argvars[0]);
19701 nr = 0;
19702 if (arg != NULL)
19703 {
19704 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019705 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019706 else
19707 EMSG2(_(e_invexpr2), arg);
19708 }
19709 }
19710 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019711 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019712#endif
19713 rettv->vval.v_number = nr;
19714}
19715
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019716
19717#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019718static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019719
19720/*
19721 * Common code for tabpagewinnr() and winnr().
19722 */
19723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019724get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019725{
19726 win_T *twin;
19727 int nr = 1;
19728 win_T *wp;
19729 char_u *arg;
19730
19731 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19732 if (argvar->v_type != VAR_UNKNOWN)
19733 {
19734 arg = get_tv_string_chk(argvar);
19735 if (arg == NULL)
19736 nr = 0; /* type error; errmsg already given */
19737 else if (STRCMP(arg, "$") == 0)
19738 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19739 else if (STRCMP(arg, "#") == 0)
19740 {
19741 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19742 if (twin == NULL)
19743 nr = 0;
19744 }
19745 else
19746 {
19747 EMSG2(_(e_invexpr2), arg);
19748 nr = 0;
19749 }
19750 }
19751
19752 if (nr > 0)
19753 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19754 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019755 {
19756 if (wp == NULL)
19757 {
19758 /* didn't find it in this tabpage */
19759 nr = 0;
19760 break;
19761 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019762 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019763 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019764 return nr;
19765}
19766#endif
19767
19768/*
19769 * "tabpagewinnr()" function
19770 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019772f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019773{
19774 int nr = 1;
19775#ifdef FEAT_WINDOWS
19776 tabpage_T *tp;
19777
19778 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19779 if (tp == NULL)
19780 nr = 0;
19781 else
19782 nr = get_winnr(tp, &argvars[1]);
19783#endif
19784 rettv->vval.v_number = nr;
19785}
19786
19787
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019788/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019789 * "tagfiles()" function
19790 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019792f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019793{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019794 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019795 tagname_T tn;
19796 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019797
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019798 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019799 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019800 fname = alloc(MAXPATHL);
19801 if (fname == NULL)
19802 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019803
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019804 for (first = TRUE; ; first = FALSE)
19805 if (get_tagfname(&tn, first, fname) == FAIL
19806 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019807 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019808 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019809 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019810}
19811
19812/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019813 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019814 */
19815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019816f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019817{
19818 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019819
19820 tag_pattern = get_tv_string(&argvars[0]);
19821
19822 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019823 if (*tag_pattern == NUL)
19824 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019825
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019826 if (rettv_list_alloc(rettv) == OK)
19827 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019828}
19829
19830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 * "tempname()" function
19832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019834f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835{
19836 static int x = 'A';
19837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019839 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840
19841 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19842 * names. Skip 'I' and 'O', they are used for shell redirection. */
19843 do
19844 {
19845 if (x == 'Z')
19846 x = '0';
19847 else if (x == '9')
19848 x = 'A';
19849 else
19850 {
19851#ifdef EBCDIC
19852 if (x == 'I')
19853 x = 'J';
19854 else if (x == 'R')
19855 x = 'S';
19856 else
19857#endif
19858 ++x;
19859 }
19860 } while (x == 'I' || x == 'O');
19861}
19862
19863/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019864 * "test(list)" function: Just checking the walls...
19865 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019867f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019868{
19869 /* Used for unit testing. Change the code below to your liking. */
19870#if 0
19871 listitem_T *li;
19872 list_T *l;
19873 char_u *bad, *good;
19874
19875 if (argvars[0].v_type != VAR_LIST)
19876 return;
19877 l = argvars[0].vval.v_list;
19878 if (l == NULL)
19879 return;
19880 li = l->lv_first;
19881 if (li == NULL)
19882 return;
19883 bad = get_tv_string(&li->li_tv);
19884 li = li->li_next;
19885 if (li == NULL)
19886 return;
19887 good = get_tv_string(&li->li_tv);
19888 rettv->vval.v_number = test_edit_score(bad, good);
19889#endif
19890}
19891
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019892#ifdef FEAT_FLOAT
19893/*
19894 * "tan()" function
19895 */
19896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019897f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019898{
19899 float_T f;
19900
19901 rettv->v_type = VAR_FLOAT;
19902 if (get_float_arg(argvars, &f) == OK)
19903 rettv->vval.v_float = tan(f);
19904 else
19905 rettv->vval.v_float = 0.0;
19906}
19907
19908/*
19909 * "tanh()" function
19910 */
19911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019912f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019913{
19914 float_T f;
19915
19916 rettv->v_type = VAR_FLOAT;
19917 if (get_float_arg(argvars, &f) == OK)
19918 rettv->vval.v_float = tanh(f);
19919 else
19920 rettv->vval.v_float = 0.0;
19921}
19922#endif
19923
Bram Moolenaard52d9742005-08-21 22:20:28 +000019924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 * "tolower(string)" function
19926 */
19927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019928f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929{
19930 char_u *p;
19931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019932 p = vim_strsave(get_tv_string(&argvars[0]));
19933 rettv->v_type = VAR_STRING;
19934 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935
19936 if (p != NULL)
19937 while (*p != NUL)
19938 {
19939#ifdef FEAT_MBYTE
19940 int l;
19941
19942 if (enc_utf8)
19943 {
19944 int c, lc;
19945
19946 c = utf_ptr2char(p);
19947 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019948 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 /* TODO: reallocate string when byte count changes. */
19950 if (utf_char2len(lc) == l)
19951 utf_char2bytes(lc, p);
19952 p += l;
19953 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019954 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 p += l; /* skip multi-byte character */
19956 else
19957#endif
19958 {
19959 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19960 ++p;
19961 }
19962 }
19963}
19964
19965/*
19966 * "toupper(string)" function
19967 */
19968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019969f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019972 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973}
19974
19975/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019976 * "tr(string, fromstr, tostr)" function
19977 */
19978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019979f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019980{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019981 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019982 char_u *fromstr;
19983 char_u *tostr;
19984 char_u *p;
19985#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019986 int inlen;
19987 int fromlen;
19988 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019989 int idx;
19990 char_u *cpstr;
19991 int cplen;
19992 int first = TRUE;
19993#endif
19994 char_u buf[NUMBUFLEN];
19995 char_u buf2[NUMBUFLEN];
19996 garray_T ga;
19997
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019998 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019999 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20000 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020001
20002 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020003 rettv->v_type = VAR_STRING;
20004 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020005 if (fromstr == NULL || tostr == NULL)
20006 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020007 ga_init2(&ga, (int)sizeof(char), 80);
20008
20009#ifdef FEAT_MBYTE
20010 if (!has_mbyte)
20011#endif
20012 /* not multi-byte: fromstr and tostr must be the same length */
20013 if (STRLEN(fromstr) != STRLEN(tostr))
20014 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020015#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020016error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020017#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020018 EMSG2(_(e_invarg2), fromstr);
20019 ga_clear(&ga);
20020 return;
20021 }
20022
20023 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020024 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020025 {
20026#ifdef FEAT_MBYTE
20027 if (has_mbyte)
20028 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020029 inlen = (*mb_ptr2len)(in_str);
20030 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020031 cplen = inlen;
20032 idx = 0;
20033 for (p = fromstr; *p != NUL; p += fromlen)
20034 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020035 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020036 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020037 {
20038 for (p = tostr; *p != NUL; p += tolen)
20039 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020040 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020041 if (idx-- == 0)
20042 {
20043 cplen = tolen;
20044 cpstr = p;
20045 break;
20046 }
20047 }
20048 if (*p == NUL) /* tostr is shorter than fromstr */
20049 goto error;
20050 break;
20051 }
20052 ++idx;
20053 }
20054
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020055 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020056 {
20057 /* Check that fromstr and tostr have the same number of
20058 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020059 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020060 first = FALSE;
20061 for (p = tostr; *p != NUL; p += tolen)
20062 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020063 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020064 --idx;
20065 }
20066 if (idx != 0)
20067 goto error;
20068 }
20069
Bram Moolenaarcde88542015-08-11 19:14:00 +020020070 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020071 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020072 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020073
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020074 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020075 }
20076 else
20077#endif
20078 {
20079 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020080 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020081 if (p != NULL)
20082 ga_append(&ga, tostr[p - fromstr]);
20083 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020084 ga_append(&ga, *in_str);
20085 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020086 }
20087 }
20088
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020089 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020090 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020091 ga_append(&ga, NUL);
20092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020093 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020094}
20095
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020096#ifdef FEAT_FLOAT
20097/*
20098 * "trunc({float})" function
20099 */
20100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020101f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020102{
20103 float_T f;
20104
20105 rettv->v_type = VAR_FLOAT;
20106 if (get_float_arg(argvars, &f) == OK)
20107 /* trunc() is not in C90, use floor() or ceil() instead. */
20108 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20109 else
20110 rettv->vval.v_float = 0.0;
20111}
20112#endif
20113
Bram Moolenaar8299df92004-07-10 09:47:34 +000020114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 * "type(expr)" function
20116 */
20117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020118f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020120 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020121
20122 switch (argvars[0].v_type)
20123 {
20124 case VAR_NUMBER: n = 0; break;
20125 case VAR_STRING: n = 1; break;
20126 case VAR_FUNC: n = 2; break;
20127 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020128 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020129 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020130 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020131 if (argvars[0].vval.v_number == VVAL_FALSE
20132 || argvars[0].vval.v_number == VVAL_TRUE)
20133 n = 6;
20134 else
20135 n = 7;
20136 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020137 case VAR_JOB: n = 8; break;
20138 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020139 case VAR_UNKNOWN:
20140 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20141 n = -1;
20142 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020143 }
20144 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145}
20146
20147/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020148 * "undofile(name)" function
20149 */
20150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020151f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020152{
20153 rettv->v_type = VAR_STRING;
20154#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020155 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020156 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020157
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020158 if (*fname == NUL)
20159 {
20160 /* If there is no file name there will be no undo file. */
20161 rettv->vval.v_string = NULL;
20162 }
20163 else
20164 {
20165 char_u *ffname = FullName_save(fname, FALSE);
20166
20167 if (ffname != NULL)
20168 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20169 vim_free(ffname);
20170 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020171 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020172#else
20173 rettv->vval.v_string = NULL;
20174#endif
20175}
20176
20177/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020178 * "undotree()" function
20179 */
20180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020181f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020182{
20183 if (rettv_dict_alloc(rettv) == OK)
20184 {
20185 dict_T *dict = rettv->vval.v_dict;
20186 list_T *list;
20187
Bram Moolenaar730cde92010-06-27 05:18:54 +020020188 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020189 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020190 dict_add_nr_str(dict, "save_last",
20191 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020192 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20193 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020194 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020195
20196 list = list_alloc();
20197 if (list != NULL)
20198 {
20199 u_eval_tree(curbuf->b_u_oldhead, list);
20200 dict_add_list(dict, "entries", list);
20201 }
20202 }
20203}
20204
20205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020206 * "values(dict)" function
20207 */
20208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020209f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020210{
20211 dict_list(argvars, rettv, 1);
20212}
20213
20214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 * "virtcol(string)" function
20216 */
20217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020218f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219{
20220 colnr_T vcol = 0;
20221 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020222 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020224 fp = var2fpos(&argvars[0], FALSE, &fnum);
20225 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20226 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 {
20228 getvvcol(curwin, fp, NULL, NULL, &vcol);
20229 ++vcol;
20230 }
20231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020232 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233}
20234
20235/*
20236 * "visualmode()" function
20237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020239f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 char_u str[2];
20242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020243 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 str[0] = curbuf->b_visual_mode_eval;
20245 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020246 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247
20248 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020249 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251}
20252
20253/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020254 * "wildmenumode()" function
20255 */
20256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020257f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020258{
20259#ifdef FEAT_WILDMENU
20260 if (wild_menu_showing)
20261 rettv->vval.v_number = 1;
20262#endif
20263}
20264
20265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 * "winbufnr(nr)" function
20267 */
20268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020269f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270{
20271 win_T *wp;
20272
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020273 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020275 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020277 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278}
20279
20280/*
20281 * "wincol()" function
20282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020284f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285{
20286 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020287 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288}
20289
20290/*
20291 * "winheight(nr)" function
20292 */
20293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020294f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295{
20296 win_T *wp;
20297
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020298 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020302 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303}
20304
20305/*
20306 * "winline()" function
20307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020309f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310{
20311 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020312 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313}
20314
20315/*
20316 * "winnr()" function
20317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020319f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320{
20321 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020322
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020324 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020326 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327}
20328
20329/*
20330 * "winrestcmd()" function
20331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020333f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334{
20335#ifdef FEAT_WINDOWS
20336 win_T *wp;
20337 int winnr = 1;
20338 garray_T ga;
20339 char_u buf[50];
20340
20341 ga_init2(&ga, (int)sizeof(char), 70);
20342 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20343 {
20344 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20345 ga_concat(&ga, buf);
20346# ifdef FEAT_VERTSPLIT
20347 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20348 ga_concat(&ga, buf);
20349# endif
20350 ++winnr;
20351 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020352 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020354 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020358 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359}
20360
20361/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020362 * "winrestview()" function
20363 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020365f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020366{
20367 dict_T *dict;
20368
20369 if (argvars[0].v_type != VAR_DICT
20370 || (dict = argvars[0].vval.v_dict) == NULL)
20371 EMSG(_(e_invarg));
20372 else
20373 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020374 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20375 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20376 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20377 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020378#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020379 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20380 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020381#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020382 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20383 {
20384 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20385 curwin->w_set_curswant = FALSE;
20386 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020387
Bram Moolenaar82c25852014-05-28 16:47:16 +020020388 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20389 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020390#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020391 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20392 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020393#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020394 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20395 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20396 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20397 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020398
20399 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020400 win_new_height(curwin, curwin->w_height);
20401# ifdef FEAT_VERTSPLIT
20402 win_new_width(curwin, W_WIDTH(curwin));
20403# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020404 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020405
Bram Moolenaarb851a962014-10-31 15:45:52 +010020406 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020407 curwin->w_topline = 1;
20408 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20409 curwin->w_topline = curbuf->b_ml.ml_line_count;
20410#ifdef FEAT_DIFF
20411 check_topfill(curwin, TRUE);
20412#endif
20413 }
20414}
20415
20416/*
20417 * "winsaveview()" function
20418 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020420f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020421{
20422 dict_T *dict;
20423
Bram Moolenaara800b422010-06-27 01:15:55 +020020424 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020425 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020426 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020427
20428 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20429 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20430#ifdef FEAT_VIRTUALEDIT
20431 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20432#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020433 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020434 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20435
20436 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20437#ifdef FEAT_DIFF
20438 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20439#endif
20440 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20441 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20442}
20443
20444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 * "winwidth(nr)" function
20446 */
20447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020448f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449{
20450 win_T *wp;
20451
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020452 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020454 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 else
20456#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020457 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020459 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460#endif
20461}
20462
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020464 * "wordcount()" function
20465 */
20466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020467f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020468{
20469 if (rettv_dict_alloc(rettv) == FAIL)
20470 return;
20471 cursor_pos_info(rettv->vval.v_dict);
20472}
20473
20474/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020475 * Write list of strings to file
20476 */
20477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020478write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020479{
20480 listitem_T *li;
20481 int c;
20482 int ret = OK;
20483 char_u *s;
20484
20485 for (li = list->lv_first; li != NULL; li = li->li_next)
20486 {
20487 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20488 {
20489 if (*s == '\n')
20490 c = putc(NUL, fd);
20491 else
20492 c = putc(*s, fd);
20493 if (c == EOF)
20494 {
20495 ret = FAIL;
20496 break;
20497 }
20498 }
20499 if (!binary || li->li_next != NULL)
20500 if (putc('\n', fd) == EOF)
20501 {
20502 ret = FAIL;
20503 break;
20504 }
20505 if (ret == FAIL)
20506 {
20507 EMSG(_(e_write));
20508 break;
20509 }
20510 }
20511 return ret;
20512}
20513
20514/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020515 * "writefile()" function
20516 */
20517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020518f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020519{
20520 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020521 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020522 char_u *fname;
20523 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020524 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020525
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020526 if (check_restricted() || check_secure())
20527 return;
20528
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020529 if (argvars[0].v_type != VAR_LIST)
20530 {
20531 EMSG2(_(e_listarg), "writefile()");
20532 return;
20533 }
20534 if (argvars[0].vval.v_list == NULL)
20535 return;
20536
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020537 if (argvars[2].v_type != VAR_UNKNOWN)
20538 {
20539 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20540 binary = TRUE;
20541 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20542 append = TRUE;
20543 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020544
20545 /* Always open the file in binary mode, library functions have a mind of
20546 * their own about CR-LF conversion. */
20547 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020548 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20549 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020550 {
20551 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20552 ret = -1;
20553 }
20554 else
20555 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020556 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20557 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020558 fclose(fd);
20559 }
20560
20561 rettv->vval.v_number = ret;
20562}
20563
20564/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020565 * "xor(expr, expr)" function
20566 */
20567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020568f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020569{
20570 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20571 ^ get_tv_number_chk(&argvars[1], NULL);
20572}
20573
20574
20575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020577 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 */
20579 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020580var2fpos(
20581 typval_T *varp,
20582 int dollar_lnum, /* TRUE when $ is last line */
20583 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020585 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020587 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588
Bram Moolenaara5525202006-03-02 22:52:09 +000020589 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020590 if (varp->v_type == VAR_LIST)
20591 {
20592 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020593 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020594 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020595 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020596
20597 l = varp->vval.v_list;
20598 if (l == NULL)
20599 return NULL;
20600
20601 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020602 pos.lnum = list_find_nr(l, 0L, &error);
20603 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020604 return NULL; /* invalid line number */
20605
20606 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020607 pos.col = list_find_nr(l, 1L, &error);
20608 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020609 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020610 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020611
20612 /* We accept "$" for the column number: last column. */
20613 li = list_find(l, 1L);
20614 if (li != NULL && li->li_tv.v_type == VAR_STRING
20615 && li->li_tv.vval.v_string != NULL
20616 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20617 pos.col = len + 1;
20618
Bram Moolenaara5525202006-03-02 22:52:09 +000020619 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020620 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020621 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020622 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020623
Bram Moolenaara5525202006-03-02 22:52:09 +000020624#ifdef FEAT_VIRTUALEDIT
20625 /* Get the virtual offset. Defaults to zero. */
20626 pos.coladd = list_find_nr(l, 2L, &error);
20627 if (error)
20628 pos.coladd = 0;
20629#endif
20630
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020631 return &pos;
20632 }
20633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020634 name = get_tv_string_chk(varp);
20635 if (name == NULL)
20636 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020637 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020639 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20640 {
20641 if (VIsual_active)
20642 return &VIsual;
20643 return &curwin->w_cursor;
20644 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020645 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020647 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20649 return NULL;
20650 return pp;
20651 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020652
20653#ifdef FEAT_VIRTUALEDIT
20654 pos.coladd = 0;
20655#endif
20656
Bram Moolenaar477933c2007-07-17 14:32:23 +000020657 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020658 {
20659 pos.col = 0;
20660 if (name[1] == '0') /* "w0": first visible line */
20661 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020662 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020663 pos.lnum = curwin->w_topline;
20664 return &pos;
20665 }
20666 else if (name[1] == '$') /* "w$": last visible line */
20667 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020668 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020669 pos.lnum = curwin->w_botline - 1;
20670 return &pos;
20671 }
20672 }
20673 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020675 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 {
20677 pos.lnum = curbuf->b_ml.ml_line_count;
20678 pos.col = 0;
20679 }
20680 else
20681 {
20682 pos.lnum = curwin->w_cursor.lnum;
20683 pos.col = (colnr_T)STRLEN(ml_get_curline());
20684 }
20685 return &pos;
20686 }
20687 return NULL;
20688}
20689
20690/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020691 * Convert list in "arg" into a position and optional file number.
20692 * When "fnump" is NULL there is no file number, only 3 items.
20693 * Note that the column is passed on as-is, the caller may want to decrement
20694 * it to use 1 for the first column.
20695 * Return FAIL when conversion is not possible, doesn't check the position for
20696 * validity.
20697 */
20698 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020699list2fpos(
20700 typval_T *arg,
20701 pos_T *posp,
20702 int *fnump,
20703 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020704{
20705 list_T *l = arg->vval.v_list;
20706 long i = 0;
20707 long n;
20708
Bram Moolenaar493c1782014-05-28 14:34:46 +020020709 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20710 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020711 if (arg->v_type != VAR_LIST
20712 || l == NULL
20713 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020714 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020715 return FAIL;
20716
20717 if (fnump != NULL)
20718 {
20719 n = list_find_nr(l, i++, NULL); /* fnum */
20720 if (n < 0)
20721 return FAIL;
20722 if (n == 0)
20723 n = curbuf->b_fnum; /* current buffer */
20724 *fnump = n;
20725 }
20726
20727 n = list_find_nr(l, i++, NULL); /* lnum */
20728 if (n < 0)
20729 return FAIL;
20730 posp->lnum = n;
20731
20732 n = list_find_nr(l, i++, NULL); /* col */
20733 if (n < 0)
20734 return FAIL;
20735 posp->col = n;
20736
20737#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020738 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020739 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020740 posp->coladd = 0;
20741 else
20742 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020743#endif
20744
Bram Moolenaar493c1782014-05-28 14:34:46 +020020745 if (curswantp != NULL)
20746 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20747
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020748 return OK;
20749}
20750
20751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 * Get the length of an environment variable name.
20753 * Advance "arg" to the first character after the name.
20754 * Return 0 for error.
20755 */
20756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020757get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758{
20759 char_u *p;
20760 int len;
20761
20762 for (p = *arg; vim_isIDc(*p); ++p)
20763 ;
20764 if (p == *arg) /* no name found */
20765 return 0;
20766
20767 len = (int)(p - *arg);
20768 *arg = p;
20769 return len;
20770}
20771
20772/*
20773 * Get the length of the name of a function or internal variable.
20774 * "arg" is advanced to the first non-white character after the name.
20775 * Return 0 if something is wrong.
20776 */
20777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020778get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779{
20780 char_u *p;
20781 int len;
20782
20783 /* Find the end of the name. */
20784 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020785 {
20786 if (*p == ':')
20787 {
20788 /* "s:" is start of "s:var", but "n:" is not and can be used in
20789 * slice "[n:]". Also "xx:" is not a namespace. */
20790 len = (int)(p - *arg);
20791 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20792 || len > 1)
20793 break;
20794 }
20795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 if (p == *arg) /* no name found */
20797 return 0;
20798
20799 len = (int)(p - *arg);
20800 *arg = skipwhite(p);
20801
20802 return len;
20803}
20804
20805/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020806 * Get the length of the name of a variable or function.
20807 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020809 * Return -1 if curly braces expansion failed.
20810 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 * If the name contains 'magic' {}'s, expand them and return the
20812 * expanded name in an allocated string via 'alias' - caller must free.
20813 */
20814 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020815get_name_len(
20816 char_u **arg,
20817 char_u **alias,
20818 int evaluate,
20819 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820{
20821 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 char_u *p;
20823 char_u *expr_start;
20824 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825
20826 *alias = NULL; /* default to no alias */
20827
20828 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20829 && (*arg)[2] == (int)KE_SNR)
20830 {
20831 /* hard coded <SNR>, already translated */
20832 *arg += 3;
20833 return get_id_len(arg) + 3;
20834 }
20835 len = eval_fname_script(*arg);
20836 if (len > 0)
20837 {
20838 /* literal "<SID>", "s:" or "<SNR>" */
20839 *arg += len;
20840 }
20841
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020845 p = find_name_end(*arg, &expr_start, &expr_end,
20846 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 if (expr_start != NULL)
20848 {
20849 char_u *temp_string;
20850
20851 if (!evaluate)
20852 {
20853 len += (int)(p - *arg);
20854 *arg = skipwhite(p);
20855 return len;
20856 }
20857
20858 /*
20859 * Include any <SID> etc in the expanded string:
20860 * Thus the -len here.
20861 */
20862 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20863 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020864 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865 *alias = temp_string;
20866 *arg = skipwhite(p);
20867 return (int)STRLEN(temp_string);
20868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869
20870 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020871 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 EMSG2(_(e_invexpr2), *arg);
20873
20874 return len;
20875}
20876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020877/*
20878 * Find the end of a variable or function name, taking care of magic braces.
20879 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20880 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020881 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020882 * Return a pointer to just after the name. Equal to "arg" if there is no
20883 * valid name.
20884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020886find_name_end(
20887 char_u *arg,
20888 char_u **expr_start,
20889 char_u **expr_end,
20890 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020892 int mb_nest = 0;
20893 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020895 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020897 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020899 *expr_start = NULL;
20900 *expr_end = NULL;
20901 }
20902
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020903 /* Quick check for valid starting character. */
20904 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20905 return arg;
20906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020907 for (p = arg; *p != NUL
20908 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020909 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020910 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020911 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020912 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020913 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020914 if (*p == '\'')
20915 {
20916 /* skip over 'string' to avoid counting [ and ] inside it. */
20917 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20918 ;
20919 if (*p == NUL)
20920 break;
20921 }
20922 else if (*p == '"')
20923 {
20924 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20925 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20926 if (*p == '\\' && p[1] != NUL)
20927 ++p;
20928 if (*p == NUL)
20929 break;
20930 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020931 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20932 {
20933 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020934 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020935 len = (int)(p - arg);
20936 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020937 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020938 break;
20939 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020941 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020943 if (*p == '[')
20944 ++br_nest;
20945 else if (*p == ']')
20946 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020949 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020951 if (*p == '{')
20952 {
20953 mb_nest++;
20954 if (expr_start != NULL && *expr_start == NULL)
20955 *expr_start = p;
20956 }
20957 else if (*p == '}')
20958 {
20959 mb_nest--;
20960 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20961 *expr_end = p;
20962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 }
20965
20966 return p;
20967}
20968
20969/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020970 * Expands out the 'magic' {}'s in a variable/function name.
20971 * Note that this can call itself recursively, to deal with
20972 * constructs like foo{bar}{baz}{bam}
20973 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20974 * "in_start" ^
20975 * "expr_start" ^
20976 * "expr_end" ^
20977 * "in_end" ^
20978 *
20979 * Returns a new allocated string, which the caller must free.
20980 * Returns NULL for failure.
20981 */
20982 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020983make_expanded_name(
20984 char_u *in_start,
20985 char_u *expr_start,
20986 char_u *expr_end,
20987 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020988{
20989 char_u c1;
20990 char_u *retval = NULL;
20991 char_u *temp_result;
20992 char_u *nextcmd = NULL;
20993
20994 if (expr_end == NULL || in_end == NULL)
20995 return NULL;
20996 *expr_start = NUL;
20997 *expr_end = NUL;
20998 c1 = *in_end;
20999 *in_end = NUL;
21000
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021001 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021002 if (temp_result != NULL && nextcmd == NULL)
21003 {
21004 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21005 + (in_end - expr_end) + 1));
21006 if (retval != NULL)
21007 {
21008 STRCPY(retval, in_start);
21009 STRCAT(retval, temp_result);
21010 STRCAT(retval, expr_end + 1);
21011 }
21012 }
21013 vim_free(temp_result);
21014
21015 *in_end = c1; /* put char back for error messages */
21016 *expr_start = '{';
21017 *expr_end = '}';
21018
21019 if (retval != NULL)
21020 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021021 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021022 if (expr_start != NULL)
21023 {
21024 /* Further expansion! */
21025 temp_result = make_expanded_name(retval, expr_start,
21026 expr_end, temp_result);
21027 vim_free(retval);
21028 retval = temp_result;
21029 }
21030 }
21031
21032 return retval;
21033}
21034
21035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021037 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 */
21039 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021040eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021042 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21043}
21044
21045/*
21046 * Return TRUE if character "c" can be used as the first character in a
21047 * variable or function name (excluding '{' and '}').
21048 */
21049 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021050eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021051{
21052 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053}
21054
21055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056 * Set number v: variable to "val".
21057 */
21058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021059set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021061 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062}
21063
21064/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021065 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 */
21067 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021068get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021070 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071}
21072
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021073/*
21074 * Get string v: variable value. Uses a static buffer, can only be used once.
21075 */
21076 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021077get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021078{
21079 return get_tv_string(&vimvars[idx].vv_tv);
21080}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021081
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021083 * Get List v: variable value. Caller must take care of reference count when
21084 * needed.
21085 */
21086 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021087get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021088{
21089 return vimvars[idx].vv_list;
21090}
21091
21092/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021093 * Set v:char to character "c".
21094 */
21095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021096set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021097{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021098 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021099
21100#ifdef FEAT_MBYTE
21101 if (has_mbyte)
21102 buf[(*mb_char2bytes)(c, buf)] = NUL;
21103 else
21104#endif
21105 {
21106 buf[0] = c;
21107 buf[1] = NUL;
21108 }
21109 set_vim_var_string(VV_CHAR, buf, -1);
21110}
21111
21112/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021113 * Set v:count to "count" and v:count1 to "count1".
21114 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 */
21116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021117set_vcount(
21118 long count,
21119 long count1,
21120 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021122 if (set_prevcount)
21123 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021124 vimvars[VV_COUNT].vv_nr = count;
21125 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126}
21127
21128/*
21129 * Set string v: variable to a copy of "val".
21130 */
21131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021132set_vim_var_string(
21133 int idx,
21134 char_u *val,
21135 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136{
Bram Moolenaara542c682016-01-31 16:28:04 +010021137 clear_tv(&vimvars[idx].vv_di.di_tv);
21138 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021140 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021142 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021144 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145}
21146
21147/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021148 * Set List v: variable to "val".
21149 */
21150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021151set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021152{
Bram Moolenaara542c682016-01-31 16:28:04 +010021153 clear_tv(&vimvars[idx].vv_di.di_tv);
21154 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021155 vimvars[idx].vv_list = val;
21156 if (val != NULL)
21157 ++val->lv_refcount;
21158}
21159
21160/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021161 * Set Dictionary v: variable to "val".
21162 */
21163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021164set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021165{
21166 int todo;
21167 hashitem_T *hi;
21168
Bram Moolenaara542c682016-01-31 16:28:04 +010021169 clear_tv(&vimvars[idx].vv_di.di_tv);
21170 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021171 vimvars[idx].vv_dict = val;
21172 if (val != NULL)
21173 {
21174 ++val->dv_refcount;
21175
21176 /* Set readonly */
21177 todo = (int)val->dv_hashtab.ht_used;
21178 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21179 {
21180 if (HASHITEM_EMPTY(hi))
21181 continue;
21182 --todo;
21183 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21184 }
21185 }
21186}
21187
21188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 * Set v:register if needed.
21190 */
21191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021192set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193{
21194 char_u regname;
21195
21196 if (c == 0 || c == ' ')
21197 regname = '"';
21198 else
21199 regname = c;
21200 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021201 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 set_vim_var_string(VV_REG, &regname, 1);
21203}
21204
21205/*
21206 * Get or set v:exception. If "oldval" == NULL, return the current value.
21207 * Otherwise, restore the value to "oldval" and return NULL.
21208 * Must always be called in pairs to save and restore v:exception! Does not
21209 * take care of memory allocations.
21210 */
21211 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021212v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213{
21214 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021215 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216
Bram Moolenaare9a41262005-01-15 22:18:47 +000021217 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 return NULL;
21219}
21220
21221/*
21222 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21223 * Otherwise, restore the value to "oldval" and return NULL.
21224 * Must always be called in pairs to save and restore v:throwpoint! Does not
21225 * take care of memory allocations.
21226 */
21227 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021228v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229{
21230 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021231 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232
Bram Moolenaare9a41262005-01-15 22:18:47 +000021233 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 return NULL;
21235}
21236
21237#if defined(FEAT_AUTOCMD) || defined(PROTO)
21238/*
21239 * Set v:cmdarg.
21240 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21241 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21242 * Must always be called in pairs!
21243 */
21244 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021245set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246{
21247 char_u *oldval;
21248 char_u *newval;
21249 unsigned len;
21250
Bram Moolenaare9a41262005-01-15 22:18:47 +000021251 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021252 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021254 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021255 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021256 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 }
21258
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021259 if (eap->force_bin == FORCE_BIN)
21260 len = 6;
21261 else if (eap->force_bin == FORCE_NOBIN)
21262 len = 8;
21263 else
21264 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021265
21266 if (eap->read_edit)
21267 len += 7;
21268
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021269 if (eap->force_ff != 0)
21270 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21271# ifdef FEAT_MBYTE
21272 if (eap->force_enc != 0)
21273 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021274 if (eap->bad_char != 0)
21275 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021276# endif
21277
21278 newval = alloc(len + 1);
21279 if (newval == NULL)
21280 return NULL;
21281
21282 if (eap->force_bin == FORCE_BIN)
21283 sprintf((char *)newval, " ++bin");
21284 else if (eap->force_bin == FORCE_NOBIN)
21285 sprintf((char *)newval, " ++nobin");
21286 else
21287 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021288
21289 if (eap->read_edit)
21290 STRCAT(newval, " ++edit");
21291
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021292 if (eap->force_ff != 0)
21293 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21294 eap->cmd + eap->force_ff);
21295# ifdef FEAT_MBYTE
21296 if (eap->force_enc != 0)
21297 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21298 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021299 if (eap->bad_char == BAD_KEEP)
21300 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21301 else if (eap->bad_char == BAD_DROP)
21302 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21303 else if (eap->bad_char != 0)
21304 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021305# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021306 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021307 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308}
21309#endif
21310
21311/*
21312 * Get the value of internal variable "name".
21313 * Return OK or FAIL.
21314 */
21315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021316get_var_tv(
21317 char_u *name,
21318 int len, /* length of "name" */
21319 typval_T *rettv, /* NULL when only checking existence */
21320 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21321 int verbose, /* may give error message */
21322 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323{
21324 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021325 typval_T *tv = NULL;
21326 typval_T atv;
21327 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329
21330 /* truncate the name, so that we can use strcmp() */
21331 cc = name[len];
21332 name[len] = NUL;
21333
21334 /*
21335 * Check for "b:changedtick".
21336 */
21337 if (STRCMP(name, "b:changedtick") == 0)
21338 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021339 atv.v_type = VAR_NUMBER;
21340 atv.vval.v_number = curbuf->b_changedtick;
21341 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 }
21343
21344 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 * Check for user-defined variables.
21346 */
21347 else
21348 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021349 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021351 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021352 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021353 if (dip != NULL)
21354 *dip = v;
21355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 }
21357
Bram Moolenaare9a41262005-01-15 22:18:47 +000021358 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021360 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021361 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 ret = FAIL;
21363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021364 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021365 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366
21367 name[len] = cc;
21368
21369 return ret;
21370}
21371
21372/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021373 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21374 * Also handle function call with Funcref variable: func(expr)
21375 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21376 */
21377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021378handle_subscript(
21379 char_u **arg,
21380 typval_T *rettv,
21381 int evaluate, /* do more than finding the end */
21382 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021383{
21384 int ret = OK;
21385 dict_T *selfdict = NULL;
21386 char_u *s;
21387 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021388 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021389
21390 while (ret == OK
21391 && (**arg == '['
21392 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021393 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021394 && !vim_iswhite(*(*arg - 1)))
21395 {
21396 if (**arg == '(')
21397 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021398 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021399 if (evaluate)
21400 {
21401 functv = *rettv;
21402 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021403
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021404 /* Invoke the function. Recursive! */
21405 s = functv.vval.v_string;
21406 }
21407 else
21408 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021409 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021410 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21411 &len, evaluate, selfdict);
21412
21413 /* Clear the funcref afterwards, so that deleting it while
21414 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021415 if (evaluate)
21416 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021417
21418 /* Stop the expression evaluation when immediately aborting on
21419 * error, or when an interrupt occurred or an exception was thrown
21420 * but not caught. */
21421 if (aborting())
21422 {
21423 if (ret == OK)
21424 clear_tv(rettv);
21425 ret = FAIL;
21426 }
21427 dict_unref(selfdict);
21428 selfdict = NULL;
21429 }
21430 else /* **arg == '[' || **arg == '.' */
21431 {
21432 dict_unref(selfdict);
21433 if (rettv->v_type == VAR_DICT)
21434 {
21435 selfdict = rettv->vval.v_dict;
21436 if (selfdict != NULL)
21437 ++selfdict->dv_refcount;
21438 }
21439 else
21440 selfdict = NULL;
21441 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21442 {
21443 clear_tv(rettv);
21444 ret = FAIL;
21445 }
21446 }
21447 }
21448 dict_unref(selfdict);
21449 return ret;
21450}
21451
21452/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021453 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021454 * value).
21455 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021456 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021457alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021458{
Bram Moolenaar33570922005-01-25 22:26:29 +000021459 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021460}
21461
21462/*
21463 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 * The string "s" must have been allocated, it is consumed.
21465 * Return NULL for out of memory, the variable otherwise.
21466 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021467 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021468alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469{
Bram Moolenaar33570922005-01-25 22:26:29 +000021470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021472 rettv = alloc_tv();
21473 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021475 rettv->v_type = VAR_STRING;
21476 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 }
21478 else
21479 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021480 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481}
21482
21483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021484 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021487free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488{
21489 if (varp != NULL)
21490 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021491 switch (varp->v_type)
21492 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021493 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021494 func_unref(varp->vval.v_string);
21495 /*FALLTHROUGH*/
21496 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021497 vim_free(varp->vval.v_string);
21498 break;
21499 case VAR_LIST:
21500 list_unref(varp->vval.v_list);
21501 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 case VAR_DICT:
21503 dict_unref(varp->vval.v_dict);
21504 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021505 case VAR_JOB:
21506#ifdef FEAT_JOB
21507 job_unref(varp->vval.v_job);
21508 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021509#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021510 case VAR_CHANNEL:
21511#ifdef FEAT_CHANNEL
21512 channel_unref(varp->vval.v_channel);
21513 break;
21514#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021515 case VAR_NUMBER:
21516 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021517 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021518 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021519 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521 vim_free(varp);
21522 }
21523}
21524
21525/*
21526 * Free the memory for a variable value and set the value to NULL or 0.
21527 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021528 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021529clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530{
21531 if (varp != NULL)
21532 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021533 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021535 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021536 func_unref(varp->vval.v_string);
21537 /*FALLTHROUGH*/
21538 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021539 vim_free(varp->vval.v_string);
21540 varp->vval.v_string = NULL;
21541 break;
21542 case VAR_LIST:
21543 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021544 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021545 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021546 case VAR_DICT:
21547 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021548 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021549 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021550 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021551 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021552 varp->vval.v_number = 0;
21553 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021554 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021555#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021556 varp->vval.v_float = 0.0;
21557 break;
21558#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021559 case VAR_JOB:
21560#ifdef FEAT_JOB
21561 job_unref(varp->vval.v_job);
21562 varp->vval.v_job = NULL;
21563#endif
21564 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021565 case VAR_CHANNEL:
21566#ifdef FEAT_CHANNEL
21567 channel_unref(varp->vval.v_channel);
21568 varp->vval.v_channel = NULL;
21569#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021570 case VAR_UNKNOWN:
21571 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021573 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 }
21575}
21576
21577/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021578 * Set the value of a variable to NULL without freeing items.
21579 */
21580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021581init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021582{
21583 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021584 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021585}
21586
21587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 * Get the number value of a variable.
21589 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021590 * For incompatible types, return 0.
21591 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21592 * caller of incompatible types: it sets *denote to TRUE if "denote"
21593 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 */
21595 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021596get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021598 int error = FALSE;
21599
21600 return get_tv_number_chk(varp, &error); /* return 0L on error */
21601}
21602
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021603 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021604get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021605{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021606 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021608 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021610 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021611 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021612 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021613#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021614 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021615 break;
21616#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021617 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021618 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021619 break;
21620 case VAR_STRING:
21621 if (varp->vval.v_string != NULL)
21622 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021623 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021624 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021625 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021626 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021627 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021628 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021629 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021630 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021631 case VAR_SPECIAL:
21632 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21633 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021634 case VAR_JOB:
21635#ifdef FEAT_JOB
21636 EMSG(_("E910: Using a Job as a Number"));
21637 break;
21638#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021639 case VAR_CHANNEL:
21640#ifdef FEAT_CHANNEL
21641 EMSG(_("E913: Using a Channel as a Number"));
21642 break;
21643#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021644 case VAR_UNKNOWN:
21645 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021646 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021648 if (denote == NULL) /* useful for values that must be unsigned */
21649 n = -1;
21650 else
21651 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021652 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653}
21654
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021655#ifdef FEAT_FLOAT
21656 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021657get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021658{
21659 switch (varp->v_type)
21660 {
21661 case VAR_NUMBER:
21662 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021663 case VAR_FLOAT:
21664 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021665 case VAR_FUNC:
21666 EMSG(_("E891: Using a Funcref as a Float"));
21667 break;
21668 case VAR_STRING:
21669 EMSG(_("E892: Using a String as a Float"));
21670 break;
21671 case VAR_LIST:
21672 EMSG(_("E893: Using a List as a Float"));
21673 break;
21674 case VAR_DICT:
21675 EMSG(_("E894: Using a Dictionary as a Float"));
21676 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021677 case VAR_SPECIAL:
21678 EMSG(_("E907: Using a special value as a Float"));
21679 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021680 case VAR_JOB:
21681# ifdef FEAT_JOB
21682 EMSG(_("E911: Using a Job as a Float"));
21683 break;
21684# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021685 case VAR_CHANNEL:
21686# ifdef FEAT_CHANNEL
21687 EMSG(_("E914: Using a Channel as a Float"));
21688 break;
21689# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021690 case VAR_UNKNOWN:
21691 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021692 break;
21693 }
21694 return 0;
21695}
21696#endif
21697
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021699 * Get the lnum from the first argument.
21700 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021701 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 */
21703 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021704get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705{
Bram Moolenaar33570922005-01-25 22:26:29 +000021706 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 linenr_T lnum;
21708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021709 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 if (lnum == 0) /* no valid number, try using line() */
21711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021712 rettv.v_type = VAR_NUMBER;
21713 f_line(argvars, &rettv);
21714 lnum = rettv.vval.v_number;
21715 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 }
21717 return lnum;
21718}
21719
21720/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021721 * Get the lnum from the first argument.
21722 * Also accepts "$", then "buf" is used.
21723 * Returns 0 on error.
21724 */
21725 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021726get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021727{
21728 if (argvars[0].v_type == VAR_STRING
21729 && argvars[0].vval.v_string != NULL
21730 && argvars[0].vval.v_string[0] == '$'
21731 && buf != NULL)
21732 return buf->b_ml.ml_line_count;
21733 return get_tv_number_chk(&argvars[0], NULL);
21734}
21735
21736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 * Get the string value of a variable.
21738 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021739 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21740 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 * If the String variable has never been set, return an empty string.
21742 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021743 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21744 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 */
21746 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021747get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748{
21749 static char_u mybuf[NUMBUFLEN];
21750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021751 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752}
21753
21754 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021755get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021757 char_u *res = get_tv_string_buf_chk(varp, buf);
21758
21759 return res != NULL ? res : (char_u *)"";
21760}
21761
Bram Moolenaar7d647822014-04-05 21:28:56 +020021762/*
21763 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21764 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021765 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021766get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021767{
21768 static char_u mybuf[NUMBUFLEN];
21769
21770 return get_tv_string_buf_chk(varp, mybuf);
21771}
21772
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021773 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021774get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021775{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021776 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021778 case VAR_NUMBER:
21779 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21780 return buf;
21781 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021782 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021783 break;
21784 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021785 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021786 break;
21787 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021788 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021789 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021790 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021791#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021792 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021793 break;
21794#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021795 case VAR_STRING:
21796 if (varp->vval.v_string != NULL)
21797 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021798 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021799 case VAR_SPECIAL:
21800 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21801 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021802 case VAR_JOB:
21803#ifdef FEAT_JOB
21804 {
21805 job_T *job = varp->vval.v_job;
21806 char *status = job->jv_status == JOB_FAILED ? "fail"
21807 : job->jv_status == JOB_ENDED ? "dead"
21808 : "run";
21809# ifdef UNIX
21810 vim_snprintf((char *)buf, NUMBUFLEN,
21811 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021812# elif defined(WIN32)
21813 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021814 "process %ld %s",
21815 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021816 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021817# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021818 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021819 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21820# endif
21821 return buf;
21822 }
21823#endif
21824 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021825 case VAR_CHANNEL:
21826#ifdef FEAT_CHANNEL
21827 {
21828 channel_T *channel = varp->vval.v_channel;
21829 char *status = channel_status(channel);
21830
21831 vim_snprintf((char *)buf, NUMBUFLEN,
21832 "channel %d %s", channel->ch_id, status);
21833 return buf;
21834 }
21835#endif
21836 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021837 case VAR_UNKNOWN:
21838 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021839 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021841 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842}
21843
21844/*
21845 * Find variable "name" in the list of variables.
21846 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021847 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021848 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021849 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021851 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021852find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021855 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856
Bram Moolenaara7043832005-01-21 11:56:39 +000021857 ht = find_var_ht(name, &varname);
21858 if (htp != NULL)
21859 *htp = ht;
21860 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021862 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863}
21864
21865/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021866 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021867 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021869 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021870find_var_in_ht(
21871 hashtab_T *ht,
21872 int htname,
21873 char_u *varname,
21874 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021875{
Bram Moolenaar33570922005-01-25 22:26:29 +000021876 hashitem_T *hi;
21877
21878 if (*varname == NUL)
21879 {
21880 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021881 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021883 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021884 case 'g': return &globvars_var;
21885 case 'v': return &vimvars_var;
21886 case 'b': return &curbuf->b_bufvar;
21887 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021888#ifdef FEAT_WINDOWS
21889 case 't': return &curtab->tp_winvar;
21890#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021891 case 'l': return current_funccal == NULL
21892 ? NULL : &current_funccal->l_vars_var;
21893 case 'a': return current_funccal == NULL
21894 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 }
21896 return NULL;
21897 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021898
21899 hi = hash_find(ht, varname);
21900 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021901 {
21902 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021903 * worked find the variable again. Don't auto-load a script if it was
21904 * loaded already, otherwise it would be loaded every time when
21905 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021906 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021907 {
21908 /* Note: script_autoload() may make "hi" invalid. It must either
21909 * be obtained again or not used. */
21910 if (!script_autoload(varname, FALSE) || aborting())
21911 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021912 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021913 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021914 if (HASHITEM_EMPTY(hi))
21915 return NULL;
21916 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021917 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021918}
21919
21920/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021921 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021922 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021923 * Set "varname" to the start of name without ':'.
21924 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021925 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021926find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021928 hashitem_T *hi;
21929
Bram Moolenaar73627d02015-08-11 15:46:09 +020021930 if (name[0] == NUL)
21931 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932 if (name[1] != ':')
21933 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021934 /* The name must not start with a colon or #. */
21935 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 return NULL;
21937 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021938
21939 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021940 hi = hash_find(&compat_hashtab, name);
21941 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021942 return &compat_hashtab;
21943
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021945 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021946 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 }
21948 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021949 if (*name == 'g') /* global variable */
21950 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021951 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21952 */
21953 if (vim_strchr(name + 2, ':') != NULL
21954 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021955 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021957 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021959 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021960#ifdef FEAT_WINDOWS
21961 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021962 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021963#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021964 if (*name == 'v') /* v: variable */
21965 return &vimvarht;
21966 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021967 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021968 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021969 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 if (*name == 's' /* script variable */
21971 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21972 return &SCRIPT_VARS(current_SID);
21973 return NULL;
21974}
21975
21976/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021977 * Get function call environment based on bactrace debug level
21978 */
21979 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021980get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021981{
21982 int i;
21983 funccall_T *funccal;
21984 funccall_T *temp_funccal;
21985
21986 funccal = current_funccal;
21987 if (debug_backtrace_level > 0)
21988 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021989 for (i = 0; i < debug_backtrace_level; i++)
21990 {
21991 temp_funccal = funccal->caller;
21992 if (temp_funccal)
21993 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021994 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021995 /* backtrace level overflow. reset to max */
21996 debug_backtrace_level = i;
21997 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021998 }
21999 return funccal;
22000}
22001
22002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022004 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 * Returns NULL when it doesn't exist.
22006 */
22007 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022008get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009{
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022012 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 if (v == NULL)
22014 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022015 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016}
22017
22018/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 * sourcing this script and when executing functions defined in the script.
22021 */
22022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022023new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024{
Bram Moolenaara7043832005-01-21 11:56:39 +000022025 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022026 hashtab_T *ht;
22027 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022028
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22030 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022031 /* Re-allocating ga_data means that an ht_array pointing to
22032 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022033 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022034 for (i = 1; i <= ga_scripts.ga_len; ++i)
22035 {
22036 ht = &SCRIPT_VARS(i);
22037 if (ht->ht_mask == HT_INIT_SIZE - 1)
22038 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022039 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022040 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022041 }
22042
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 while (ga_scripts.ga_len < id)
22044 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022045 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022046 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022047 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 }
22050 }
22051}
22052
22053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022054 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22055 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 */
22057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022058init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059{
Bram Moolenaar33570922005-01-25 22:26:29 +000022060 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022061 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022062 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022063 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022064 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022065 dict_var->di_tv.vval.v_dict = dict;
22066 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022067 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022068 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22069 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070}
22071
22072/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022073 * Unreference a dictionary initialized by init_var_dict().
22074 */
22075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022076unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022077{
22078 /* Now the dict needs to be freed if no one else is using it, go back to
22079 * normal reference counting. */
22080 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22081 dict_unref(dict);
22082}
22083
22084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022086 * Frees all allocated variables and the value they contain.
22087 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 */
22089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022090vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022091{
22092 vars_clear_ext(ht, TRUE);
22093}
22094
22095/*
22096 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22097 */
22098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022099vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100{
Bram Moolenaara7043832005-01-21 11:56:39 +000022101 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022102 hashitem_T *hi;
22103 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104
Bram Moolenaar33570922005-01-25 22:26:29 +000022105 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022106 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022107 for (hi = ht->ht_array; todo > 0; ++hi)
22108 {
22109 if (!HASHITEM_EMPTY(hi))
22110 {
22111 --todo;
22112
Bram Moolenaar33570922005-01-25 22:26:29 +000022113 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022114 * ht_array might change then. hash_clear() takes care of it
22115 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022116 v = HI2DI(hi);
22117 if (free_val)
22118 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022119 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022120 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022121 }
22122 }
22123 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022124 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125}
22126
Bram Moolenaara7043832005-01-21 11:56:39 +000022127/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022128 * Delete a variable from hashtab "ht" at item "hi".
22129 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022132delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133{
Bram Moolenaar33570922005-01-25 22:26:29 +000022134 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022135
22136 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022137 clear_tv(&di->di_tv);
22138 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139}
22140
22141/*
22142 * List the value of one internal variable.
22143 */
22144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022145list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022147 char_u *tofree;
22148 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022149 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022150
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022151 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022152 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022153 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022154 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155}
22156
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022158list_one_var_a(
22159 char_u *prefix,
22160 char_u *name,
22161 int type,
22162 char_u *string,
22163 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164{
Bram Moolenaar31859182007-08-14 20:41:13 +000022165 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22166 msg_start();
22167 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 if (name != NULL) /* "a:" vars don't have a name stored */
22169 msg_puts(name);
22170 msg_putchar(' ');
22171 msg_advance(22);
22172 if (type == VAR_NUMBER)
22173 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022174 else if (type == VAR_FUNC)
22175 msg_putchar('*');
22176 else if (type == VAR_LIST)
22177 {
22178 msg_putchar('[');
22179 if (*string == '[')
22180 ++string;
22181 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022182 else if (type == VAR_DICT)
22183 {
22184 msg_putchar('{');
22185 if (*string == '{')
22186 ++string;
22187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 else
22189 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022190
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022192
22193 if (type == VAR_FUNC)
22194 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022195 if (*first)
22196 {
22197 msg_clr_eos();
22198 *first = FALSE;
22199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200}
22201
22202/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022203 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 * If the variable already exists, the value is updated.
22205 * Otherwise the variable is created.
22206 */
22207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022208set_var(
22209 char_u *name,
22210 typval_T *tv,
22211 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212{
Bram Moolenaar33570922005-01-25 22:26:29 +000022213 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022215 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022217 ht = find_var_ht(name, &varname);
22218 if (ht == NULL || *varname == NUL)
22219 {
22220 EMSG2(_(e_illvar), name);
22221 return;
22222 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022223 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022224
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022225 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22226 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022227
Bram Moolenaar33570922005-01-25 22:26:29 +000022228 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022230 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022231 if (var_check_ro(v->di_flags, name, FALSE)
22232 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022233 return;
22234 if (v->di_tv.v_type != tv->v_type
22235 && !((v->di_tv.v_type == VAR_STRING
22236 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022237 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022238 || tv->v_type == VAR_NUMBER))
22239#ifdef FEAT_FLOAT
22240 && !((v->di_tv.v_type == VAR_NUMBER
22241 || v->di_tv.v_type == VAR_FLOAT)
22242 && (tv->v_type == VAR_NUMBER
22243 || tv->v_type == VAR_FLOAT))
22244#endif
22245 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022246 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022247 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022248 return;
22249 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022250
22251 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022252 * Handle setting internal v: variables separately where needed to
22253 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022254 */
22255 if (ht == &vimvarht)
22256 {
22257 if (v->di_tv.v_type == VAR_STRING)
22258 {
22259 vim_free(v->di_tv.vval.v_string);
22260 if (copy || tv->v_type != VAR_STRING)
22261 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22262 else
22263 {
22264 /* Take over the string to avoid an extra alloc/free. */
22265 v->di_tv.vval.v_string = tv->vval.v_string;
22266 tv->vval.v_string = NULL;
22267 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022268 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022269 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022270 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022271 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022272 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022273 if (STRCMP(varname, "searchforward") == 0)
22274 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022275#ifdef FEAT_SEARCH_EXTRA
22276 else if (STRCMP(varname, "hlsearch") == 0)
22277 {
22278 no_hlsearch = !v->di_tv.vval.v_number;
22279 redraw_all_later(SOME_VALID);
22280 }
22281#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022282 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022283 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022284 else if (v->di_tv.v_type != tv->v_type)
22285 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022286 }
22287
22288 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 }
22290 else /* add a new variable */
22291 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022292 /* Can't add "v:" variable. */
22293 if (ht == &vimvarht)
22294 {
22295 EMSG2(_(e_illvar), name);
22296 return;
22297 }
22298
Bram Moolenaar92124a32005-06-17 22:03:40 +000022299 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022300 if (!valid_varname(varname))
22301 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022302
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022303 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22304 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022305 if (v == NULL)
22306 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022307 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022310 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311 return;
22312 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022313 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022315
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022316 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022317 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022318 else
22319 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022320 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022321 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022322 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324}
22325
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022326/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022327 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022328 * Also give an error message.
22329 */
22330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022331var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022332{
22333 if (flags & DI_FLAGS_RO)
22334 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022335 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022336 return TRUE;
22337 }
22338 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22339 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022340 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022341 return TRUE;
22342 }
22343 return FALSE;
22344}
22345
22346/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022347 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22348 * Also give an error message.
22349 */
22350 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022351var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022352{
22353 if (flags & DI_FLAGS_FIX)
22354 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022355 EMSG2(_("E795: Cannot delete variable %s"),
22356 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022357 return TRUE;
22358 }
22359 return FALSE;
22360}
22361
22362/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022363 * Check if a funcref is assigned to a valid variable name.
22364 * Return TRUE and give an error if not.
22365 */
22366 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022367var_check_func_name(
22368 char_u *name, /* points to start of variable name */
22369 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022370{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022371 /* Allow for w: b: s: and t:. */
22372 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022373 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22374 ? name[2] : name[0]))
22375 {
22376 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22377 name);
22378 return TRUE;
22379 }
22380 /* Don't allow hiding a function. When "v" is not NULL we might be
22381 * assigning another function to the same var, the type is checked
22382 * below. */
22383 if (new_var && function_exists(name))
22384 {
22385 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22386 name);
22387 return TRUE;
22388 }
22389 return FALSE;
22390}
22391
22392/*
22393 * Check if a variable name is valid.
22394 * Return FALSE and give an error if not.
22395 */
22396 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022397valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022398{
22399 char_u *p;
22400
22401 for (p = varname; *p != NUL; ++p)
22402 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22403 && *p != AUTOLOAD_CHAR)
22404 {
22405 EMSG2(_(e_illvar), varname);
22406 return FALSE;
22407 }
22408 return TRUE;
22409}
22410
22411/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022412 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022413 * Also give an error message, using "name" or _("name") when use_gettext is
22414 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022415 */
22416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022417tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022418{
22419 if (lock & VAR_LOCKED)
22420 {
22421 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022422 name == NULL ? (char_u *)_("Unknown")
22423 : use_gettext ? (char_u *)_(name)
22424 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022425 return TRUE;
22426 }
22427 if (lock & VAR_FIXED)
22428 {
22429 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022430 name == NULL ? (char_u *)_("Unknown")
22431 : use_gettext ? (char_u *)_(name)
22432 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022433 return TRUE;
22434 }
22435 return FALSE;
22436}
22437
22438/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022439 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022440 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022441 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022442 * It is OK for "from" and "to" to point to the same item. This is used to
22443 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022444 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022446copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022448 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022449 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022450 switch (from->v_type)
22451 {
22452 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022453 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022454 to->vval.v_number = from->vval.v_number;
22455 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022456 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022457#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022458 to->vval.v_float = from->vval.v_float;
22459 break;
22460#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022461 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022462#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022463 to->vval.v_job = from->vval.v_job;
22464 ++to->vval.v_job->jv_refcount;
22465 break;
22466#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022467 case VAR_CHANNEL:
22468#ifdef FEAT_CHANNEL
22469 to->vval.v_channel = from->vval.v_channel;
22470 ++to->vval.v_channel->ch_refcount;
22471 break;
22472#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022473 case VAR_STRING:
22474 case VAR_FUNC:
22475 if (from->vval.v_string == NULL)
22476 to->vval.v_string = NULL;
22477 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022478 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022479 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022480 if (from->v_type == VAR_FUNC)
22481 func_ref(to->vval.v_string);
22482 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022483 break;
22484 case VAR_LIST:
22485 if (from->vval.v_list == NULL)
22486 to->vval.v_list = NULL;
22487 else
22488 {
22489 to->vval.v_list = from->vval.v_list;
22490 ++to->vval.v_list->lv_refcount;
22491 }
22492 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022493 case VAR_DICT:
22494 if (from->vval.v_dict == NULL)
22495 to->vval.v_dict = NULL;
22496 else
22497 {
22498 to->vval.v_dict = from->vval.v_dict;
22499 ++to->vval.v_dict->dv_refcount;
22500 }
22501 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022502 case VAR_UNKNOWN:
22503 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022504 break;
22505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506}
22507
22508/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022509 * Make a copy of an item.
22510 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022511 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22512 * reference to an already copied list/dict can be used.
22513 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022514 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022516item_copy(
22517 typval_T *from,
22518 typval_T *to,
22519 int deep,
22520 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022521{
22522 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022523 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022524
Bram Moolenaar33570922005-01-25 22:26:29 +000022525 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022526 {
22527 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022528 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022529 }
22530 ++recurse;
22531
22532 switch (from->v_type)
22533 {
22534 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022535 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022536 case VAR_STRING:
22537 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022538 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022539 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022540 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022541 copy_tv(from, to);
22542 break;
22543 case VAR_LIST:
22544 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022545 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022546 if (from->vval.v_list == NULL)
22547 to->vval.v_list = NULL;
22548 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22549 {
22550 /* use the copy made earlier */
22551 to->vval.v_list = from->vval.v_list->lv_copylist;
22552 ++to->vval.v_list->lv_refcount;
22553 }
22554 else
22555 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22556 if (to->vval.v_list == NULL)
22557 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022558 break;
22559 case VAR_DICT:
22560 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022561 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022562 if (from->vval.v_dict == NULL)
22563 to->vval.v_dict = NULL;
22564 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22565 {
22566 /* use the copy made earlier */
22567 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22568 ++to->vval.v_dict->dv_refcount;
22569 }
22570 else
22571 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22572 if (to->vval.v_dict == NULL)
22573 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022574 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022575 case VAR_UNKNOWN:
22576 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022577 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022578 }
22579 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022580 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022581}
22582
22583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 * ":echo expr1 ..." print each argument separated with a space, add a
22585 * newline at the end.
22586 * ":echon expr1 ..." print each argument plain.
22587 */
22588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022589ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590{
22591 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022593 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 char_u *p;
22595 int needclr = TRUE;
22596 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022597 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598
22599 if (eap->skip)
22600 ++emsg_skip;
22601 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22602 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022603 /* If eval1() causes an error message the text from the command may
22604 * still need to be cleared. E.g., "echo 22,44". */
22605 need_clr_eos = needclr;
22606
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022608 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 {
22610 /*
22611 * Report the invalid expression unless the expression evaluation
22612 * has been cancelled due to an aborting error, an interrupt, or an
22613 * exception.
22614 */
22615 if (!aborting())
22616 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022617 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 break;
22619 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022620 need_clr_eos = FALSE;
22621
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 if (!eap->skip)
22623 {
22624 if (atstart)
22625 {
22626 atstart = FALSE;
22627 /* Call msg_start() after eval1(), evaluating the expression
22628 * may cause a message to appear. */
22629 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022630 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022631 /* Mark the saved text as finishing the line, so that what
22632 * follows is displayed on a new line when scrolling back
22633 * at the more prompt. */
22634 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 }
22638 else if (eap->cmdidx == CMD_echo)
22639 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022640 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022641 if (p != NULL)
22642 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022644 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022646 if (*p != TAB && needclr)
22647 {
22648 /* remove any text still there from the command */
22649 msg_clr_eos();
22650 needclr = FALSE;
22651 }
22652 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 }
22654 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022655 {
22656#ifdef FEAT_MBYTE
22657 if (has_mbyte)
22658 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022659 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022660
22661 (void)msg_outtrans_len_attr(p, i, echo_attr);
22662 p += i - 1;
22663 }
22664 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022666 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022669 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022671 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 arg = skipwhite(arg);
22673 }
22674 eap->nextcmd = check_nextcmd(arg);
22675
22676 if (eap->skip)
22677 --emsg_skip;
22678 else
22679 {
22680 /* remove text that may still be there from the command */
22681 if (needclr)
22682 msg_clr_eos();
22683 if (eap->cmdidx == CMD_echo)
22684 msg_end();
22685 }
22686}
22687
22688/*
22689 * ":echohl {name}".
22690 */
22691 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022692ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693{
22694 int id;
22695
22696 id = syn_name2id(eap->arg);
22697 if (id == 0)
22698 echo_attr = 0;
22699 else
22700 echo_attr = syn_id2attr(id);
22701}
22702
22703/*
22704 * ":execute expr1 ..." execute the result of an expression.
22705 * ":echomsg expr1 ..." Print a message
22706 * ":echoerr expr1 ..." Print an error
22707 * Each gets spaces around each argument and a newline at the end for
22708 * echo commands
22709 */
22710 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022711ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712{
22713 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022714 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 int ret = OK;
22716 char_u *p;
22717 garray_T ga;
22718 int len;
22719 int save_did_emsg;
22720
22721 ga_init2(&ga, 1, 80);
22722
22723 if (eap->skip)
22724 ++emsg_skip;
22725 while (*arg != NUL && *arg != '|' && *arg != '\n')
22726 {
22727 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022728 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 {
22730 /*
22731 * Report the invalid expression unless the expression evaluation
22732 * has been cancelled due to an aborting error, an interrupt, or an
22733 * exception.
22734 */
22735 if (!aborting())
22736 EMSG2(_(e_invexpr2), p);
22737 ret = FAIL;
22738 break;
22739 }
22740
22741 if (!eap->skip)
22742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022743 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 len = (int)STRLEN(p);
22745 if (ga_grow(&ga, len + 2) == FAIL)
22746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022747 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 ret = FAIL;
22749 break;
22750 }
22751 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 ga.ga_len += len;
22755 }
22756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022757 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 arg = skipwhite(arg);
22759 }
22760
22761 if (ret != FAIL && ga.ga_data != NULL)
22762 {
22763 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022766 out_flush();
22767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 else if (eap->cmdidx == CMD_echoerr)
22769 {
22770 /* We don't want to abort following commands, restore did_emsg. */
22771 save_did_emsg = did_emsg;
22772 EMSG((char_u *)ga.ga_data);
22773 if (!force_abort)
22774 did_emsg = save_did_emsg;
22775 }
22776 else if (eap->cmdidx == CMD_execute)
22777 do_cmdline((char_u *)ga.ga_data,
22778 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22779 }
22780
22781 ga_clear(&ga);
22782
22783 if (eap->skip)
22784 --emsg_skip;
22785
22786 eap->nextcmd = check_nextcmd(arg);
22787}
22788
22789/*
22790 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22791 * "arg" points to the "&" or '+' when called, to "option" when returning.
22792 * Returns NULL when no option name found. Otherwise pointer to the char
22793 * after the option name.
22794 */
22795 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022796find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797{
22798 char_u *p = *arg;
22799
22800 ++p;
22801 if (*p == 'g' && p[1] == ':')
22802 {
22803 *opt_flags = OPT_GLOBAL;
22804 p += 2;
22805 }
22806 else if (*p == 'l' && p[1] == ':')
22807 {
22808 *opt_flags = OPT_LOCAL;
22809 p += 2;
22810 }
22811 else
22812 *opt_flags = 0;
22813
22814 if (!ASCII_ISALPHA(*p))
22815 return NULL;
22816 *arg = p;
22817
22818 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22819 p += 4; /* termcap option */
22820 else
22821 while (ASCII_ISALPHA(*p))
22822 ++p;
22823 return p;
22824}
22825
22826/*
22827 * ":function"
22828 */
22829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022830ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831{
22832 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022833 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 int j;
22835 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022837 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 char_u *name = NULL;
22839 char_u *p;
22840 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022841 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 garray_T newargs;
22843 garray_T newlines;
22844 int varargs = FALSE;
22845 int mustend = FALSE;
22846 int flags = 0;
22847 ufunc_T *fp;
22848 int indent;
22849 int nesting;
22850 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022851 dictitem_T *v;
22852 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022853 static int func_nr = 0; /* number for nameless function */
22854 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022855 hashtab_T *ht;
22856 int todo;
22857 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022858 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859
22860 /*
22861 * ":function" without argument: list functions.
22862 */
22863 if (ends_excmd(*eap->arg))
22864 {
22865 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022866 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022867 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022868 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022869 {
22870 if (!HASHITEM_EMPTY(hi))
22871 {
22872 --todo;
22873 fp = HI2UF(hi);
22874 if (!isdigit(*fp->uf_name))
22875 list_func_head(fp, FALSE);
22876 }
22877 }
22878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 eap->nextcmd = check_nextcmd(eap->arg);
22880 return;
22881 }
22882
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022883 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022884 * ":function /pat": list functions matching pattern.
22885 */
22886 if (*eap->arg == '/')
22887 {
22888 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22889 if (!eap->skip)
22890 {
22891 regmatch_T regmatch;
22892
22893 c = *p;
22894 *p = NUL;
22895 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22896 *p = c;
22897 if (regmatch.regprog != NULL)
22898 {
22899 regmatch.rm_ic = p_ic;
22900
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022901 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022902 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22903 {
22904 if (!HASHITEM_EMPTY(hi))
22905 {
22906 --todo;
22907 fp = HI2UF(hi);
22908 if (!isdigit(*fp->uf_name)
22909 && vim_regexec(&regmatch, fp->uf_name, 0))
22910 list_func_head(fp, FALSE);
22911 }
22912 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022913 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022914 }
22915 }
22916 if (*p == '/')
22917 ++p;
22918 eap->nextcmd = check_nextcmd(p);
22919 return;
22920 }
22921
22922 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022923 * Get the function name. There are these situations:
22924 * func normal function name
22925 * "name" == func, "fudi.fd_dict" == NULL
22926 * dict.func new dictionary entry
22927 * "name" == NULL, "fudi.fd_dict" set,
22928 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22929 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022930 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022931 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22932 * dict.func existing dict entry that's not a Funcref
22933 * "name" == NULL, "fudi.fd_dict" set,
22934 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022935 * s:func script-local function name
22936 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022939 name = trans_function_name(&p, eap->skip, 0, &fudi);
22940 paren = (vim_strchr(p, '(') != NULL);
22941 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 {
22943 /*
22944 * Return on an invalid expression in braces, unless the expression
22945 * evaluation has been cancelled due to an aborting error, an
22946 * interrupt, or an exception.
22947 */
22948 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022950 if (!eap->skip && fudi.fd_newkey != NULL)
22951 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022952 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 else
22956 eap->skip = TRUE;
22957 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022958
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 /* An error in a function call during evaluation of an expression in magic
22960 * braces should not cause the function not to be defined. */
22961 saved_did_emsg = did_emsg;
22962 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963
22964 /*
22965 * ":function func" with only function name: list function.
22966 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022967 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 {
22969 if (!ends_excmd(*skipwhite(p)))
22970 {
22971 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022972 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 }
22974 eap->nextcmd = check_nextcmd(p);
22975 if (eap->nextcmd != NULL)
22976 *p = NUL;
22977 if (!eap->skip && !got_int)
22978 {
22979 fp = find_func(name);
22980 if (fp != NULL)
22981 {
22982 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022983 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022985 if (FUNCLINE(fp, j) == NULL)
22986 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 msg_putchar('\n');
22988 msg_outnum((long)(j + 1));
22989 if (j < 9)
22990 msg_putchar(' ');
22991 if (j < 99)
22992 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022993 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 out_flush(); /* show a line at a time */
22995 ui_breakcheck();
22996 }
22997 if (!got_int)
22998 {
22999 msg_putchar('\n');
23000 msg_puts((char_u *)" endfunction");
23001 }
23002 }
23003 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023004 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023006 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 }
23008
23009 /*
23010 * ":function name(arg1, arg2)" Define function.
23011 */
23012 p = skipwhite(p);
23013 if (*p != '(')
23014 {
23015 if (!eap->skip)
23016 {
23017 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023018 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 }
23020 /* attempt to continue by skipping some text */
23021 if (vim_strchr(p, '(') != NULL)
23022 p = vim_strchr(p, '(');
23023 }
23024 p = skipwhite(p + 1);
23025
23026 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23027 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23028
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023029 if (!eap->skip)
23030 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023031 /* Check the name of the function. Unless it's a dictionary function
23032 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023033 if (name != NULL)
23034 arg = name;
23035 else
23036 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023037 if (arg != NULL && (fudi.fd_di == NULL
23038 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023039 {
23040 if (*arg == K_SPECIAL)
23041 j = 3;
23042 else
23043 j = 0;
23044 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23045 : eval_isnamec(arg[j])))
23046 ++j;
23047 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023048 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023049 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023050 /* Disallow using the g: dict. */
23051 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23052 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023053 }
23054
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 /*
23056 * Isolate the arguments: "arg1, arg2, ...)"
23057 */
23058 while (*p != ')')
23059 {
23060 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23061 {
23062 varargs = TRUE;
23063 p += 3;
23064 mustend = TRUE;
23065 }
23066 else
23067 {
23068 arg = p;
23069 while (ASCII_ISALNUM(*p) || *p == '_')
23070 ++p;
23071 if (arg == p || isdigit(*arg)
23072 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23073 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23074 {
23075 if (!eap->skip)
23076 EMSG2(_("E125: Illegal argument: %s"), arg);
23077 break;
23078 }
23079 if (ga_grow(&newargs, 1) == FAIL)
23080 goto erret;
23081 c = *p;
23082 *p = NUL;
23083 arg = vim_strsave(arg);
23084 if (arg == NULL)
23085 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023086
23087 /* Check for duplicate argument name. */
23088 for (i = 0; i < newargs.ga_len; ++i)
23089 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23090 {
23091 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023092 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023093 goto erret;
23094 }
23095
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23097 *p = c;
23098 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 if (*p == ',')
23100 ++p;
23101 else
23102 mustend = TRUE;
23103 }
23104 p = skipwhite(p);
23105 if (mustend && *p != ')')
23106 {
23107 if (!eap->skip)
23108 EMSG2(_(e_invarg2), eap->arg);
23109 break;
23110 }
23111 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023112 if (*p != ')')
23113 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 ++p; /* skip the ')' */
23115
Bram Moolenaare9a41262005-01-15 22:18:47 +000023116 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 for (;;)
23118 {
23119 p = skipwhite(p);
23120 if (STRNCMP(p, "range", 5) == 0)
23121 {
23122 flags |= FC_RANGE;
23123 p += 5;
23124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023125 else if (STRNCMP(p, "dict", 4) == 0)
23126 {
23127 flags |= FC_DICT;
23128 p += 4;
23129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130 else if (STRNCMP(p, "abort", 5) == 0)
23131 {
23132 flags |= FC_ABORT;
23133 p += 5;
23134 }
23135 else
23136 break;
23137 }
23138
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023139 /* When there is a line break use what follows for the function body.
23140 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23141 if (*p == '\n')
23142 line_arg = p + 1;
23143 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 EMSG(_(e_trailing));
23145
23146 /*
23147 * Read the body of the function, until ":endfunction" is found.
23148 */
23149 if (KeyTyped)
23150 {
23151 /* Check if the function already exists, don't let the user type the
23152 * whole function before telling him it doesn't work! For a script we
23153 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023154 if (!eap->skip && !eap->forceit)
23155 {
23156 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23157 EMSG(_(e_funcdict));
23158 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023159 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023162 if (!eap->skip && did_emsg)
23163 goto erret;
23164
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 msg_putchar('\n'); /* don't overwrite the function name */
23166 cmdline_row = msg_row;
23167 }
23168
23169 indent = 2;
23170 nesting = 0;
23171 for (;;)
23172 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023173 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023174 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023175 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023176 saved_wait_return = FALSE;
23177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023179 sourcing_lnum_off = sourcing_lnum;
23180
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023181 if (line_arg != NULL)
23182 {
23183 /* Use eap->arg, split up in parts by line breaks. */
23184 theline = line_arg;
23185 p = vim_strchr(theline, '\n');
23186 if (p == NULL)
23187 line_arg += STRLEN(line_arg);
23188 else
23189 {
23190 *p = NUL;
23191 line_arg = p + 1;
23192 }
23193 }
23194 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 theline = getcmdline(':', 0L, indent);
23196 else
23197 theline = eap->getline(':', eap->cookie, indent);
23198 if (KeyTyped)
23199 lines_left = Rows - 1;
23200 if (theline == NULL)
23201 {
23202 EMSG(_("E126: Missing :endfunction"));
23203 goto erret;
23204 }
23205
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023206 /* Detect line continuation: sourcing_lnum increased more than one. */
23207 if (sourcing_lnum > sourcing_lnum_off + 1)
23208 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23209 else
23210 sourcing_lnum_off = 0;
23211
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 if (skip_until != NULL)
23213 {
23214 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23215 * don't check for ":endfunc". */
23216 if (STRCMP(theline, skip_until) == 0)
23217 {
23218 vim_free(skip_until);
23219 skip_until = NULL;
23220 }
23221 }
23222 else
23223 {
23224 /* skip ':' and blanks*/
23225 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23226 ;
23227
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023228 /* Check for "endfunction". */
23229 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023231 if (line_arg == NULL)
23232 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 break;
23234 }
23235
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023236 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 * at "end". */
23238 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23239 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023240 else if (STRNCMP(p, "if", 2) == 0
23241 || STRNCMP(p, "wh", 2) == 0
23242 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 || STRNCMP(p, "try", 3) == 0)
23244 indent += 2;
23245
23246 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023247 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023249 if (*p == '!')
23250 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023252 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23253 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023255 ++nesting;
23256 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 }
23258 }
23259
23260 /* Check for ":append" or ":insert". */
23261 p = skip_range(p, NULL);
23262 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23263 || (p[0] == 'i'
23264 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23265 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23266 skip_until = vim_strsave((char_u *)".");
23267
23268 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23269 arg = skipwhite(skiptowhite(p));
23270 if (arg[0] == '<' && arg[1] =='<'
23271 && ((p[0] == 'p' && p[1] == 'y'
23272 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23273 || (p[0] == 'p' && p[1] == 'e'
23274 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23275 || (p[0] == 't' && p[1] == 'c'
23276 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023277 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23278 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23280 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023281 || (p[0] == 'm' && p[1] == 'z'
23282 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 ))
23284 {
23285 /* ":python <<" continues until a dot, like ":append" */
23286 p = skipwhite(arg + 2);
23287 if (*p == NUL)
23288 skip_until = vim_strsave((char_u *)".");
23289 else
23290 skip_until = vim_strsave(p);
23291 }
23292 }
23293
23294 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023295 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023296 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023297 if (line_arg == NULL)
23298 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023300 }
23301
23302 /* Copy the line to newly allocated memory. get_one_sourceline()
23303 * allocates 250 bytes per line, this saves 80% on average. The cost
23304 * is an extra alloc/free. */
23305 p = vim_strsave(theline);
23306 if (p != NULL)
23307 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023308 if (line_arg == NULL)
23309 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023310 theline = p;
23311 }
23312
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023313 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23314
23315 /* Add NULL lines for continuation lines, so that the line count is
23316 * equal to the index in the growarray. */
23317 while (sourcing_lnum_off-- > 0)
23318 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023319
23320 /* Check for end of eap->arg. */
23321 if (line_arg != NULL && *line_arg == NUL)
23322 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 }
23324
23325 /* Don't define the function when skipping commands or when an error was
23326 * detected. */
23327 if (eap->skip || did_emsg)
23328 goto erret;
23329
23330 /*
23331 * If there are no errors, add the function
23332 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023333 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023334 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023335 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023336 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023337 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023338 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023339 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023340 goto erret;
23341 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023342
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023343 fp = find_func(name);
23344 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023346 if (!eap->forceit)
23347 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023348 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023349 goto erret;
23350 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023351 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023352 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023353 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023354 name);
23355 goto erret;
23356 }
23357 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023358 ga_clear_strings(&(fp->uf_args));
23359 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023360 vim_free(name);
23361 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 }
23364 else
23365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023366 char numbuf[20];
23367
23368 fp = NULL;
23369 if (fudi.fd_newkey == NULL && !eap->forceit)
23370 {
23371 EMSG(_(e_funcdict));
23372 goto erret;
23373 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023374 if (fudi.fd_di == NULL)
23375 {
23376 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023377 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023378 goto erret;
23379 }
23380 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023381 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023382 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023383
23384 /* Give the function a sequential number. Can only be used with a
23385 * Funcref! */
23386 vim_free(name);
23387 sprintf(numbuf, "%d", ++func_nr);
23388 name = vim_strsave((char_u *)numbuf);
23389 if (name == NULL)
23390 goto erret;
23391 }
23392
23393 if (fp == NULL)
23394 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023395 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023396 {
23397 int slen, plen;
23398 char_u *scriptname;
23399
23400 /* Check that the autoload name matches the script name. */
23401 j = FAIL;
23402 if (sourcing_name != NULL)
23403 {
23404 scriptname = autoload_name(name);
23405 if (scriptname != NULL)
23406 {
23407 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023408 plen = (int)STRLEN(p);
23409 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023410 if (slen > plen && fnamecmp(p,
23411 sourcing_name + slen - plen) == 0)
23412 j = OK;
23413 vim_free(scriptname);
23414 }
23415 }
23416 if (j == FAIL)
23417 {
23418 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23419 goto erret;
23420 }
23421 }
23422
23423 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 if (fp == NULL)
23425 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023426
23427 if (fudi.fd_dict != NULL)
23428 {
23429 if (fudi.fd_di == NULL)
23430 {
23431 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023432 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023433 if (fudi.fd_di == NULL)
23434 {
23435 vim_free(fp);
23436 goto erret;
23437 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023438 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23439 {
23440 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023441 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023442 goto erret;
23443 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023444 }
23445 else
23446 /* overwrite existing dict entry */
23447 clear_tv(&fudi.fd_di->di_tv);
23448 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023449 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023450 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023451 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023452
23453 /* behave like "dict" was used */
23454 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023455 }
23456
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023458 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023459 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23460 {
23461 vim_free(fp);
23462 goto erret;
23463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023465 fp->uf_args = newargs;
23466 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023467#ifdef FEAT_PROFILE
23468 fp->uf_tml_count = NULL;
23469 fp->uf_tml_total = NULL;
23470 fp->uf_tml_self = NULL;
23471 fp->uf_profiling = FALSE;
23472 if (prof_def_func())
23473 func_do_profile(fp);
23474#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023475 fp->uf_varargs = varargs;
23476 fp->uf_flags = flags;
23477 fp->uf_calls = 0;
23478 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023479 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480
23481erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 ga_clear_strings(&newargs);
23483 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023484ret_free:
23485 vim_free(skip_until);
23486 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023489 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490}
23491
23492/*
23493 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023494 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023496 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023497 * TFN_INT: internal function name OK
23498 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023499 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 * Advances "pp" to just after the function name (if no error).
23501 */
23502 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023503trans_function_name(
23504 char_u **pp,
23505 int skip, /* only find the end, don't evaluate */
23506 int flags,
23507 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023509 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 char_u *start;
23511 char_u *end;
23512 int lead;
23513 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023515 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023516
23517 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023518 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023520
23521 /* Check for hard coded <SNR>: already translated function ID (from a user
23522 * command). */
23523 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23524 && (*pp)[2] == (int)KE_SNR)
23525 {
23526 *pp += 3;
23527 len = get_id_len(pp) + 3;
23528 return vim_strnsave(start, len);
23529 }
23530
23531 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23532 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023534 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023536
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023537 /* Note that TFN_ flags use the same values as GLV_ flags. */
23538 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023539 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023540 if (end == start)
23541 {
23542 if (!skip)
23543 EMSG(_("E129: Function name required"));
23544 goto theend;
23545 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023546 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023547 {
23548 /*
23549 * Report an invalid expression in braces, unless the expression
23550 * evaluation has been cancelled due to an aborting error, an
23551 * interrupt, or an exception.
23552 */
23553 if (!aborting())
23554 {
23555 if (end != NULL)
23556 EMSG2(_(e_invarg2), start);
23557 }
23558 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023559 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023560 goto theend;
23561 }
23562
23563 if (lv.ll_tv != NULL)
23564 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023565 if (fdp != NULL)
23566 {
23567 fdp->fd_dict = lv.ll_dict;
23568 fdp->fd_newkey = lv.ll_newkey;
23569 lv.ll_newkey = NULL;
23570 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023571 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023572 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23573 {
23574 name = vim_strsave(lv.ll_tv->vval.v_string);
23575 *pp = end;
23576 }
23577 else
23578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023579 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23580 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023581 EMSG(_(e_funcref));
23582 else
23583 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023584 name = NULL;
23585 }
23586 goto theend;
23587 }
23588
23589 if (lv.ll_name == NULL)
23590 {
23591 /* Error found, but continue after the function name. */
23592 *pp = end;
23593 goto theend;
23594 }
23595
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023596 /* Check if the name is a Funcref. If so, use the value. */
23597 if (lv.ll_exp_name != NULL)
23598 {
23599 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023600 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023601 if (name == lv.ll_exp_name)
23602 name = NULL;
23603 }
23604 else
23605 {
23606 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023607 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023608 if (name == *pp)
23609 name = NULL;
23610 }
23611 if (name != NULL)
23612 {
23613 name = vim_strsave(name);
23614 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023615 if (STRNCMP(name, "<SNR>", 5) == 0)
23616 {
23617 /* Change "<SNR>" to the byte sequence. */
23618 name[0] = K_SPECIAL;
23619 name[1] = KS_EXTRA;
23620 name[2] = (int)KE_SNR;
23621 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23622 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023623 goto theend;
23624 }
23625
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023626 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023627 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023628 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023629 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23630 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23631 {
23632 /* When there was "s:" already or the name expanded to get a
23633 * leading "s:" then remove it. */
23634 lv.ll_name += 2;
23635 len -= 2;
23636 lead = 2;
23637 }
23638 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023639 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023640 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023641 /* skip over "s:" and "g:" */
23642 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023643 lv.ll_name += 2;
23644 len = (int)(end - lv.ll_name);
23645 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023646
23647 /*
23648 * Copy the function name to allocated memory.
23649 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23650 * Accept <SNR>123_name() outside a script.
23651 */
23652 if (skip)
23653 lead = 0; /* do nothing */
23654 else if (lead > 0)
23655 {
23656 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023657 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23658 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023659 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023660 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023661 if (current_SID <= 0)
23662 {
23663 EMSG(_(e_usingsid));
23664 goto theend;
23665 }
23666 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23667 lead += (int)STRLEN(sid_buf);
23668 }
23669 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023670 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023671 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023672 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023673 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023674 goto theend;
23675 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023676 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023677 {
23678 char_u *cp = vim_strchr(lv.ll_name, ':');
23679
23680 if (cp != NULL && cp < end)
23681 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023682 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023683 goto theend;
23684 }
23685 }
23686
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023687 name = alloc((unsigned)(len + lead + 1));
23688 if (name != NULL)
23689 {
23690 if (lead > 0)
23691 {
23692 name[0] = K_SPECIAL;
23693 name[1] = KS_EXTRA;
23694 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023695 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023696 STRCPY(name + 3, sid_buf);
23697 }
23698 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023699 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023700 }
23701 *pp = end;
23702
23703theend:
23704 clear_lval(&lv);
23705 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706}
23707
23708/*
23709 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23710 * Return 2 if "p" starts with "s:".
23711 * Return 0 otherwise.
23712 */
23713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023714eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023716 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23717 * the standard library function. */
23718 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23719 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 return 5;
23721 if (p[0] == 's' && p[1] == ':')
23722 return 2;
23723 return 0;
23724}
23725
23726/*
23727 * Return TRUE if "p" starts with "<SID>" or "s:".
23728 * Only works if eval_fname_script() returned non-zero for "p"!
23729 */
23730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023731eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732{
23733 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23734}
23735
23736/*
23737 * List the head of the function: "name(arg1, arg2)".
23738 */
23739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023740list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741{
23742 int j;
23743
23744 msg_start();
23745 if (indent)
23746 MSG_PUTS(" ");
23747 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023748 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749 {
23750 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023751 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 }
23753 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023754 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023756 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 {
23758 if (j)
23759 MSG_PUTS(", ");
23760 msg_puts(FUNCARG(fp, j));
23761 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023762 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 {
23764 if (j)
23765 MSG_PUTS(", ");
23766 MSG_PUTS("...");
23767 }
23768 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023769 if (fp->uf_flags & FC_ABORT)
23770 MSG_PUTS(" abort");
23771 if (fp->uf_flags & FC_RANGE)
23772 MSG_PUTS(" range");
23773 if (fp->uf_flags & FC_DICT)
23774 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023775 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023776 if (p_verbose > 0)
23777 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023778}
23779
23780/*
23781 * Find a function by name, return pointer to it in ufuncs.
23782 * Return NULL for unknown function.
23783 */
23784 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023785find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023787 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023789 hi = hash_find(&func_hashtab, name);
23790 if (!HASHITEM_EMPTY(hi))
23791 return HI2UF(hi);
23792 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793}
23794
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023795#if defined(EXITFREE) || defined(PROTO)
23796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023797free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023798{
23799 hashitem_T *hi;
23800
23801 /* Need to start all over every time, because func_free() may change the
23802 * hash table. */
23803 while (func_hashtab.ht_used > 0)
23804 for (hi = func_hashtab.ht_array; ; ++hi)
23805 if (!HASHITEM_EMPTY(hi))
23806 {
23807 func_free(HI2UF(hi));
23808 break;
23809 }
23810}
23811#endif
23812
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023814translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023815{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023816 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023817 return find_internal_func(name) >= 0;
23818 return find_func(name) != NULL;
23819}
23820
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023821/*
23822 * Return TRUE if a function "name" exists.
23823 */
23824 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023825function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023826{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023827 char_u *nm = name;
23828 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023829 int n = FALSE;
23830
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023831 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23832 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023833 nm = skipwhite(nm);
23834
23835 /* Only accept "funcname", "funcname ", "funcname (..." and
23836 * "funcname(...", not "funcname!...". */
23837 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023838 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023839 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023840 return n;
23841}
23842
Bram Moolenaara1544c02013-05-30 12:35:52 +020023843 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023844get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023845{
23846 char_u *nm = name;
23847 char_u *p;
23848
23849 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23850
23851 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023852 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023853 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023854
Bram Moolenaara1544c02013-05-30 12:35:52 +020023855 vim_free(p);
23856 return NULL;
23857}
23858
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023859/*
23860 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023861 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23862 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023863 */
23864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023865builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023866{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023867 char_u *p;
23868
23869 if (!ASCII_ISLOWER(name[0]))
23870 return FALSE;
23871 p = vim_strchr(name, AUTOLOAD_CHAR);
23872 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023873}
23874
Bram Moolenaar05159a02005-02-26 23:04:13 +000023875#if defined(FEAT_PROFILE) || defined(PROTO)
23876/*
23877 * Start profiling function "fp".
23878 */
23879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023880func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023881{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023882 int len = fp->uf_lines.ga_len;
23883
23884 if (len == 0)
23885 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023886 fp->uf_tm_count = 0;
23887 profile_zero(&fp->uf_tm_self);
23888 profile_zero(&fp->uf_tm_total);
23889 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023890 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023891 if (fp->uf_tml_total == NULL)
23892 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023893 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023894 if (fp->uf_tml_self == NULL)
23895 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023896 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023897 fp->uf_tml_idx = -1;
23898 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23899 || fp->uf_tml_self == NULL)
23900 return; /* out of memory */
23901
23902 fp->uf_profiling = TRUE;
23903}
23904
23905/*
23906 * Dump the profiling results for all functions in file "fd".
23907 */
23908 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023909func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023910{
23911 hashitem_T *hi;
23912 int todo;
23913 ufunc_T *fp;
23914 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023915 ufunc_T **sorttab;
23916 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023918 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023919 if (todo == 0)
23920 return; /* nothing to dump */
23921
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023922 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023923
Bram Moolenaar05159a02005-02-26 23:04:13 +000023924 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23925 {
23926 if (!HASHITEM_EMPTY(hi))
23927 {
23928 --todo;
23929 fp = HI2UF(hi);
23930 if (fp->uf_profiling)
23931 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023932 if (sorttab != NULL)
23933 sorttab[st_len++] = fp;
23934
Bram Moolenaar05159a02005-02-26 23:04:13 +000023935 if (fp->uf_name[0] == K_SPECIAL)
23936 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23937 else
23938 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23939 if (fp->uf_tm_count == 1)
23940 fprintf(fd, "Called 1 time\n");
23941 else
23942 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23943 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23944 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23945 fprintf(fd, "\n");
23946 fprintf(fd, "count total (s) self (s)\n");
23947
23948 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23949 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023950 if (FUNCLINE(fp, i) == NULL)
23951 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023952 prof_func_line(fd, fp->uf_tml_count[i],
23953 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023954 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23955 }
23956 fprintf(fd, "\n");
23957 }
23958 }
23959 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023960
23961 if (sorttab != NULL && st_len > 0)
23962 {
23963 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23964 prof_total_cmp);
23965 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23966 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23967 prof_self_cmp);
23968 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23969 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023970
23971 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023972}
Bram Moolenaar73830342005-02-28 22:48:19 +000023973
23974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023975prof_sort_list(
23976 FILE *fd,
23977 ufunc_T **sorttab,
23978 int st_len,
23979 char *title,
23980 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023981{
23982 int i;
23983 ufunc_T *fp;
23984
23985 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23986 fprintf(fd, "count total (s) self (s) function\n");
23987 for (i = 0; i < 20 && i < st_len; ++i)
23988 {
23989 fp = sorttab[i];
23990 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23991 prefer_self);
23992 if (fp->uf_name[0] == K_SPECIAL)
23993 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23994 else
23995 fprintf(fd, " %s()\n", fp->uf_name);
23996 }
23997 fprintf(fd, "\n");
23998}
23999
24000/*
24001 * Print the count and times for one function or function line.
24002 */
24003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024004prof_func_line(
24005 FILE *fd,
24006 int count,
24007 proftime_T *total,
24008 proftime_T *self,
24009 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024010{
24011 if (count > 0)
24012 {
24013 fprintf(fd, "%5d ", count);
24014 if (prefer_self && profile_equal(total, self))
24015 fprintf(fd, " ");
24016 else
24017 fprintf(fd, "%s ", profile_msg(total));
24018 if (!prefer_self && profile_equal(total, self))
24019 fprintf(fd, " ");
24020 else
24021 fprintf(fd, "%s ", profile_msg(self));
24022 }
24023 else
24024 fprintf(fd, " ");
24025}
24026
24027/*
24028 * Compare function for total time sorting.
24029 */
24030 static int
24031#ifdef __BORLANDC__
24032_RTLENTRYF
24033#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024034prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024035{
24036 ufunc_T *p1, *p2;
24037
24038 p1 = *(ufunc_T **)s1;
24039 p2 = *(ufunc_T **)s2;
24040 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24041}
24042
24043/*
24044 * Compare function for self time sorting.
24045 */
24046 static int
24047#ifdef __BORLANDC__
24048_RTLENTRYF
24049#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024050prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024051{
24052 ufunc_T *p1, *p2;
24053
24054 p1 = *(ufunc_T **)s1;
24055 p2 = *(ufunc_T **)s2;
24056 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24057}
24058
Bram Moolenaar05159a02005-02-26 23:04:13 +000024059#endif
24060
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024061/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024062 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024063 * Return TRUE if a package was loaded.
24064 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024065 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024066script_autoload(
24067 char_u *name,
24068 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024069{
24070 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024071 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024072 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024073 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024074
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024075 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024076 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024077 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024078 return FALSE;
24079
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024080 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024081
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024082 /* Find the name in the list of previously loaded package names. Skip
24083 * "autoload/", it's always the same. */
24084 for (i = 0; i < ga_loaded.ga_len; ++i)
24085 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24086 break;
24087 if (!reload && i < ga_loaded.ga_len)
24088 ret = FALSE; /* was loaded already */
24089 else
24090 {
24091 /* Remember the name if it wasn't loaded already. */
24092 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24093 {
24094 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24095 tofree = NULL;
24096 }
24097
24098 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024099 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024100 ret = TRUE;
24101 }
24102
24103 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024104 return ret;
24105}
24106
24107/*
24108 * Return the autoload script name for a function or variable name.
24109 * Returns NULL when out of memory.
24110 */
24111 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024112autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024113{
24114 char_u *p;
24115 char_u *scriptname;
24116
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024117 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024118 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24119 if (scriptname == NULL)
24120 return FALSE;
24121 STRCPY(scriptname, "autoload/");
24122 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024123 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024124 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024125 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024126 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024127 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024128}
24129
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24131
24132/*
24133 * Function given to ExpandGeneric() to obtain the list of user defined
24134 * function names.
24135 */
24136 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024137get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024139 static long_u done;
24140 static hashitem_T *hi;
24141 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142
24143 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024145 done = 0;
24146 hi = func_hashtab.ht_array;
24147 }
24148 if (done < func_hashtab.ht_used)
24149 {
24150 if (done++ > 0)
24151 ++hi;
24152 while (HASHITEM_EMPTY(hi))
24153 ++hi;
24154 fp = HI2UF(hi);
24155
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024156 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024157 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024158
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024159 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24160 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024161
24162 cat_func_name(IObuff, fp);
24163 if (xp->xp_context != EXPAND_USER_FUNC)
24164 {
24165 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024166 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 STRCAT(IObuff, ")");
24168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169 return IObuff;
24170 }
24171 return NULL;
24172}
24173
24174#endif /* FEAT_CMDL_COMPL */
24175
24176/*
24177 * Copy the function name of "fp" to buffer "buf".
24178 * "buf" must be able to hold the function name plus three bytes.
24179 * Takes care of script-local function names.
24180 */
24181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024182cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024184 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 {
24186 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024187 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 }
24189 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024190 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191}
24192
24193/*
24194 * ":delfunction {name}"
24195 */
24196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024197ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024199 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200 char_u *p;
24201 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024202 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203
24204 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024205 name = trans_function_name(&p, eap->skip, 0, &fudi);
24206 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024208 {
24209 if (fudi.fd_dict != NULL && !eap->skip)
24210 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213 if (!ends_excmd(*skipwhite(p)))
24214 {
24215 vim_free(name);
24216 EMSG(_(e_trailing));
24217 return;
24218 }
24219 eap->nextcmd = check_nextcmd(p);
24220 if (eap->nextcmd != NULL)
24221 *p = NUL;
24222
24223 if (!eap->skip)
24224 fp = find_func(name);
24225 vim_free(name);
24226
24227 if (!eap->skip)
24228 {
24229 if (fp == NULL)
24230 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024231 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 return;
24233 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024234 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024235 {
24236 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24237 return;
24238 }
24239
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024240 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024242 /* Delete the dict item that refers to the function, it will
24243 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024244 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024246 else
24247 func_free(fp);
24248 }
24249}
24250
24251/*
24252 * Free a function and remove it from the list of functions.
24253 */
24254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024255func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024256{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024257 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024258
24259 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024260 ga_clear_strings(&(fp->uf_args));
24261 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024262#ifdef FEAT_PROFILE
24263 vim_free(fp->uf_tml_count);
24264 vim_free(fp->uf_tml_total);
24265 vim_free(fp->uf_tml_self);
24266#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024267
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024268 /* remove the function from the function hashtable */
24269 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24270 if (HASHITEM_EMPTY(hi))
24271 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024272 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024273 hash_remove(&func_hashtab, hi);
24274
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024275 vim_free(fp);
24276}
24277
24278/*
24279 * Unreference a Function: decrement the reference count and free it when it
24280 * becomes zero. Only for numbered functions.
24281 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024283func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024284{
24285 ufunc_T *fp;
24286
24287 if (name != NULL && isdigit(*name))
24288 {
24289 fp = find_func(name);
24290 if (fp == NULL)
24291 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024292 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024293 {
24294 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024295 * when "uf_calls" becomes zero. */
24296 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024297 func_free(fp);
24298 }
24299 }
24300}
24301
24302/*
24303 * Count a reference to a Function.
24304 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024306func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024307{
24308 ufunc_T *fp;
24309
24310 if (name != NULL && isdigit(*name))
24311 {
24312 fp = find_func(name);
24313 if (fp == NULL)
24314 EMSG2(_(e_intern2), "func_ref()");
24315 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024316 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 }
24318}
24319
24320/*
24321 * Call a user function.
24322 */
24323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024324call_user_func(
24325 ufunc_T *fp, /* pointer to function */
24326 int argcount, /* nr of args */
24327 typval_T *argvars, /* arguments */
24328 typval_T *rettv, /* return value */
24329 linenr_T firstline, /* first line of range */
24330 linenr_T lastline, /* last line of range */
24331 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332{
Bram Moolenaar33570922005-01-25 22:26:29 +000024333 char_u *save_sourcing_name;
24334 linenr_T save_sourcing_lnum;
24335 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024336 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024337 int save_did_emsg;
24338 static int depth = 0;
24339 dictitem_T *v;
24340 int fixvar_idx = 0; /* index in fixvar[] */
24341 int i;
24342 int ai;
24343 char_u numbuf[NUMBUFLEN];
24344 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024345 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024346#ifdef FEAT_PROFILE
24347 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024348 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350
24351 /* If depth of calling is getting too high, don't execute the function */
24352 if (depth >= p_mfd)
24353 {
24354 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024355 rettv->v_type = VAR_NUMBER;
24356 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 return;
24358 }
24359 ++depth;
24360
24361 line_breakcheck(); /* check for CTRL-C hit */
24362
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024363 fc = (funccall_T *)alloc(sizeof(funccall_T));
24364 fc->caller = current_funccal;
24365 current_funccal = fc;
24366 fc->func = fp;
24367 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024368 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024369 fc->linenr = 0;
24370 fc->returned = FALSE;
24371 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024373 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24374 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375
Bram Moolenaar33570922005-01-25 22:26:29 +000024376 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024377 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024378 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24379 * each argument variable and saves a lot of time.
24380 */
24381 /*
24382 * Init l: variables.
24383 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024384 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024385 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024386 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024387 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24388 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024389 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024390 name = v->di_key;
24391 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024392 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024393 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024394 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024395 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024396 v->di_tv.vval.v_dict = selfdict;
24397 ++selfdict->dv_refcount;
24398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024399
Bram Moolenaar33570922005-01-25 22:26:29 +000024400 /*
24401 * Init a: variables.
24402 * Set a:0 to "argcount".
24403 * Set a:000 to a list with room for the "..." arguments.
24404 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024405 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024406 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024407 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024408 /* Use "name" to avoid a warning from some compiler that checks the
24409 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024410 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024411 name = v->di_key;
24412 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024413 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024414 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024415 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024416 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024417 v->di_tv.vval.v_list = &fc->l_varlist;
24418 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24419 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24420 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024421
24422 /*
24423 * Set a:firstline to "firstline" and a:lastline to "lastline".
24424 * Set a:name to named arguments.
24425 * Set a:N to the "..." arguments.
24426 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024427 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024428 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024429 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024430 (varnumber_T)lastline);
24431 for (i = 0; i < argcount; ++i)
24432 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024433 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024434 if (ai < 0)
24435 /* named argument a:name */
24436 name = FUNCARG(fp, i);
24437 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024438 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024439 /* "..." argument a:1, a:2, etc. */
24440 sprintf((char *)numbuf, "%d", ai + 1);
24441 name = numbuf;
24442 }
24443 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24444 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024445 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024446 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24447 }
24448 else
24449 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024450 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24451 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024452 if (v == NULL)
24453 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024454 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024455 }
24456 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024457 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024458
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024459 /* Note: the values are copied directly to avoid alloc/free.
24460 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024461 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024462 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024463
24464 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24465 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024466 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24467 fc->l_listitems[ai].li_tv = argvars[i];
24468 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024469 }
24470 }
24471
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472 /* Don't redraw while executing the function. */
24473 ++RedrawingDisabled;
24474 save_sourcing_name = sourcing_name;
24475 save_sourcing_lnum = sourcing_lnum;
24476 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024477 /* need space for function name + ("function " + 3) or "[number]" */
24478 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24479 + STRLEN(fp->uf_name) + 20;
24480 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 if (sourcing_name != NULL)
24482 {
24483 if (save_sourcing_name != NULL
24484 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024485 sprintf((char *)sourcing_name, "%s[%d]..",
24486 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487 else
24488 STRCPY(sourcing_name, "function ");
24489 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24490
24491 if (p_verbose >= 12)
24492 {
24493 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024494 verbose_enter_scroll();
24495
Bram Moolenaar555b2802005-05-19 21:08:39 +000024496 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497 if (p_verbose >= 14)
24498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024500 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024501 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024502 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503
24504 msg_puts((char_u *)"(");
24505 for (i = 0; i < argcount; ++i)
24506 {
24507 if (i > 0)
24508 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024509 if (argvars[i].v_type == VAR_NUMBER)
24510 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511 else
24512 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024513 /* Do not want errors such as E724 here. */
24514 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024515 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024516 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024517 if (s != NULL)
24518 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024519 if (vim_strsize(s) > MSG_BUF_CLEN)
24520 {
24521 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24522 s = buf;
24523 }
24524 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024525 vim_free(tofree);
24526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 }
24528 }
24529 msg_puts((char_u *)")");
24530 }
24531 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024532
24533 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 --no_wait_return;
24535 }
24536 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024537#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024538 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024539 {
24540 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24541 func_do_profile(fp);
24542 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024543 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024544 {
24545 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024546 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024547 profile_zero(&fp->uf_tm_children);
24548 }
24549 script_prof_save(&wait_start);
24550 }
24551#endif
24552
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024554 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 save_did_emsg = did_emsg;
24556 did_emsg = FALSE;
24557
24558 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024559 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24561
24562 --RedrawingDisabled;
24563
24564 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024565 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024567 clear_tv(rettv);
24568 rettv->v_type = VAR_NUMBER;
24569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 }
24571
Bram Moolenaar05159a02005-02-26 23:04:13 +000024572#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024573 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024574 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024575 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024576 profile_end(&call_start);
24577 profile_sub_wait(&wait_start, &call_start);
24578 profile_add(&fp->uf_tm_total, &call_start);
24579 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024580 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024581 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024582 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24583 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024584 }
24585 }
24586#endif
24587
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588 /* when being verbose, mention the return value */
24589 if (p_verbose >= 12)
24590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024592 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024595 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024596 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024597 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024598 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024601 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024602 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024603 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024604 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024605
Bram Moolenaar555b2802005-05-19 21:08:39 +000024606 /* The value may be very long. Skip the middle part, so that we
24607 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024608 * truncate it at the end. Don't want errors such as E724 here. */
24609 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024610 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024611 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024612 if (s != NULL)
24613 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024614 if (vim_strsize(s) > MSG_BUF_CLEN)
24615 {
24616 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24617 s = buf;
24618 }
24619 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024620 vim_free(tofree);
24621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622 }
24623 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024624
24625 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 --no_wait_return;
24627 }
24628
24629 vim_free(sourcing_name);
24630 sourcing_name = save_sourcing_name;
24631 sourcing_lnum = save_sourcing_lnum;
24632 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024633#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024634 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024635 script_prof_restore(&wait_start);
24636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637
24638 if (p_verbose >= 12 && sourcing_name != NULL)
24639 {
24640 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024641 verbose_enter_scroll();
24642
Bram Moolenaar555b2802005-05-19 21:08:39 +000024643 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024645
24646 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 --no_wait_return;
24648 }
24649
24650 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024651 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024653
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024654 /* If the a:000 list and the l: and a: dicts are not referenced we can
24655 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024656 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24657 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24658 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24659 {
24660 free_funccal(fc, FALSE);
24661 }
24662 else
24663 {
24664 hashitem_T *hi;
24665 listitem_T *li;
24666 int todo;
24667
24668 /* "fc" is still in use. This can happen when returning "a:000" or
24669 * assigning "l:" to a global variable.
24670 * Link "fc" in the list for garbage collection later. */
24671 fc->caller = previous_funccal;
24672 previous_funccal = fc;
24673
24674 /* Make a copy of the a: variables, since we didn't do that above. */
24675 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24676 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24677 {
24678 if (!HASHITEM_EMPTY(hi))
24679 {
24680 --todo;
24681 v = HI2DI(hi);
24682 copy_tv(&v->di_tv, &v->di_tv);
24683 }
24684 }
24685
24686 /* Make a copy of the a:000 items, since we didn't do that above. */
24687 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24688 copy_tv(&li->li_tv, &li->li_tv);
24689 }
24690}
24691
24692/*
24693 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024694 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024695 */
24696 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024697can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024698{
24699 return (fc->l_varlist.lv_copyID != copyID
24700 && fc->l_vars.dv_copyID != copyID
24701 && fc->l_avars.dv_copyID != copyID);
24702}
24703
24704/*
24705 * Free "fc" and what it contains.
24706 */
24707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024708free_funccal(
24709 funccall_T *fc,
24710 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024711{
24712 listitem_T *li;
24713
24714 /* The a: variables typevals may not have been allocated, only free the
24715 * allocated variables. */
24716 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24717
24718 /* free all l: variables */
24719 vars_clear(&fc->l_vars.dv_hashtab);
24720
24721 /* Free the a:000 variables if they were allocated. */
24722 if (free_val)
24723 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24724 clear_tv(&li->li_tv);
24725
24726 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727}
24728
24729/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024730 * Add a number variable "name" to dict "dp" with value "nr".
24731 */
24732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024733add_nr_var(
24734 dict_T *dp,
24735 dictitem_T *v,
24736 char *name,
24737 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024738{
24739 STRCPY(v->di_key, name);
24740 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24741 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24742 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024743 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024744 v->di_tv.vval.v_number = nr;
24745}
24746
24747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024748 * ":return [expr]"
24749 */
24750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024751ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024752{
24753 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024754 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755 int returning = FALSE;
24756
24757 if (current_funccal == NULL)
24758 {
24759 EMSG(_("E133: :return not inside a function"));
24760 return;
24761 }
24762
24763 if (eap->skip)
24764 ++emsg_skip;
24765
24766 eap->nextcmd = NULL;
24767 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024768 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 {
24770 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024771 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024773 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774 }
24775 /* It's safer to return also on error. */
24776 else if (!eap->skip)
24777 {
24778 /*
24779 * Return unless the expression evaluation has been cancelled due to an
24780 * aborting error, an interrupt, or an exception.
24781 */
24782 if (!aborting())
24783 returning = do_return(eap, FALSE, TRUE, NULL);
24784 }
24785
24786 /* When skipping or the return gets pending, advance to the next command
24787 * in this line (!returning). Otherwise, ignore the rest of the line.
24788 * Following lines will be ignored by get_func_line(). */
24789 if (returning)
24790 eap->nextcmd = NULL;
24791 else if (eap->nextcmd == NULL) /* no argument */
24792 eap->nextcmd = check_nextcmd(arg);
24793
24794 if (eap->skip)
24795 --emsg_skip;
24796}
24797
24798/*
24799 * Return from a function. Possibly makes the return pending. Also called
24800 * for a pending return at the ":endtry" or after returning from an extra
24801 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024802 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024803 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 * FALSE when the return gets pending.
24805 */
24806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024807do_return(
24808 exarg_T *eap,
24809 int reanimate,
24810 int is_cmd,
24811 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812{
24813 int idx;
24814 struct condstack *cstack = eap->cstack;
24815
24816 if (reanimate)
24817 /* Undo the return. */
24818 current_funccal->returned = FALSE;
24819
24820 /*
24821 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24822 * not in its finally clause (which then is to be executed next) is found.
24823 * In this case, make the ":return" pending for execution at the ":endtry".
24824 * Otherwise, return normally.
24825 */
24826 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24827 if (idx >= 0)
24828 {
24829 cstack->cs_pending[idx] = CSTP_RETURN;
24830
24831 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024832 /* A pending return again gets pending. "rettv" points to an
24833 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024835 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024836 else
24837 {
24838 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024839 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024841 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024843 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024844 {
24845 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024846 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024847 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848 else
24849 EMSG(_(e_outofmem));
24850 }
24851 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024852 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853
24854 if (reanimate)
24855 {
24856 /* The pending return value could be overwritten by a ":return"
24857 * without argument in a finally clause; reset the default
24858 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024859 current_funccal->rettv->v_type = VAR_NUMBER;
24860 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024861 }
24862 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024863 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 }
24865 else
24866 {
24867 current_funccal->returned = TRUE;
24868
24869 /* If the return is carried out now, store the return value. For
24870 * a return immediately after reanimation, the value is already
24871 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024872 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024874 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024875 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024877 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 }
24879 }
24880
24881 return idx < 0;
24882}
24883
24884/*
24885 * Free the variable with a pending return value.
24886 */
24887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024888discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889{
Bram Moolenaar33570922005-01-25 22:26:29 +000024890 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891}
24892
24893/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024894 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 * is an allocated string. Used by report_pending() for verbose messages.
24896 */
24897 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024898get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024900 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024901 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024902 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024904 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024905 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024906 if (s == NULL)
24907 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024908
24909 STRCPY(IObuff, ":return ");
24910 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24911 if (STRLEN(s) + 8 >= IOSIZE)
24912 STRCPY(IObuff + IOSIZE - 4, "...");
24913 vim_free(tofree);
24914 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915}
24916
24917/*
24918 * Get next function line.
24919 * Called by do_cmdline() to get the next line.
24920 * Returns allocated string, or NULL for end of function.
24921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024923get_func_line(
24924 int c UNUSED,
24925 void *cookie,
24926 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927{
Bram Moolenaar33570922005-01-25 22:26:29 +000024928 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024929 ufunc_T *fp = fcp->func;
24930 char_u *retval;
24931 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932
24933 /* If breakpoints have been added/deleted need to check for it. */
24934 if (fcp->dbg_tick != debug_tick)
24935 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024936 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 sourcing_lnum);
24938 fcp->dbg_tick = debug_tick;
24939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024940#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024941 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024942 func_line_end(cookie);
24943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944
Bram Moolenaar05159a02005-02-26 23:04:13 +000024945 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024946 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24947 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024948 retval = NULL;
24949 else
24950 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024951 /* Skip NULL lines (continuation lines). */
24952 while (fcp->linenr < gap->ga_len
24953 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24954 ++fcp->linenr;
24955 if (fcp->linenr >= gap->ga_len)
24956 retval = NULL;
24957 else
24958 {
24959 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24960 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024961#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024962 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024963 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024964#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966 }
24967
24968 /* Did we encounter a breakpoint? */
24969 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24970 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024971 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024972 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024973 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974 sourcing_lnum);
24975 fcp->dbg_tick = debug_tick;
24976 }
24977
24978 return retval;
24979}
24980
Bram Moolenaar05159a02005-02-26 23:04:13 +000024981#if defined(FEAT_PROFILE) || defined(PROTO)
24982/*
24983 * Called when starting to read a function line.
24984 * "sourcing_lnum" must be correct!
24985 * When skipping lines it may not actually be executed, but we won't find out
24986 * until later and we need to store the time now.
24987 */
24988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024989func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024990{
24991 funccall_T *fcp = (funccall_T *)cookie;
24992 ufunc_T *fp = fcp->func;
24993
24994 if (fp->uf_profiling && sourcing_lnum >= 1
24995 && sourcing_lnum <= fp->uf_lines.ga_len)
24996 {
24997 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024998 /* Skip continuation lines. */
24999 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25000 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025001 fp->uf_tml_execed = FALSE;
25002 profile_start(&fp->uf_tml_start);
25003 profile_zero(&fp->uf_tml_children);
25004 profile_get_wait(&fp->uf_tml_wait);
25005 }
25006}
25007
25008/*
25009 * Called when actually executing a function line.
25010 */
25011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025012func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025013{
25014 funccall_T *fcp = (funccall_T *)cookie;
25015 ufunc_T *fp = fcp->func;
25016
25017 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25018 fp->uf_tml_execed = TRUE;
25019}
25020
25021/*
25022 * Called when done with a function line.
25023 */
25024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025025func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025026{
25027 funccall_T *fcp = (funccall_T *)cookie;
25028 ufunc_T *fp = fcp->func;
25029
25030 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25031 {
25032 if (fp->uf_tml_execed)
25033 {
25034 ++fp->uf_tml_count[fp->uf_tml_idx];
25035 profile_end(&fp->uf_tml_start);
25036 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025037 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025038 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25039 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025040 }
25041 fp->uf_tml_idx = -1;
25042 }
25043}
25044#endif
25045
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046/*
25047 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025048 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 */
25050 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025051func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052{
Bram Moolenaar33570922005-01-25 22:26:29 +000025053 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054
25055 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25056 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025057 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058 || fcp->returned);
25059}
25060
25061/*
25062 * return TRUE if cookie indicates a function which "abort"s on errors.
25063 */
25064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025065func_has_abort(
25066 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025068 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069}
25070
25071#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25072typedef enum
25073{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025074 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25075 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25076 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077} var_flavour_T;
25078
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025079static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080
25081 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025082var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083{
25084 char_u *p = varname;
25085
25086 if (ASCII_ISUPPER(*p))
25087 {
25088 while (*(++p))
25089 if (ASCII_ISLOWER(*p))
25090 return VAR_FLAVOUR_SESSION;
25091 return VAR_FLAVOUR_VIMINFO;
25092 }
25093 else
25094 return VAR_FLAVOUR_DEFAULT;
25095}
25096#endif
25097
25098#if defined(FEAT_VIMINFO) || defined(PROTO)
25099/*
25100 * Restore global vars that start with a capital from the viminfo file
25101 */
25102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025103read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104{
25105 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025106 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025107 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025108 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109
25110 if (!writing && (find_viminfo_parameter('!') != NULL))
25111 {
25112 tab = vim_strchr(virp->vir_line + 1, '\t');
25113 if (tab != NULL)
25114 {
25115 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025116 switch (*tab)
25117 {
25118 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025119#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025120 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025121#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025122 case 'D': type = VAR_DICT; break;
25123 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025124 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126
25127 tab = vim_strchr(tab, '\t');
25128 if (tab != NULL)
25129 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025130 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025131 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025132 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025134#ifdef FEAT_FLOAT
25135 else if (type == VAR_FLOAT)
25136 (void)string2float(tab + 1, &tv.vval.v_float);
25137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025139 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025140 if (type == VAR_DICT || type == VAR_LIST)
25141 {
25142 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25143
25144 if (etv == NULL)
25145 /* Failed to parse back the dict or list, use it as a
25146 * string. */
25147 tv.v_type = VAR_STRING;
25148 else
25149 {
25150 vim_free(tv.vval.v_string);
25151 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025152 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025153 }
25154 }
25155
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025156 /* when in a function use global variables */
25157 save_funccal = current_funccal;
25158 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025159 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025160 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025161
25162 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025163 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025164 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25165 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166 }
25167 }
25168 }
25169
25170 return viminfo_readline(virp);
25171}
25172
25173/*
25174 * Write global vars that start with a capital to the viminfo file
25175 */
25176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025177write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178{
Bram Moolenaar33570922005-01-25 22:26:29 +000025179 hashitem_T *hi;
25180 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025181 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025182 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025183 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025184 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025185 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025186
25187 if (find_viminfo_parameter('!') == NULL)
25188 return;
25189
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025190 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025191
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025192 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025193 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025195 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025197 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025198 this_var = HI2DI(hi);
25199 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025201 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025202 {
25203 case VAR_STRING: s = "STR"; break;
25204 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025205 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025206 case VAR_DICT: s = "DIC"; break;
25207 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025208 case VAR_SPECIAL: s = "XPL"; break;
25209
25210 case VAR_UNKNOWN:
25211 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025212 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025213 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025214 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025215 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025216 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025217 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025218 if (p != NULL)
25219 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025220 vim_free(tofree);
25221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025222 }
25223 }
25224}
25225#endif
25226
25227#if defined(FEAT_SESSION) || defined(PROTO)
25228 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025229store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230{
Bram Moolenaar33570922005-01-25 22:26:29 +000025231 hashitem_T *hi;
25232 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025233 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 char_u *p, *t;
25235
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025236 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025237 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025239 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025241 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025242 this_var = HI2DI(hi);
25243 if ((this_var->di_tv.v_type == VAR_NUMBER
25244 || this_var->di_tv.v_type == VAR_STRING)
25245 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025246 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025247 /* Escape special characters with a backslash. Turn a LF and
25248 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025249 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025250 (char_u *)"\\\"\n\r");
25251 if (p == NULL) /* out of memory */
25252 break;
25253 for (t = p; *t != NUL; ++t)
25254 if (*t == '\n')
25255 *t = 'n';
25256 else if (*t == '\r')
25257 *t = 'r';
25258 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025259 this_var->di_key,
25260 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25261 : ' ',
25262 p,
25263 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25264 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025265 || put_eol(fd) == FAIL)
25266 {
25267 vim_free(p);
25268 return FAIL;
25269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270 vim_free(p);
25271 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025272#ifdef FEAT_FLOAT
25273 else if (this_var->di_tv.v_type == VAR_FLOAT
25274 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25275 {
25276 float_T f = this_var->di_tv.vval.v_float;
25277 int sign = ' ';
25278
25279 if (f < 0)
25280 {
25281 f = -f;
25282 sign = '-';
25283 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025284 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025285 this_var->di_key, sign, f) < 0)
25286 || put_eol(fd) == FAIL)
25287 return FAIL;
25288 }
25289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290 }
25291 }
25292 return OK;
25293}
25294#endif
25295
Bram Moolenaar661b1822005-07-28 22:36:45 +000025296/*
25297 * Display script name where an item was last set.
25298 * Should only be invoked when 'verbose' is non-zero.
25299 */
25300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025301last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025302{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025303 char_u *p;
25304
Bram Moolenaar661b1822005-07-28 22:36:45 +000025305 if (scriptID != 0)
25306 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025307 p = home_replace_save(NULL, get_scriptname(scriptID));
25308 if (p != NULL)
25309 {
25310 verbose_enter();
25311 MSG_PUTS(_("\n\tLast set from "));
25312 MSG_PUTS(p);
25313 vim_free(p);
25314 verbose_leave();
25315 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025316 }
25317}
25318
Bram Moolenaard812df62008-11-09 12:46:09 +000025319/*
25320 * List v:oldfiles in a nice way.
25321 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025322 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025323ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025324{
25325 list_T *l = vimvars[VV_OLDFILES].vv_list;
25326 listitem_T *li;
25327 int nr = 0;
25328
25329 if (l == NULL)
25330 msg((char_u *)_("No old files"));
25331 else
25332 {
25333 msg_start();
25334 msg_scroll = TRUE;
25335 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25336 {
25337 msg_outnum((long)++nr);
25338 MSG_PUTS(": ");
25339 msg_outtrans(get_tv_string(&li->li_tv));
25340 msg_putchar('\n');
25341 out_flush(); /* output one line at a time */
25342 ui_breakcheck();
25343 }
25344 /* Assume "got_int" was set to truncate the listing. */
25345 got_int = FALSE;
25346
25347#ifdef FEAT_BROWSE_CMD
25348 if (cmdmod.browse)
25349 {
25350 quit_more = FALSE;
25351 nr = prompt_for_number(FALSE);
25352 msg_starthere();
25353 if (nr > 0)
25354 {
25355 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25356 (long)nr);
25357
25358 if (p != NULL)
25359 {
25360 p = expand_env_save(p);
25361 eap->arg = p;
25362 eap->cmdidx = CMD_edit;
25363 cmdmod.browse = FALSE;
25364 do_exedit(eap, NULL);
25365 vim_free(p);
25366 }
25367 }
25368 }
25369#endif
25370 }
25371}
25372
Bram Moolenaar53744302015-07-17 17:38:22 +020025373/* reset v:option_new, v:option_old and v:option_type */
25374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025375reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025376{
25377 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25378 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25379 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25380}
25381
25382
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383#endif /* FEAT_EVAL */
25384
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025386#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387
25388#ifdef WIN3264
25389/*
25390 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25391 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025392static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25393static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25394static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395
25396/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025397 * Get the short path (8.3) for the filename in "fnamep".
25398 * Only works for a valid file name.
25399 * When the path gets longer "fnamep" is changed and the allocated buffer
25400 * is put in "bufp".
25401 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25402 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403 */
25404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025405get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025406{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025407 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408 char_u *newbuf;
25409
25410 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025411 l = GetShortPathName(*fnamep, *fnamep, len);
25412 if (l > len - 1)
25413 {
25414 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025415 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 newbuf = vim_strnsave(*fnamep, l);
25417 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025418 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419
25420 vim_free(*bufp);
25421 *fnamep = *bufp = newbuf;
25422
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025423 /* Really should always succeed, as the buffer is big enough. */
25424 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425 }
25426
25427 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025428 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429}
25430
25431/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025432 * Get the short path (8.3) for the filename in "fname". The converted
25433 * path is returned in "bufp".
25434 *
25435 * Some of the directories specified in "fname" may not exist. This function
25436 * will shorten the existing directories at the beginning of the path and then
25437 * append the remaining non-existing path.
25438 *
25439 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025440 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025441 * bufp - Pointer to an allocated buffer for the filename.
25442 * fnamelen - Length of the filename pointed to by fname
25443 *
25444 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025445 */
25446 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025447shortpath_for_invalid_fname(
25448 char_u **fname,
25449 char_u **bufp,
25450 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025452 char_u *short_fname, *save_fname, *pbuf_unused;
25453 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025455 int old_len, len;
25456 int new_len, sfx_len;
25457 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458
25459 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025460 old_len = *fnamelen;
25461 save_fname = vim_strnsave(*fname, old_len);
25462 pbuf_unused = NULL;
25463 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025465 endp = save_fname + old_len - 1; /* Find the end of the copy */
25466 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025468 /*
25469 * Try shortening the supplied path till it succeeds by removing one
25470 * directory at a time from the tail of the path.
25471 */
25472 len = 0;
25473 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025475 /* go back one path-separator */
25476 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25477 --endp;
25478 if (endp <= save_fname)
25479 break; /* processed the complete path */
25480
25481 /*
25482 * Replace the path separator with a NUL and try to shorten the
25483 * resulting path.
25484 */
25485 ch = *endp;
25486 *endp = 0;
25487 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025488 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025489 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25490 {
25491 retval = FAIL;
25492 goto theend;
25493 }
25494 *endp = ch; /* preserve the string */
25495
25496 if (len > 0)
25497 break; /* successfully shortened the path */
25498
25499 /* failed to shorten the path. Skip the path separator */
25500 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 }
25502
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025503 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025505 /*
25506 * Succeeded in shortening the path. Now concatenate the shortened
25507 * path with the remaining path at the tail.
25508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025510 /* Compute the length of the new path. */
25511 sfx_len = (int)(save_endp - endp) + 1;
25512 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025514 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025516 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025518 /* There is not enough space in the currently allocated string,
25519 * copy it to a buffer big enough. */
25520 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025522 {
25523 retval = FAIL;
25524 goto theend;
25525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526 }
25527 else
25528 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025529 /* Transfer short_fname to the main buffer (it's big enough),
25530 * unless get_short_pathname() did its work in-place. */
25531 *fname = *bufp = save_fname;
25532 if (short_fname != save_fname)
25533 vim_strncpy(save_fname, short_fname, len);
25534 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025536
25537 /* concat the not-shortened part of the path */
25538 vim_strncpy(*fname + len, endp, sfx_len);
25539 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025541
25542theend:
25543 vim_free(pbuf_unused);
25544 vim_free(save_fname);
25545
25546 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547}
25548
25549/*
25550 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025551 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552 */
25553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025554shortpath_for_partial(
25555 char_u **fnamep,
25556 char_u **bufp,
25557 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558{
25559 int sepcount, len, tflen;
25560 char_u *p;
25561 char_u *pbuf, *tfname;
25562 int hasTilde;
25563
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025564 /* Count up the path separators from the RHS.. so we know which part
25565 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025566 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025567 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 if (vim_ispathsep(*p))
25569 ++sepcount;
25570
25571 /* Need full path first (use expand_env() to remove a "~/") */
25572 hasTilde = (**fnamep == '~');
25573 if (hasTilde)
25574 pbuf = tfname = expand_env_save(*fnamep);
25575 else
25576 pbuf = tfname = FullName_save(*fnamep, FALSE);
25577
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025578 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025579
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025580 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25581 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582
25583 if (len == 0)
25584 {
25585 /* Don't have a valid filename, so shorten the rest of the
25586 * path if we can. This CAN give us invalid 8.3 filenames, but
25587 * there's not a lot of point in guessing what it might be.
25588 */
25589 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025590 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25591 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592 }
25593
25594 /* Count the paths backward to find the beginning of the desired string. */
25595 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025596 {
25597#ifdef FEAT_MBYTE
25598 if (has_mbyte)
25599 p -= mb_head_off(tfname, p);
25600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601 if (vim_ispathsep(*p))
25602 {
25603 if (sepcount == 0 || (hasTilde && sepcount == 1))
25604 break;
25605 else
25606 sepcount --;
25607 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609 if (hasTilde)
25610 {
25611 --p;
25612 if (p >= tfname)
25613 *p = '~';
25614 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616 }
25617 else
25618 ++p;
25619
25620 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25621 vim_free(*bufp);
25622 *fnamelen = (int)STRLEN(p);
25623 *bufp = pbuf;
25624 *fnamep = p;
25625
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025626 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627}
25628#endif /* WIN3264 */
25629
25630/*
25631 * Adjust a filename, according to a string of modifiers.
25632 * *fnamep must be NUL terminated when called. When returning, the length is
25633 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025634 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635 * When there is an error, *fnamep is set to NULL.
25636 */
25637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025638modify_fname(
25639 char_u *src, /* string with modifiers */
25640 int *usedlen, /* characters after src that are used */
25641 char_u **fnamep, /* file name so far */
25642 char_u **bufp, /* buffer for allocated file name or NULL */
25643 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025644{
25645 int valid = 0;
25646 char_u *tail;
25647 char_u *s, *p, *pbuf;
25648 char_u dirname[MAXPATHL];
25649 int c;
25650 int has_fullname = 0;
25651#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025652 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653 int has_shortname = 0;
25654#endif
25655
25656repeat:
25657 /* ":p" - full path/file_name */
25658 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25659 {
25660 has_fullname = 1;
25661
25662 valid |= VALID_PATH;
25663 *usedlen += 2;
25664
25665 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25666 if ((*fnamep)[0] == '~'
25667#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25668 && ((*fnamep)[1] == '/'
25669# ifdef BACKSLASH_IN_FILENAME
25670 || (*fnamep)[1] == '\\'
25671# endif
25672 || (*fnamep)[1] == NUL)
25673
25674#endif
25675 )
25676 {
25677 *fnamep = expand_env_save(*fnamep);
25678 vim_free(*bufp); /* free any allocated file name */
25679 *bufp = *fnamep;
25680 if (*fnamep == NULL)
25681 return -1;
25682 }
25683
25684 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025685 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686 {
25687 if (vim_ispathsep(*p)
25688 && p[1] == '.'
25689 && (p[2] == NUL
25690 || vim_ispathsep(p[2])
25691 || (p[2] == '.'
25692 && (p[3] == NUL || vim_ispathsep(p[3])))))
25693 break;
25694 }
25695
25696 /* FullName_save() is slow, don't use it when not needed. */
25697 if (*p != NUL || !vim_isAbsName(*fnamep))
25698 {
25699 *fnamep = FullName_save(*fnamep, *p != NUL);
25700 vim_free(*bufp); /* free any allocated file name */
25701 *bufp = *fnamep;
25702 if (*fnamep == NULL)
25703 return -1;
25704 }
25705
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025706#ifdef WIN3264
25707# if _WIN32_WINNT >= 0x0500
25708 if (vim_strchr(*fnamep, '~') != NULL)
25709 {
25710 /* Expand 8.3 filename to full path. Needed to make sure the same
25711 * file does not have two different names.
25712 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25713 p = alloc(_MAX_PATH + 1);
25714 if (p != NULL)
25715 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025716 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025717 {
25718 vim_free(*bufp);
25719 *bufp = *fnamep = p;
25720 }
25721 else
25722 vim_free(p);
25723 }
25724 }
25725# endif
25726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025727 /* Append a path separator to a directory. */
25728 if (mch_isdir(*fnamep))
25729 {
25730 /* Make room for one or two extra characters. */
25731 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25732 vim_free(*bufp); /* free any allocated file name */
25733 *bufp = *fnamep;
25734 if (*fnamep == NULL)
25735 return -1;
25736 add_pathsep(*fnamep);
25737 }
25738 }
25739
25740 /* ":." - path relative to the current directory */
25741 /* ":~" - path relative to the home directory */
25742 /* ":8" - shortname path - postponed till after */
25743 while (src[*usedlen] == ':'
25744 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25745 {
25746 *usedlen += 2;
25747 if (c == '8')
25748 {
25749#ifdef WIN3264
25750 has_shortname = 1; /* Postpone this. */
25751#endif
25752 continue;
25753 }
25754 pbuf = NULL;
25755 /* Need full path first (use expand_env() to remove a "~/") */
25756 if (!has_fullname)
25757 {
25758 if (c == '.' && **fnamep == '~')
25759 p = pbuf = expand_env_save(*fnamep);
25760 else
25761 p = pbuf = FullName_save(*fnamep, FALSE);
25762 }
25763 else
25764 p = *fnamep;
25765
25766 has_fullname = 0;
25767
25768 if (p != NULL)
25769 {
25770 if (c == '.')
25771 {
25772 mch_dirname(dirname, MAXPATHL);
25773 s = shorten_fname(p, dirname);
25774 if (s != NULL)
25775 {
25776 *fnamep = s;
25777 if (pbuf != NULL)
25778 {
25779 vim_free(*bufp); /* free any allocated file name */
25780 *bufp = pbuf;
25781 pbuf = NULL;
25782 }
25783 }
25784 }
25785 else
25786 {
25787 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25788 /* Only replace it when it starts with '~' */
25789 if (*dirname == '~')
25790 {
25791 s = vim_strsave(dirname);
25792 if (s != NULL)
25793 {
25794 *fnamep = s;
25795 vim_free(*bufp);
25796 *bufp = s;
25797 }
25798 }
25799 }
25800 vim_free(pbuf);
25801 }
25802 }
25803
25804 tail = gettail(*fnamep);
25805 *fnamelen = (int)STRLEN(*fnamep);
25806
25807 /* ":h" - head, remove "/file_name", can be repeated */
25808 /* Don't remove the first "/" or "c:\" */
25809 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25810 {
25811 valid |= VALID_HEAD;
25812 *usedlen += 2;
25813 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025814 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025815 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816 *fnamelen = (int)(tail - *fnamep);
25817#ifdef VMS
25818 if (*fnamelen > 0)
25819 *fnamelen += 1; /* the path separator is part of the path */
25820#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025821 if (*fnamelen == 0)
25822 {
25823 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25824 p = vim_strsave((char_u *)".");
25825 if (p == NULL)
25826 return -1;
25827 vim_free(*bufp);
25828 *bufp = *fnamep = tail = p;
25829 *fnamelen = 1;
25830 }
25831 else
25832 {
25833 while (tail > s && !after_pathsep(s, tail))
25834 mb_ptr_back(*fnamep, tail);
25835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 }
25837
25838 /* ":8" - shortname */
25839 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25840 {
25841 *usedlen += 2;
25842#ifdef WIN3264
25843 has_shortname = 1;
25844#endif
25845 }
25846
25847#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025848 /*
25849 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025850 */
25851 if (has_shortname)
25852 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025853 /* Copy the string if it is shortened by :h and when it wasn't copied
25854 * yet, because we are going to change it in place. Avoids changing
25855 * the buffer name for "%:8". */
25856 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025857 {
25858 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025859 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860 return -1;
25861 vim_free(*bufp);
25862 *bufp = *fnamep = p;
25863 }
25864
25865 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025866 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867 if (!has_fullname && !vim_isAbsName(*fnamep))
25868 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025869 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870 return -1;
25871 }
25872 else
25873 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025874 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875
Bram Moolenaardc935552011-08-17 15:23:23 +020025876 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025877 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025878 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025879 return -1;
25880
25881 if (l == 0)
25882 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025883 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025885 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025886 return -1;
25887 }
25888 *fnamelen = l;
25889 }
25890 }
25891#endif /* WIN3264 */
25892
25893 /* ":t" - tail, just the basename */
25894 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25895 {
25896 *usedlen += 2;
25897 *fnamelen -= (int)(tail - *fnamep);
25898 *fnamep = tail;
25899 }
25900
25901 /* ":e" - extension, can be repeated */
25902 /* ":r" - root, without extension, can be repeated */
25903 while (src[*usedlen] == ':'
25904 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25905 {
25906 /* find a '.' in the tail:
25907 * - for second :e: before the current fname
25908 * - otherwise: The last '.'
25909 */
25910 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25911 s = *fnamep - 2;
25912 else
25913 s = *fnamep + *fnamelen - 1;
25914 for ( ; s > tail; --s)
25915 if (s[0] == '.')
25916 break;
25917 if (src[*usedlen + 1] == 'e') /* :e */
25918 {
25919 if (s > tail)
25920 {
25921 *fnamelen += (int)(*fnamep - (s + 1));
25922 *fnamep = s + 1;
25923#ifdef VMS
25924 /* cut version from the extension */
25925 s = *fnamep + *fnamelen - 1;
25926 for ( ; s > *fnamep; --s)
25927 if (s[0] == ';')
25928 break;
25929 if (s > *fnamep)
25930 *fnamelen = s - *fnamep;
25931#endif
25932 }
25933 else if (*fnamep <= tail)
25934 *fnamelen = 0;
25935 }
25936 else /* :r */
25937 {
25938 if (s > tail) /* remove one extension */
25939 *fnamelen = (int)(s - *fnamep);
25940 }
25941 *usedlen += 2;
25942 }
25943
25944 /* ":s?pat?foo?" - substitute */
25945 /* ":gs?pat?foo?" - global substitute */
25946 if (src[*usedlen] == ':'
25947 && (src[*usedlen + 1] == 's'
25948 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25949 {
25950 char_u *str;
25951 char_u *pat;
25952 char_u *sub;
25953 int sep;
25954 char_u *flags;
25955 int didit = FALSE;
25956
25957 flags = (char_u *)"";
25958 s = src + *usedlen + 2;
25959 if (src[*usedlen + 1] == 'g')
25960 {
25961 flags = (char_u *)"g";
25962 ++s;
25963 }
25964
25965 sep = *s++;
25966 if (sep)
25967 {
25968 /* find end of pattern */
25969 p = vim_strchr(s, sep);
25970 if (p != NULL)
25971 {
25972 pat = vim_strnsave(s, (int)(p - s));
25973 if (pat != NULL)
25974 {
25975 s = p + 1;
25976 /* find end of substitution */
25977 p = vim_strchr(s, sep);
25978 if (p != NULL)
25979 {
25980 sub = vim_strnsave(s, (int)(p - s));
25981 str = vim_strnsave(*fnamep, *fnamelen);
25982 if (sub != NULL && str != NULL)
25983 {
25984 *usedlen = (int)(p + 1 - src);
25985 s = do_string_sub(str, pat, sub, flags);
25986 if (s != NULL)
25987 {
25988 *fnamep = s;
25989 *fnamelen = (int)STRLEN(s);
25990 vim_free(*bufp);
25991 *bufp = s;
25992 didit = TRUE;
25993 }
25994 }
25995 vim_free(sub);
25996 vim_free(str);
25997 }
25998 vim_free(pat);
25999 }
26000 }
26001 /* after using ":s", repeat all the modifiers */
26002 if (didit)
26003 goto repeat;
26004 }
26005 }
26006
Bram Moolenaar26df0922014-02-23 23:39:13 +010026007 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26008 {
26009 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26010 if (p == NULL)
26011 return -1;
26012 vim_free(*bufp);
26013 *bufp = *fnamep = p;
26014 *fnamelen = (int)STRLEN(p);
26015 *usedlen += 2;
26016 }
26017
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018 return valid;
26019}
26020
26021/*
26022 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26023 * "flags" can be "g" to do a global substitute.
26024 * Returns an allocated string, NULL for error.
26025 */
26026 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026027do_string_sub(
26028 char_u *str,
26029 char_u *pat,
26030 char_u *sub,
26031 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026032{
26033 int sublen;
26034 regmatch_T regmatch;
26035 int i;
26036 int do_all;
26037 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026038 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026039 garray_T ga;
26040 char_u *ret;
26041 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026042 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043
26044 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26045 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026046 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026047
26048 ga_init2(&ga, 1, 200);
26049
26050 do_all = (flags[0] == 'g');
26051
26052 regmatch.rm_ic = p_ic;
26053 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26054 if (regmatch.regprog != NULL)
26055 {
26056 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026057 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26059 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026060 /* Skip empty match except for first match. */
26061 if (regmatch.startp[0] == regmatch.endp[0])
26062 {
26063 if (zero_width == regmatch.startp[0])
26064 {
26065 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026066 i = MB_PTR2LEN(tail);
26067 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26068 (size_t)i);
26069 ga.ga_len += i;
26070 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026071 continue;
26072 }
26073 zero_width = regmatch.startp[0];
26074 }
26075
Bram Moolenaar071d4272004-06-13 20:20:40 +000026076 /*
26077 * Get some space for a temporary buffer to do the substitution
26078 * into. It will contain:
26079 * - The text up to where the match is.
26080 * - The substituted text.
26081 * - The text after the match.
26082 */
26083 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026084 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26086 {
26087 ga_clear(&ga);
26088 break;
26089 }
26090
26091 /* copy the text up to where the match is */
26092 i = (int)(regmatch.startp[0] - tail);
26093 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26094 /* add the substituted text */
26095 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26096 + ga.ga_len + i, TRUE, TRUE, FALSE);
26097 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026098 tail = regmatch.endp[0];
26099 if (*tail == NUL)
26100 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026101 if (!do_all)
26102 break;
26103 }
26104
26105 if (ga.ga_data != NULL)
26106 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26107
Bram Moolenaar473de612013-06-08 18:19:48 +020026108 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026109 }
26110
26111 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26112 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026113 if (p_cpo == empty_option)
26114 p_cpo = save_cpo;
26115 else
26116 /* Darn, evaluating {sub} expression changed the value. */
26117 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118
26119 return ret;
26120}
26121
26122#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */