blob: c2d18aa8694240cc1adadf918ff5ea0f4b546466 [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
506static void f_ch_open(typval_T *argvars, typval_T *rettv);
507static void f_ch_close(typval_T *argvars, typval_T *rettv);
508static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
509static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
510#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100511static void f_changenr(typval_T *argvars, typval_T *rettv);
512static void f_char2nr(typval_T *argvars, typval_T *rettv);
513static void f_cindent(typval_T *argvars, typval_T *rettv);
514static void f_clearmatches(typval_T *argvars, typval_T *rettv);
515static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000516#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_complete(typval_T *argvars, typval_T *rettv);
518static void f_complete_add(typval_T *argvars, typval_T *rettv);
519static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_confirm(typval_T *argvars, typval_T *rettv);
522static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_cos(typval_T *argvars, typval_T *rettv);
525static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_count(typval_T *argvars, typval_T *rettv);
528static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
529static void f_cursor(typval_T *argsvars, typval_T *rettv);
530static void f_deepcopy(typval_T *argvars, typval_T *rettv);
531static void f_delete(typval_T *argvars, typval_T *rettv);
532static void f_did_filetype(typval_T *argvars, typval_T *rettv);
533static void f_diff_filler(typval_T *argvars, typval_T *rettv);
534static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
535static void f_empty(typval_T *argvars, typval_T *rettv);
536static void f_escape(typval_T *argvars, typval_T *rettv);
537static void f_eval(typval_T *argvars, typval_T *rettv);
538static void f_eventhandler(typval_T *argvars, typval_T *rettv);
539static void f_executable(typval_T *argvars, typval_T *rettv);
540static void f_exepath(typval_T *argvars, typval_T *rettv);
541static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200542#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100543static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200544#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100545static void f_expand(typval_T *argvars, typval_T *rettv);
546static void f_extend(typval_T *argvars, typval_T *rettv);
547static void f_feedkeys(typval_T *argvars, typval_T *rettv);
548static void f_filereadable(typval_T *argvars, typval_T *rettv);
549static void f_filewritable(typval_T *argvars, typval_T *rettv);
550static void f_filter(typval_T *argvars, typval_T *rettv);
551static void f_finddir(typval_T *argvars, typval_T *rettv);
552static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000553#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100554static void f_float2nr(typval_T *argvars, typval_T *rettv);
555static void f_floor(typval_T *argvars, typval_T *rettv);
556static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_fnameescape(typval_T *argvars, typval_T *rettv);
559static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
560static void f_foldclosed(typval_T *argvars, typval_T *rettv);
561static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
562static void f_foldlevel(typval_T *argvars, typval_T *rettv);
563static void f_foldtext(typval_T *argvars, typval_T *rettv);
564static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
565static void f_foreground(typval_T *argvars, typval_T *rettv);
566static void f_function(typval_T *argvars, typval_T *rettv);
567static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
568static void f_get(typval_T *argvars, typval_T *rettv);
569static void f_getbufline(typval_T *argvars, typval_T *rettv);
570static void f_getbufvar(typval_T *argvars, typval_T *rettv);
571static void f_getchar(typval_T *argvars, typval_T *rettv);
572static void f_getcharmod(typval_T *argvars, typval_T *rettv);
573static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
574static void f_getcmdline(typval_T *argvars, typval_T *rettv);
575static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
576static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
577static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
578static void f_getcwd(typval_T *argvars, typval_T *rettv);
579static void f_getfontname(typval_T *argvars, typval_T *rettv);
580static void f_getfperm(typval_T *argvars, typval_T *rettv);
581static void f_getfsize(typval_T *argvars, typval_T *rettv);
582static void f_getftime(typval_T *argvars, typval_T *rettv);
583static void f_getftype(typval_T *argvars, typval_T *rettv);
584static void f_getline(typval_T *argvars, typval_T *rettv);
585static void f_getmatches(typval_T *argvars, typval_T *rettv);
586static void f_getpid(typval_T *argvars, typval_T *rettv);
587static void f_getcurpos(typval_T *argvars, typval_T *rettv);
588static void f_getpos(typval_T *argvars, typval_T *rettv);
589static void f_getqflist(typval_T *argvars, typval_T *rettv);
590static void f_getreg(typval_T *argvars, typval_T *rettv);
591static void f_getregtype(typval_T *argvars, typval_T *rettv);
592static void f_gettabvar(typval_T *argvars, typval_T *rettv);
593static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
594static void f_getwinposx(typval_T *argvars, typval_T *rettv);
595static void f_getwinposy(typval_T *argvars, typval_T *rettv);
596static void f_getwinvar(typval_T *argvars, typval_T *rettv);
597static void f_glob(typval_T *argvars, typval_T *rettv);
598static void f_globpath(typval_T *argvars, typval_T *rettv);
599static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
600static void f_has(typval_T *argvars, typval_T *rettv);
601static void f_has_key(typval_T *argvars, typval_T *rettv);
602static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
603static void f_hasmapto(typval_T *argvars, typval_T *rettv);
604static void f_histadd(typval_T *argvars, typval_T *rettv);
605static void f_histdel(typval_T *argvars, typval_T *rettv);
606static void f_histget(typval_T *argvars, typval_T *rettv);
607static void f_histnr(typval_T *argvars, typval_T *rettv);
608static void f_hlID(typval_T *argvars, typval_T *rettv);
609static void f_hlexists(typval_T *argvars, typval_T *rettv);
610static void f_hostname(typval_T *argvars, typval_T *rettv);
611static void f_iconv(typval_T *argvars, typval_T *rettv);
612static void f_indent(typval_T *argvars, typval_T *rettv);
613static void f_index(typval_T *argvars, typval_T *rettv);
614static void f_input(typval_T *argvars, typval_T *rettv);
615static void f_inputdialog(typval_T *argvars, typval_T *rettv);
616static void f_inputlist(typval_T *argvars, typval_T *rettv);
617static void f_inputrestore(typval_T *argvars, typval_T *rettv);
618static void f_inputsave(typval_T *argvars, typval_T *rettv);
619static void f_inputsecret(typval_T *argvars, typval_T *rettv);
620static void f_insert(typval_T *argvars, typval_T *rettv);
621static void f_invert(typval_T *argvars, typval_T *rettv);
622static void f_isdirectory(typval_T *argvars, typval_T *rettv);
623static void f_islocked(typval_T *argvars, typval_T *rettv);
624static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100625#ifdef FEAT_JOB
626static void f_job_start(typval_T *argvars, typval_T *rettv);
627static void f_job_stop(typval_T *argvars, typval_T *rettv);
628static void f_job_status(typval_T *argvars, typval_T *rettv);
629#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100630static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100631static void f_jsdecode(typval_T *argvars, typval_T *rettv);
632static void f_jsencode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100633static void f_jsondecode(typval_T *argvars, typval_T *rettv);
634static void f_jsonencode(typval_T *argvars, typval_T *rettv);
635static void f_keys(typval_T *argvars, typval_T *rettv);
636static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
637static void f_len(typval_T *argvars, typval_T *rettv);
638static void f_libcall(typval_T *argvars, typval_T *rettv);
639static void f_libcallnr(typval_T *argvars, typval_T *rettv);
640static void f_line(typval_T *argvars, typval_T *rettv);
641static void f_line2byte(typval_T *argvars, typval_T *rettv);
642static void f_lispindent(typval_T *argvars, typval_T *rettv);
643static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000644#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_log(typval_T *argvars, typval_T *rettv);
646static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000647#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200648#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200650#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_map(typval_T *argvars, typval_T *rettv);
652static void f_maparg(typval_T *argvars, typval_T *rettv);
653static void f_mapcheck(typval_T *argvars, typval_T *rettv);
654static void f_match(typval_T *argvars, typval_T *rettv);
655static void f_matchadd(typval_T *argvars, typval_T *rettv);
656static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
657static void f_matcharg(typval_T *argvars, typval_T *rettv);
658static void f_matchdelete(typval_T *argvars, typval_T *rettv);
659static void f_matchend(typval_T *argvars, typval_T *rettv);
660static void f_matchlist(typval_T *argvars, typval_T *rettv);
661static void f_matchstr(typval_T *argvars, typval_T *rettv);
662static void f_max(typval_T *argvars, typval_T *rettv);
663static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000664#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000666#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100668#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
672static void f_nr2char(typval_T *argvars, typval_T *rettv);
673static void f_or(typval_T *argvars, typval_T *rettv);
674static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100675#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100677#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000678#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
682static void f_printf(typval_T *argvars, typval_T *rettv);
683static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200684#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200686#endif
687#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_range(typval_T *argvars, typval_T *rettv);
691static void f_readfile(typval_T *argvars, typval_T *rettv);
692static void f_reltime(typval_T *argvars, typval_T *rettv);
693static void f_reltimestr(typval_T *argvars, typval_T *rettv);
694static void f_remote_expr(typval_T *argvars, typval_T *rettv);
695static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
696static void f_remote_peek(typval_T *argvars, typval_T *rettv);
697static void f_remote_read(typval_T *argvars, typval_T *rettv);
698static void f_remote_send(typval_T *argvars, typval_T *rettv);
699static void f_remove(typval_T *argvars, typval_T *rettv);
700static void f_rename(typval_T *argvars, typval_T *rettv);
701static void f_repeat(typval_T *argvars, typval_T *rettv);
702static void f_resolve(typval_T *argvars, typval_T *rettv);
703static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_screenattr(typval_T *argvars, typval_T *rettv);
708static void f_screenchar(typval_T *argvars, typval_T *rettv);
709static void f_screencol(typval_T *argvars, typval_T *rettv);
710static void f_screenrow(typval_T *argvars, typval_T *rettv);
711static void f_search(typval_T *argvars, typval_T *rettv);
712static void f_searchdecl(typval_T *argvars, typval_T *rettv);
713static void f_searchpair(typval_T *argvars, typval_T *rettv);
714static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
715static void f_searchpos(typval_T *argvars, typval_T *rettv);
716static void f_server2client(typval_T *argvars, typval_T *rettv);
717static void f_serverlist(typval_T *argvars, typval_T *rettv);
718static void f_setbufvar(typval_T *argvars, typval_T *rettv);
719static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
720static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
721static void f_setline(typval_T *argvars, typval_T *rettv);
722static void f_setloclist(typval_T *argvars, typval_T *rettv);
723static void f_setmatches(typval_T *argvars, typval_T *rettv);
724static void f_setpos(typval_T *argvars, typval_T *rettv);
725static void f_setqflist(typval_T *argvars, typval_T *rettv);
726static void f_setreg(typval_T *argvars, typval_T *rettv);
727static void f_settabvar(typval_T *argvars, typval_T *rettv);
728static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
729static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100730#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100732#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100733static void f_shellescape(typval_T *argvars, typval_T *rettv);
734static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
735static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000736#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100737static void f_sin(typval_T *argvars, typval_T *rettv);
738static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100740static void f_sort(typval_T *argvars, typval_T *rettv);
741static void f_soundfold(typval_T *argvars, typval_T *rettv);
742static void f_spellbadword(typval_T *argvars, typval_T *rettv);
743static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
744static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000745#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100746static void f_sqrt(typval_T *argvars, typval_T *rettv);
747static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000748#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_str2nr(typval_T *argvars, typval_T *rettv);
750static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000751#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000753#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_stridx(typval_T *argvars, typval_T *rettv);
755static void f_string(typval_T *argvars, typval_T *rettv);
756static void f_strlen(typval_T *argvars, typval_T *rettv);
757static void f_strpart(typval_T *argvars, typval_T *rettv);
758static void f_strridx(typval_T *argvars, typval_T *rettv);
759static void f_strtrans(typval_T *argvars, typval_T *rettv);
760static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
761static void f_strwidth(typval_T *argvars, typval_T *rettv);
762static void f_submatch(typval_T *argvars, typval_T *rettv);
763static void f_substitute(typval_T *argvars, typval_T *rettv);
764static void f_synID(typval_T *argvars, typval_T *rettv);
765static void f_synIDattr(typval_T *argvars, typval_T *rettv);
766static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
767static void f_synstack(typval_T *argvars, typval_T *rettv);
768static void f_synconcealed(typval_T *argvars, typval_T *rettv);
769static void f_system(typval_T *argvars, typval_T *rettv);
770static void f_systemlist(typval_T *argvars, typval_T *rettv);
771static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
772static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
773static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
774static void f_taglist(typval_T *argvars, typval_T *rettv);
775static void f_tagfiles(typval_T *argvars, typval_T *rettv);
776static void f_tempname(typval_T *argvars, typval_T *rettv);
777static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200778#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_tan(typval_T *argvars, typval_T *rettv);
780static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200781#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_tolower(typval_T *argvars, typval_T *rettv);
783static void f_toupper(typval_T *argvars, typval_T *rettv);
784static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000785#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000787#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100788static void f_type(typval_T *argvars, typval_T *rettv);
789static void f_undofile(typval_T *argvars, typval_T *rettv);
790static void f_undotree(typval_T *argvars, typval_T *rettv);
791static void f_uniq(typval_T *argvars, typval_T *rettv);
792static void f_values(typval_T *argvars, typval_T *rettv);
793static void f_virtcol(typval_T *argvars, typval_T *rettv);
794static void f_visualmode(typval_T *argvars, typval_T *rettv);
795static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
796static void f_winbufnr(typval_T *argvars, typval_T *rettv);
797static void f_wincol(typval_T *argvars, typval_T *rettv);
798static void f_winheight(typval_T *argvars, typval_T *rettv);
799static void f_winline(typval_T *argvars, typval_T *rettv);
800static void f_winnr(typval_T *argvars, typval_T *rettv);
801static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
802static void f_winrestview(typval_T *argvars, typval_T *rettv);
803static void f_winsaveview(typval_T *argvars, typval_T *rettv);
804static void f_winwidth(typval_T *argvars, typval_T *rettv);
805static void f_writefile(typval_T *argvars, typval_T *rettv);
806static void f_wordcount(typval_T *argvars, typval_T *rettv);
807static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000808
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100809static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
810static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
811static int get_env_len(char_u **arg);
812static int get_id_len(char_u **arg);
813static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
814static 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 +0000815#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
816#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
817 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100818static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
819static int eval_isnamec(int c);
820static int eval_isnamec1(int c);
821static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
822static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100823static typval_T *alloc_string_tv(char_u *string);
824static void init_tv(typval_T *varp);
825static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100826#ifdef FEAT_FLOAT
827static float_T get_tv_float(typval_T *varp);
828#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100829static linenr_T get_tv_lnum(typval_T *argvars);
830static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
831static char_u *get_tv_string(typval_T *varp);
832static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
833static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
834static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
835static hashtab_T *find_var_ht(char_u *name, char_u **varname);
836static funccall_T *get_funccal(void);
837static void vars_clear_ext(hashtab_T *ht, int free_val);
838static void delete_var(hashtab_T *ht, hashitem_T *hi);
839static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
840static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
841static void set_var(char_u *name, typval_T *varp, int copy);
842static int var_check_ro(int flags, char_u *name, int use_gettext);
843static int var_check_fixed(int flags, char_u *name, int use_gettext);
844static int var_check_func_name(char_u *name, int new_var);
845static int valid_varname(char_u *varname);
846static int tv_check_lock(int lock, char_u *name, int use_gettext);
847static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
848static char_u *find_option_end(char_u **arg, int *opt_flags);
849static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
850static int eval_fname_script(char_u *p);
851static int eval_fname_sid(char_u *p);
852static void list_func_head(ufunc_T *fp, int indent);
853static ufunc_T *find_func(char_u *name);
854static int function_exists(char_u *name);
855static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100857static void func_do_profile(ufunc_T *fp);
858static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
859static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000860static int
861# ifdef __BORLANDC__
862 _RTLENTRYF
863# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000865static int
866# ifdef __BORLANDC__
867 _RTLENTRYF
868# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000870#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static int script_autoload(char_u *name, int reload);
872static char_u *autoload_name(char_u *name);
873static void cat_func_name(char_u *buf, ufunc_T *fp);
874static void func_free(ufunc_T *fp);
875static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
876static int can_free_funccal(funccall_T *fc, int copyID) ;
877static void free_funccal(funccall_T *fc, int free_val);
878static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
879static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
880static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
881static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
882static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
883static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
884static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
885static int write_list(FILE *fd, list_T *list, int binary);
886static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000887
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200888
889#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100890static int compare_func_name(const void *s1, const void *s2);
891static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892#endif
893
Bram Moolenaar33570922005-01-25 22:26:29 +0000894/*
895 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000896 */
897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100898eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000899{
Bram Moolenaar33570922005-01-25 22:26:29 +0000900 int i;
901 struct vimvar *p;
902
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200903 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
904 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200905 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000906 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000907 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000908
909 for (i = 0; i < VV_LEN; ++i)
910 {
911 p = &vimvars[i];
912 STRCPY(p->vv_di.di_key, p->vv_name);
913 if (p->vv_flags & VV_RO)
914 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
915 else if (p->vv_flags & VV_RO_SBX)
916 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
917 else
918 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000919
920 /* add to v: scope dict, unless the value is not always available */
921 if (p->vv_type != VAR_UNKNOWN)
922 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000923 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000924 /* add to compat scope dict */
925 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000926 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100927 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
928
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000929 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100930 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200931 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100932 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100933
934 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
935 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
936 set_vim_var_nr(VV_NONE, VVAL_NONE);
937 set_vim_var_nr(VV_NULL, VVAL_NULL);
938
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200939 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200940
941#ifdef EBCDIC
942 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100943 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200944 */
945 sortFunctions();
946#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000947}
948
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000949#if defined(EXITFREE) || defined(PROTO)
950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100951eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952{
953 int i;
954 struct vimvar *p;
955
956 for (i = 0; i < VV_LEN; ++i)
957 {
958 p = &vimvars[i];
959 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000960 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000961 vim_free(p->vv_str);
962 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000963 }
964 else if (p->vv_di.di_tv.v_type == VAR_LIST)
965 {
966 list_unref(p->vv_list);
967 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000968 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969 }
970 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000971 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972 hash_clear(&compat_hashtab);
973
Bram Moolenaard9fba312005-06-26 22:34:35 +0000974 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100975# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200976 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100977# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000978
979 /* global variables */
980 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000981
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000982 /* autoloaded script names */
983 ga_clear_strings(&ga_loaded);
984
Bram Moolenaarcca74132013-09-25 21:00:28 +0200985 /* Script-local variables. First clear all the variables and in a second
986 * loop free the scriptvar_T, because a variable in one script might hold
987 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200988 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200989 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200990 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200991 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200992 ga_clear(&ga_scripts);
993
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000994 /* unreferenced lists and dicts */
995 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000996
997 /* functions */
998 free_all_functions();
999 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001000}
1001#endif
1002
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 * Return the name of the executed function.
1005 */
1006 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001007func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010}
1011
1012/*
1013 * Return the address holding the next breakpoint line for a funccall cookie.
1014 */
1015 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001016func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017{
Bram Moolenaar33570922005-01-25 22:26:29 +00001018 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019}
1020
1021/*
1022 * Return the address holding the debug tick for a funccall cookie.
1023 */
1024 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001025func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
Bram Moolenaar33570922005-01-25 22:26:29 +00001027 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028}
1029
1030/*
1031 * Return the nesting level for a funccall cookie.
1032 */
1033 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001034func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
Bram Moolenaar33570922005-01-25 22:26:29 +00001036 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037}
1038
1039/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001040funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001042/* pointer to list of previously used funccal, still around because some
1043 * item in it is still being used. */
1044funccall_T *previous_funccal = NULL;
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046/*
1047 * Return TRUE when a function was ended by a ":return" command.
1048 */
1049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001050current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052 return current_funccal->returned;
1053}
1054
1055
1056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 * Set an internal variable to a string value. Creates the variable if it does
1058 * not already exist.
1059 */
1060 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001061set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001063 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001064 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
1066 val = vim_strsave(value);
1067 if (val != NULL)
1068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001069 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001070 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001072 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001073 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
1075 }
1076}
1077
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001079static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080static char_u *redir_endp = NULL;
1081static char_u *redir_varname = NULL;
1082
1083/*
1084 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001085 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 * Returns OK if successfully completed the setup. FAIL otherwise.
1087 */
1088 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001089var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090{
1091 int save_emsg;
1092 int err;
1093 typval_T tv;
1094
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001095 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001096 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 {
1098 EMSG(_(e_invarg));
1099 return FAIL;
1100 }
1101
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001102 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 redir_varname = vim_strsave(name);
1104 if (redir_varname == NULL)
1105 return FAIL;
1106
1107 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1108 if (redir_lval == NULL)
1109 {
1110 var_redir_stop();
1111 return FAIL;
1112 }
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 /* The output is stored in growarray "redir_ga" until redirection ends. */
1115 ga_init2(&redir_ga, (int)sizeof(char), 500);
1116
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001118 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001119 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1121 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001122 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 if (redir_endp != NULL && *redir_endp != NUL)
1124 /* Trailing characters are present after the variable name */
1125 EMSG(_(e_trailing));
1126 else
1127 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001128 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 var_redir_stop();
1130 return FAIL;
1131 }
1132
1133 /* check if we can write to the variable: set it to or append an empty
1134 * string */
1135 save_emsg = did_emsg;
1136 did_emsg = FALSE;
1137 tv.v_type = VAR_STRING;
1138 tv.vval.v_string = (char_u *)"";
1139 if (append)
1140 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1141 else
1142 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001143 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001145 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 if (err)
1147 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 var_redir_stop();
1150 return FAIL;
1151 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152
1153 return OK;
1154}
1155
1156/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157 * Append "value[value_len]" to the variable set by var_redir_start().
1158 * The actual appending is postponed until redirection ends, because the value
1159 * appended may in fact be the string we write to, changing it may cause freed
1160 * memory to be used:
1161 * :redir => foo
1162 * :let foo
1163 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 */
1165 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001166var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001168 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169
1170 if (redir_lval == NULL)
1171 return;
1172
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001174 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001176 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001178 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001179 {
1180 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001181 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001182 }
1183 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185}
1186
1187/*
1188 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001189 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190 */
1191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001192var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194 typval_T tv;
1195
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 if (redir_lval != NULL)
1197 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001198 /* If there was no error: assign the text to the variable. */
1199 if (redir_endp != NULL)
1200 {
1201 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1202 tv.v_type = VAR_STRING;
1203 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001204 /* Call get_lval() again, if it's inside a Dict or List it may
1205 * have changed. */
1206 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001207 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001208 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1209 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1210 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001211 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001212
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001213 /* free the collected output */
1214 vim_free(redir_ga.ga_data);
1215 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217 vim_free(redir_lval);
1218 redir_lval = NULL;
1219 }
1220 vim_free(redir_varname);
1221 redir_varname = NULL;
1222}
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224# if defined(FEAT_MBYTE) || defined(PROTO)
1225 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001226eval_charconvert(
1227 char_u *enc_from,
1228 char_u *enc_to,
1229 char_u *fname_from,
1230 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231{
1232 int err = FALSE;
1233
1234 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1235 set_vim_var_string(VV_CC_TO, enc_to, -1);
1236 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1237 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1238 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1239 err = TRUE;
1240 set_vim_var_string(VV_CC_FROM, NULL, -1);
1241 set_vim_var_string(VV_CC_TO, NULL, -1);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1244
1245 if (err)
1246 return FAIL;
1247 return OK;
1248}
1249# endif
1250
1251# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1252 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001253eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254{
1255 int err = FALSE;
1256
1257 set_vim_var_string(VV_FNAME_IN, fname, -1);
1258 set_vim_var_string(VV_CMDARG, args, -1);
1259 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1260 err = TRUE;
1261 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1262 set_vim_var_string(VV_CMDARG, NULL, -1);
1263
1264 if (err)
1265 {
1266 mch_remove(fname);
1267 return FAIL;
1268 }
1269 return OK;
1270}
1271# endif
1272
1273# if defined(FEAT_DIFF) || defined(PROTO)
1274 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001275eval_diff(
1276 char_u *origfile,
1277 char_u *newfile,
1278 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279{
1280 int err = FALSE;
1281
1282 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1283 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1284 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1285 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1286 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1287 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1288 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1289}
1290
1291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292eval_patch(
1293 char_u *origfile,
1294 char_u *difffile,
1295 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int err;
1298
1299 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1300 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1301 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1302 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1303 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1304 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1305 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1306}
1307# endif
1308
1309/*
1310 * Top level evaluation function, returning a boolean.
1311 * Sets "error" to TRUE if there was an error.
1312 * Return TRUE or FALSE.
1313 */
1314 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001315eval_to_bool(
1316 char_u *arg,
1317 int *error,
1318 char_u **nextcmd,
1319 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
Bram Moolenaar33570922005-01-25 22:26:29 +00001321 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 int retval = FALSE;
1323
1324 if (skip)
1325 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 else
1329 {
1330 *error = FALSE;
1331 if (!skip)
1332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001333 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 }
1337 if (skip)
1338 --emsg_skip;
1339
1340 return retval;
1341}
1342
1343/*
1344 * Top level evaluation function, returning a string. If "skip" is TRUE,
1345 * only parsing to "nextcmd" is done, without reporting errors. Return
1346 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1347 */
1348 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001349eval_to_string_skip(
1350 char_u *arg,
1351 char_u **nextcmd,
1352 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
1356
1357 if (skip)
1358 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 retval = NULL;
1361 else
1362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 retval = vim_strsave(get_tv_string(&tv));
1364 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
1366 if (skip)
1367 --emsg_skip;
1368
1369 return retval;
1370}
1371
1372/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373 * Skip over an expression at "*pp".
1374 * Return FAIL for an error, OK otherwise.
1375 */
1376 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001377skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001378{
Bram Moolenaar33570922005-01-25 22:26:29 +00001379 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001380
1381 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001383}
1384
1385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001387 * When "convert" is TRUE convert a List into a sequence of lines and convert
1388 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 * Return pointer to allocated memory, or NULL for failure.
1390 */
1391 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001392eval_to_string(
1393 char_u *arg,
1394 char_u **nextcmd,
1395 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001400#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001401 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001404 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 retval = NULL;
1406 else
1407 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001408 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 {
1410 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001411 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001412 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001413 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001414 if (tv.vval.v_list->lv_len > 0)
1415 ga_append(&ga, NL);
1416 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417 ga_append(&ga, NUL);
1418 retval = (char_u *)ga.ga_data;
1419 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001420#ifdef FEAT_FLOAT
1421 else if (convert && tv.v_type == VAR_FLOAT)
1422 {
1423 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1424 retval = vim_strsave(numbuf);
1425 }
1426#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001427 else
1428 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431
1432 return retval;
1433}
1434
1435/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001436 * Call eval_to_string() without using current local variables and using
1437 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 */
1439 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001440eval_to_string_safe(
1441 char_u *arg,
1442 char_u **nextcmd,
1443 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444{
1445 char_u *retval;
1446 void *save_funccalp;
1447
1448 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001449 if (use_sandbox)
1450 ++sandbox;
1451 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001452 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001453 if (use_sandbox)
1454 --sandbox;
1455 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 restore_funccal(save_funccalp);
1457 return retval;
1458}
1459
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460/*
1461 * Top level evaluation function, returning a number.
1462 * Evaluates "expr" silently.
1463 * Returns -1 for an error.
1464 */
1465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001466eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
Bram Moolenaar33570922005-01-25 22:26:29 +00001468 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001470 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
1472 ++emsg_off;
1473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001474 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 retval = -1;
1476 else
1477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001478 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001479 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481 --emsg_off;
1482
1483 return retval;
1484}
1485
Bram Moolenaara40058a2005-07-11 22:42:07 +00001486/*
1487 * Prepare v: variable "idx" to be used.
1488 * Save the current typeval in "save_tv".
1489 * When not used yet add the variable to the v: hashtable.
1490 */
1491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001493{
1494 *save_tv = vimvars[idx].vv_tv;
1495 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1496 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1497}
1498
1499/*
1500 * Restore v: variable "idx" to typeval "save_tv".
1501 * When no longer defined, remove the variable from the v: hashtable.
1502 */
1503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001505{
1506 hashitem_T *hi;
1507
Bram Moolenaara40058a2005-07-11 22:42:07 +00001508 vimvars[idx].vv_tv = *save_tv;
1509 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1510 {
1511 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1512 if (HASHITEM_EMPTY(hi))
1513 EMSG2(_(e_intern2), "restore_vimvar()");
1514 else
1515 hash_remove(&vimvarht, hi);
1516 }
1517}
1518
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001519#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001520/*
1521 * Evaluate an expression to a list with suggestions.
1522 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001523 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001524 */
1525 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001526eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001527{
1528 typval_T save_val;
1529 typval_T rettv;
1530 list_T *list = NULL;
1531 char_u *p = skipwhite(expr);
1532
1533 /* Set "v:val" to the bad word. */
1534 prepare_vimvar(VV_VAL, &save_val);
1535 vimvars[VV_VAL].vv_type = VAR_STRING;
1536 vimvars[VV_VAL].vv_str = badword;
1537 if (p_verbose == 0)
1538 ++emsg_off;
1539
1540 if (eval1(&p, &rettv, TRUE) == OK)
1541 {
1542 if (rettv.v_type != VAR_LIST)
1543 clear_tv(&rettv);
1544 else
1545 list = rettv.vval.v_list;
1546 }
1547
1548 if (p_verbose == 0)
1549 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001550 restore_vimvar(VV_VAL, &save_val);
1551
1552 return list;
1553}
1554
1555/*
1556 * "list" is supposed to contain two items: a word and a number. Return the
1557 * word in "pp" and the number as the return value.
1558 * Return -1 if anything isn't right.
1559 * Used to get the good word and score from the eval_spell_expr() result.
1560 */
1561 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001562get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001563{
1564 listitem_T *li;
1565
1566 li = list->lv_first;
1567 if (li == NULL)
1568 return -1;
1569 *pp = get_tv_string(&li->li_tv);
1570
1571 li = li->li_next;
1572 if (li == NULL)
1573 return -1;
1574 return get_tv_number(&li->li_tv);
1575}
1576#endif
1577
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001578/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001579 * Top level evaluation function.
1580 * Returns an allocated typval_T with the result.
1581 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001582 */
1583 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001584eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001585{
1586 typval_T *tv;
1587
1588 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001589 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001590 {
1591 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001592 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001593 }
1594
1595 return tv;
1596}
1597
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001600 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001601 * Uses argv[argc] for the function arguments. Only Number and String
1602 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001606call_vim_function(
1607 char_u *func,
1608 int argc,
1609 char_u **argv,
1610 int safe, /* use the sandbox */
1611 int str_arg_only, /* all arguments are strings */
1612 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
Bram Moolenaar33570922005-01-25 22:26:29 +00001614 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 long n;
1616 int len;
1617 int i;
1618 int doesrange;
1619 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001622 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 for (i = 0; i < argc; i++)
1627 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001628 /* Pass a NULL or empty argument as an empty string */
1629 if (argv[i] == NULL || *argv[i] == NUL)
1630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001631 argvars[i].v_type = VAR_STRING;
1632 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001633 continue;
1634 }
1635
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001636 if (str_arg_only)
1637 len = 0;
1638 else
1639 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001640 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if (len != 0 && len == (int)STRLEN(argv[i]))
1642 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001643 argvars[i].v_type = VAR_NUMBER;
1644 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 }
1646 else
1647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001648 argvars[i].v_type = VAR_STRING;
1649 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
1651 }
1652
1653 if (safe)
1654 {
1655 save_funccalp = save_funccal();
1656 ++sandbox;
1657 }
1658
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1660 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (safe)
1664 {
1665 --sandbox;
1666 restore_funccal(save_funccalp);
1667 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668 vim_free(argvars);
1669
1670 if (ret == FAIL)
1671 clear_tv(rettv);
1672
1673 return ret;
1674}
1675
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001676/*
1677 * Call vimL function "func" and return the result as a number.
1678 * Returns -1 when calling the function fails.
1679 * Uses argv[argc] for the function arguments.
1680 */
1681 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001682call_func_retnr(
1683 char_u *func,
1684 int argc,
1685 char_u **argv,
1686 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001687{
1688 typval_T rettv;
1689 long retval;
1690
1691 /* All arguments are passed as strings, no conversion to number. */
1692 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1693 return -1;
1694
1695 retval = get_tv_number_chk(&rettv, NULL);
1696 clear_tv(&rettv);
1697 return retval;
1698}
1699
1700#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1701 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1702
Bram Moolenaar4f688582007-07-24 12:34:30 +00001703# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001704/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001705 * Call vimL function "func" and return the result as a string.
1706 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 * Uses argv[argc] for the function arguments.
1708 */
1709 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001710call_func_retstr(
1711 char_u *func,
1712 int argc,
1713 char_u **argv,
1714 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715{
1716 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001717 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001719 /* All arguments are passed as strings, no conversion to number. */
1720 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 return NULL;
1722
1723 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 return retval;
1726}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001727# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001729/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001730 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001732 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733 */
1734 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001735call_func_retlist(
1736 char_u *func,
1737 int argc,
1738 char_u **argv,
1739 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740{
1741 typval_T rettv;
1742
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001743 /* All arguments are passed as strings, no conversion to number. */
1744 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 return NULL;
1746
1747 if (rettv.v_type != VAR_LIST)
1748 {
1749 clear_tv(&rettv);
1750 return NULL;
1751 }
1752
1753 return rettv.vval.v_list;
1754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755#endif
1756
1757/*
1758 * Save the current function call pointer, and set it to NULL.
1759 * Used when executing autocommands and for ":source".
1760 */
1761 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001764 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 current_funccal = NULL;
1767 return (void *)fc;
1768}
1769
1770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001771restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001773 funccall_T *fc = (funccall_T *)vfc;
1774
1775 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776}
1777
Bram Moolenaar05159a02005-02-26 23:04:13 +00001778#if defined(FEAT_PROFILE) || defined(PROTO)
1779/*
1780 * Prepare profiling for entering a child or something else that is not
1781 * counted for the script/function itself.
1782 * Should always be called in pair with prof_child_exit().
1783 */
1784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785prof_child_enter(
1786 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001787{
1788 funccall_T *fc = current_funccal;
1789
1790 if (fc != NULL && fc->func->uf_profiling)
1791 profile_start(&fc->prof_child);
1792 script_prof_save(tm);
1793}
1794
1795/*
1796 * Take care of time spent in a child.
1797 * Should always be called after prof_child_enter().
1798 */
1799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800prof_child_exit(
1801 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001802{
1803 funccall_T *fc = current_funccal;
1804
1805 if (fc != NULL && fc->func->uf_profiling)
1806 {
1807 profile_end(&fc->prof_child);
1808 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1809 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1810 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1811 }
1812 script_prof_restore(tm);
1813}
1814#endif
1815
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#ifdef FEAT_FOLDING
1818/*
1819 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1820 * it in "*cp". Doesn't give error messages.
1821 */
1822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
Bram Moolenaar33570922005-01-25 22:26:29 +00001825 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 int retval;
1827 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001828 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1829 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
1831 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001832 if (use_sandbox)
1833 ++sandbox;
1834 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 retval = 0;
1838 else
1839 {
1840 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 if (tv.v_type == VAR_NUMBER)
1842 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001843 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 retval = 0;
1845 else
1846 {
1847 /* If the result is a string, check if there is a non-digit before
1848 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (!VIM_ISDIGIT(*s) && *s != '-')
1851 *cp = *s++;
1852 retval = atol((char *)s);
1853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001857 if (use_sandbox)
1858 --sandbox;
1859 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
1861 return retval;
1862}
1863#endif
1864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 * ":let" list all variable values
1867 * ":let var1 var2" list variable values
1868 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001869 * ":let var += expr" assignment command.
1870 * ":let var -= expr" assignment command.
1871 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 */
1874 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
1877 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001879 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 int var_count = 0;
1882 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001884 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001885 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
Bram Moolenaardb552d602006-03-23 22:59:57 +00001887 argend = skip_var_list(arg, &var_count, &semicolon);
1888 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001890 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1891 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001892 expr = skipwhite(argend);
1893 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1894 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001896 /*
1897 * ":let" without "=": list variables
1898 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 if (*arg == '[')
1900 EMSG(_(e_invarg));
1901 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001907 list_glob_vars(&first);
1908 list_buf_vars(&first);
1909 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001910#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001911 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001912#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001913 list_script_vars(&first);
1914 list_func_vars(&first);
1915 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 eap->nextcmd = check_nextcmd(arg);
1918 }
1919 else
1920 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 op[0] = '=';
1922 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001923 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001925 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1926 op[0] = *expr; /* +=, -= or .= */
1927 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001929 else
1930 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 if (eap->skip)
1933 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (eap->skip)
1936 {
1937 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 --emsg_skip;
1940 }
1941 else if (i != FAIL)
1942 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
1947 }
1948}
1949
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950/*
1951 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1952 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 * When "nextchars" is not NULL it points to a string with characters that
1954 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1955 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 * Returns OK or FAIL;
1957 */
1958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001959ex_let_vars(
1960 char_u *arg_start,
1961 typval_T *tv,
1962 int copy, /* copy values from "tv", don't move */
1963 int semicolon, /* from skip_var_list() */
1964 int var_count, /* from skip_var_list() */
1965 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966{
1967 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001968 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001970 listitem_T *item;
1971 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972
1973 if (*arg != '[')
1974 {
1975 /*
1976 * ":let var = expr" or ":for var in list"
1977 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001978 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 return FAIL;
1980 return OK;
1981 }
1982
1983 /*
1984 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1985 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001986 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 {
1988 EMSG(_(e_listreq));
1989 return FAIL;
1990 }
1991
1992 i = list_len(l);
1993 if (semicolon == 0 && var_count < i)
1994 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001995 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 }
1998 if (var_count - semicolon > i)
1999 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002000 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 return FAIL;
2002 }
2003
2004 item = l->lv_first;
2005 while (*arg != ']')
2006 {
2007 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002008 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 item = item->li_next;
2010 if (arg == NULL)
2011 return FAIL;
2012
2013 arg = skipwhite(arg);
2014 if (*arg == ';')
2015 {
2016 /* Put the rest of the list (may be empty) in the var after ';'.
2017 * Create a new list for this. */
2018 l = list_alloc();
2019 if (l == NULL)
2020 return FAIL;
2021 while (item != NULL)
2022 {
2023 list_append_tv(l, &item->li_tv);
2024 item = item->li_next;
2025 }
2026
2027 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002028 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 ltv.vval.v_list = l;
2030 l->lv_refcount = 1;
2031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2033 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 clear_tv(&ltv);
2035 if (arg == NULL)
2036 return FAIL;
2037 break;
2038 }
2039 else if (*arg != ',' && *arg != ']')
2040 {
2041 EMSG2(_(e_intern2), "ex_let_vars()");
2042 return FAIL;
2043 }
2044 }
2045
2046 return OK;
2047}
2048
2049/*
2050 * Skip over assignable variable "var" or list of variables "[var, var]".
2051 * Used for ":let varvar = expr" and ":for varvar in expr".
2052 * For "[var, var]" increment "*var_count" for each variable.
2053 * for "[var, var; var]" set "semicolon".
2054 * Return NULL for an error.
2055 */
2056 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002057skip_var_list(
2058 char_u *arg,
2059 int *var_count,
2060 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061{
2062 char_u *p, *s;
2063
2064 if (*arg == '[')
2065 {
2066 /* "[var, var]": find the matching ']'. */
2067 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002068 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069 {
2070 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2071 s = skip_var_one(p);
2072 if (s == p)
2073 {
2074 EMSG2(_(e_invarg2), p);
2075 return NULL;
2076 }
2077 ++*var_count;
2078
2079 p = skipwhite(s);
2080 if (*p == ']')
2081 break;
2082 else if (*p == ';')
2083 {
2084 if (*semicolon == 1)
2085 {
2086 EMSG(_("Double ; in list of variables"));
2087 return NULL;
2088 }
2089 *semicolon = 1;
2090 }
2091 else if (*p != ',')
2092 {
2093 EMSG2(_(e_invarg2), p);
2094 return NULL;
2095 }
2096 }
2097 return p + 1;
2098 }
2099 else
2100 return skip_var_one(arg);
2101}
2102
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002104 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002105 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002107 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002108skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002109{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002110 if (*arg == '@' && arg[1] != NUL)
2111 return arg + 2;
2112 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2113 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002114}
2115
Bram Moolenaara7043832005-01-21 11:56:39 +00002116/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 * List variables for hashtab "ht" with prefix "prefix".
2118 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002121list_hashtable_vars(
2122 hashtab_T *ht,
2123 char_u *prefix,
2124 int empty,
2125 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002126{
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 hashitem_T *hi;
2128 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002129 int todo;
2130
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002131 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002132 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2133 {
2134 if (!HASHITEM_EMPTY(hi))
2135 {
2136 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 di = HI2DI(hi);
2138 if (empty || di->di_tv.v_type != VAR_STRING
2139 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141 }
2142 }
2143}
2144
2145/*
2146 * List global variables.
2147 */
2148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002149list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002150{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002152}
2153
2154/*
2155 * List buffer variables.
2156 */
2157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002158list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002159{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002160 char_u numbuf[NUMBUFLEN];
2161
Bram Moolenaar429fa852013-04-15 12:27:36 +02002162 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002164
2165 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2167 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002168}
2169
2170/*
2171 * List window variables.
2172 */
2173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002175{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002176 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002178}
2179
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002180#ifdef FEAT_WINDOWS
2181/*
2182 * List tab page variables.
2183 */
2184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002185list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002187 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189}
2190#endif
2191
Bram Moolenaara7043832005-01-21 11:56:39 +00002192/*
2193 * List Vim variables.
2194 */
2195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002196list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199}
2200
2201/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002202 * List script-local variables, if there is a script.
2203 */
2204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002205list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002206{
2207 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2209 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002210}
2211
2212/*
2213 * List function variables, if there is a function.
2214 */
2215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002216list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002217{
2218 if (current_funccal != NULL)
2219 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002221}
2222
2223/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 * List variables in "arg".
2225 */
2226 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228{
2229 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 char_u *name_start;
2233 char_u *arg_subsc;
2234 char_u *tofree;
2235 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236
2237 while (!ends_excmd(*arg) && !got_int)
2238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002241 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2243 {
2244 emsg_severe = TRUE;
2245 EMSG(_(e_trailing));
2246 break;
2247 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 /* get_name_len() takes care of expanding curly braces */
2252 name_start = name = arg;
2253 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2254 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 /* This is mainly to keep test 49 working: when expanding
2257 * curly braces fails overrule the exception error message. */
2258 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 emsg_severe = TRUE;
2261 EMSG2(_(e_invarg2), arg);
2262 break;
2263 }
2264 error = TRUE;
2265 }
2266 else
2267 {
2268 if (tofree != NULL)
2269 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002270 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 else
2273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 /* handle d.key, l[idx], f(expr) */
2275 arg_subsc = arg;
2276 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002284 case 'g': list_glob_vars(first); break;
2285 case 'b': list_buf_vars(first); break;
2286 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002287#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002289#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002290 case 'v': list_vim_vars(first); break;
2291 case 's': list_script_vars(first); break;
2292 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293 default:
2294 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 }
2297 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 {
2299 char_u numbuf[NUMBUFLEN];
2300 char_u *tf;
2301 int c;
2302 char_u *s;
2303
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002304 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 c = *arg;
2306 *arg = NUL;
2307 list_one_var_a((char_u *)"",
2308 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002309 tv.v_type,
2310 s == NULL ? (char_u *)"" : s,
2311 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 *arg = c;
2313 vim_free(tf);
2314 }
2315 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319
2320 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322
2323 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 }
2325
2326 return arg;
2327}
2328
2329/*
2330 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2331 * Returns a pointer to the char just after the var name.
2332 * Returns NULL if there is an error.
2333 */
2334 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002335ex_let_one(
2336 char_u *arg, /* points to variable name */
2337 typval_T *tv, /* value to assign to variable */
2338 int copy, /* copy value from "tv" */
2339 char_u *endchars, /* valid chars after variable name or NULL */
2340 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341{
2342 int c1;
2343 char_u *name;
2344 char_u *p;
2345 char_u *arg_end = NULL;
2346 int len;
2347 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349
2350 /*
2351 * ":let $VAR = expr": Set environment variable.
2352 */
2353 if (*arg == '$')
2354 {
2355 /* Find the end of the name. */
2356 ++arg;
2357 name = arg;
2358 len = get_env_len(&arg);
2359 if (len == 0)
2360 EMSG2(_(e_invarg2), name - 1);
2361 else
2362 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 if (op != NULL && (*op == '+' || *op == '-'))
2364 EMSG2(_(e_letwrong), op);
2365 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002366 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002368 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 {
2370 c1 = name[len];
2371 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002372 p = get_tv_string_chk(tv);
2373 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002374 {
2375 int mustfree = FALSE;
2376 char_u *s = vim_getenv(name, &mustfree);
2377
2378 if (s != NULL)
2379 {
2380 p = tofree = concat_str(s, p);
2381 if (mustfree)
2382 vim_free(s);
2383 }
2384 }
2385 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 if (STRICMP(name, "HOME") == 0)
2389 init_homedir();
2390 else if (didset_vim && STRICMP(name, "VIM") == 0)
2391 didset_vim = FALSE;
2392 else if (didset_vimruntime
2393 && STRICMP(name, "VIMRUNTIME") == 0)
2394 didset_vimruntime = FALSE;
2395 arg_end = arg;
2396 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 }
2400 }
2401 }
2402
2403 /*
2404 * ":let &option = expr": Set option value.
2405 * ":let &l:option = expr": Set local option value.
2406 * ":let &g:option = expr": Set global option value.
2407 */
2408 else if (*arg == '&')
2409 {
2410 /* Find the end of the name. */
2411 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002412 if (p == NULL || (endchars != NULL
2413 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 EMSG(_(e_letunexp));
2415 else
2416 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 long n;
2418 int opt_type;
2419 long numval;
2420 char_u *stringval = NULL;
2421 char_u *s;
2422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 c1 = *p;
2424 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425
2426 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002427 s = get_tv_string_chk(tv); /* != NULL if number or string */
2428 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 {
2430 opt_type = get_option_value(arg, &numval,
2431 &stringval, opt_flags);
2432 if ((opt_type == 1 && *op == '.')
2433 || (opt_type == 0 && *op != '.'))
2434 EMSG2(_(e_letwrong), op);
2435 else
2436 {
2437 if (opt_type == 1) /* number */
2438 {
2439 if (*op == '+')
2440 n = numval + n;
2441 else
2442 n = numval - n;
2443 }
2444 else if (opt_type == 0 && stringval != NULL) /* string */
2445 {
2446 s = concat_str(stringval, s);
2447 vim_free(stringval);
2448 stringval = s;
2449 }
2450 }
2451 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 if (s != NULL)
2453 {
2454 set_option_value(arg, n, s, opt_flags);
2455 arg_end = p;
2456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459 }
2460 }
2461
2462 /*
2463 * ":let @r = expr": Set register contents.
2464 */
2465 else if (*arg == '@')
2466 {
2467 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 if (op != NULL && (*op == '+' || *op == '-'))
2469 EMSG2(_(e_letwrong), op);
2470 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002471 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 EMSG(_(e_letunexp));
2473 else
2474 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002475 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 char_u *s;
2477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002478 p = get_tv_string_chk(tv);
2479 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002481 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 if (s != NULL)
2483 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002484 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 vim_free(s);
2486 }
2487 }
2488 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002489 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002491 arg_end = arg + 1;
2492 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002493 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 }
2495 }
2496
2497 /*
2498 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002501 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002505 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2509 EMSG(_(e_letunexp));
2510 else
2511 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 arg_end = p;
2514 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 }
2518
2519 else
2520 EMSG2(_(e_invarg2), arg);
2521
2522 return arg_end;
2523}
2524
2525/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2527 */
2528 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002529check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530{
2531 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2532 {
2533 EMSG2(_(e_readonlyvar), arg);
2534 return TRUE;
2535 }
2536 return FALSE;
2537}
2538
2539/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 * Get an lval: variable, Dict item or List item that can be assigned a value
2541 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2542 * "name.key", "name.key[expr]" etc.
2543 * Indexing only works if "name" is an existing List or Dictionary.
2544 * "name" points to the start of the name.
2545 * If "rettv" is not NULL it points to the value to be assigned.
2546 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2547 * wrong; must end in space or cmd separator.
2548 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549 * flags:
2550 * GLV_QUIET: do not give error messages
2551 * GLV_NO_AUTOLOAD: do not use script autoloading
2552 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002554 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 */
2557 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002558get_lval(
2559 char_u *name,
2560 typval_T *rettv,
2561 lval_T *lp,
2562 int unlet,
2563 int skip,
2564 int flags, /* GLV_ values */
2565 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 char_u *expr_start, *expr_end;
2569 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 dictitem_T *v;
2571 typval_T var1;
2572 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002578 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582
2583 if (skip)
2584 {
2585 /* When skipping just find the end of the name. */
2586 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002587 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 }
2589
2590 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002591 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 if (expr_start != NULL)
2593 {
2594 /* Don't expand the name when we already know there is an error. */
2595 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2596 && *p != '[' && *p != '.')
2597 {
2598 EMSG(_(e_trailing));
2599 return NULL;
2600 }
2601
2602 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2603 if (lp->ll_exp_name == NULL)
2604 {
2605 /* Report an invalid expression in braces, unless the
2606 * expression evaluation has been cancelled due to an
2607 * aborting error, an interrupt, or an exception. */
2608 if (!aborting() && !quiet)
2609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002610 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 EMSG2(_(e_invarg2), name);
2612 return NULL;
2613 }
2614 }
2615 lp->ll_name = lp->ll_exp_name;
2616 }
2617 else
2618 lp->ll_name = name;
2619
2620 /* Without [idx] or .key we are done. */
2621 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2622 return p;
2623
2624 cc = *p;
2625 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002626 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (v == NULL && !quiet)
2628 EMSG2(_(e_undefvar), lp->ll_name);
2629 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 if (v == NULL)
2631 return NULL;
2632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 /*
2634 * Loop until no more [idx] or .key is following.
2635 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002636 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2640 && !(lp->ll_tv->v_type == VAR_DICT
2641 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
2644 EMSG(_("E689: Can only index a List or Dictionary"));
2645 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!quiet)
2650 EMSG(_("E708: [:] must come last"));
2651 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 len = -1;
2655 if (*p == '.')
2656 {
2657 key = p + 1;
2658 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2659 ;
2660 if (len == 0)
2661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (!quiet)
2663 EMSG(_(e_emptykey));
2664 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 }
2666 p = key + len;
2667 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 else
2669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (*p == ':')
2673 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674 else
2675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 empty1 = FALSE;
2677 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002679 if (get_tv_string_chk(&var1) == NULL)
2680 {
2681 /* not a number or string */
2682 clear_tv(&var1);
2683 return NULL;
2684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686
2687 /* Optionally get the second index [ :expr]. */
2688 if (*p == ':')
2689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (rettv != NULL && (rettv->v_type != VAR_LIST
2699 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (!empty1)
2704 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 }
2707 p = skipwhite(p + 1);
2708 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 else
2711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2714 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (!empty1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002719 if (get_tv_string_chk(&var2) == NULL)
2720 {
2721 /* not a number or string */
2722 if (!empty1)
2723 clear_tv(&var1);
2724 clear_tv(&var2);
2725 return NULL;
2726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002732
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 if (!quiet)
2736 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (!empty1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
2743
2744 /* Skip to past ']'. */
2745 ++p;
2746 }
2747
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 {
2750 if (len == -1)
2751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002753 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (*key == NUL)
2755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (!quiet)
2757 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
2761 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002762 lp->ll_list = NULL;
2763 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002764 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002765
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002766 /* When assigning to a scope dictionary check that a function and
2767 * variable name is valid (only variable name unless it is l: or
2768 * g: dictionary). Disallow overwriting a builtin function. */
2769 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002770 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 int prevval;
2772 int wrong;
2773
2774 if (len != -1)
2775 {
2776 prevval = key[len];
2777 key[len] = NUL;
2778 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002779 else
2780 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2782 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002784 || !valid_varname(key);
2785 if (len != -1)
2786 key[len] = prevval;
2787 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788 return NULL;
2789 }
2790
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 /* Can't add "v:" variable. */
2794 if (lp->ll_dict == &vimvardict)
2795 {
2796 EMSG2(_(e_illvar), name);
2797 return NULL;
2798 }
2799
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002800 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002804 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 if (len == -1)
2806 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 }
2809 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 p = NULL;
2817 break;
2818 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002819 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002820 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002821 return NULL;
2822
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 if (len == -1)
2824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 }
2827 else
2828 {
2829 /*
2830 * Get the number and item for the only or first index of the List.
2831 */
2832 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 else
2835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002836 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 clear_tv(&var1);
2838 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002839 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_list = lp->ll_tv->vval.v_list;
2841 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2842 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002844 if (lp->ll_n1 < 0)
2845 {
2846 lp->ll_n1 = 0;
2847 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2848 }
2849 }
2850 if (lp->ll_li == NULL)
2851 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002854 if (!quiet)
2855 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 }
2858
2859 /*
2860 * May need to find the item or absolute index for the second
2861 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 * When no index given: "lp->ll_empty2" is TRUE.
2863 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002867 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002873 {
2874 if (!quiet)
2875 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002877 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 }
2880
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2882 if (lp->ll_n1 < 0)
2883 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2884 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002885 {
2886 if (!quiet)
2887 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002890 }
2891
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 return p;
2897}
2898
2899/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002900 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 */
2902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002903clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904{
2905 vim_free(lp->ll_exp_name);
2906 vim_free(lp->ll_newkey);
2907}
2908
2909/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002910 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 */
2914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002915set_var_lval(
2916 lval_T *lp,
2917 char_u *endp,
2918 typval_T *rettv,
2919 int copy,
2920 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 listitem_T *ri;
2924 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925
2926 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 cc = *endp;
2931 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 if (op != NULL && *op != '=')
2933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002937 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002938 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002939 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002941 if ((di == NULL
2942 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2943 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2944 FALSE)))
2945 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 set_var(lp->ll_name, &tv, FALSE);
2947 clear_tv(&tv);
2948 }
2949 }
2950 else
2951 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002955 else if (tv_check_lock(lp->ll_newkey == NULL
2956 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002957 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 else if (lp->ll_range)
2960 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002961 listitem_T *ll_li = lp->ll_li;
2962 int ll_n1 = lp->ll_n1;
2963
2964 /*
2965 * Check whether any of the list items is locked
2966 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002967 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002969 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002970 return;
2971 ri = ri->li_next;
2972 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2973 break;
2974 ll_li = ll_li->li_next;
2975 ++ll_n1;
2976 }
2977
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 /*
2979 * Assign the List values to the list items.
2980 */
2981 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002982 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 if (op != NULL && *op != '=')
2984 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2985 else
2986 {
2987 clear_tv(&lp->ll_li->li_tv);
2988 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2989 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 ri = ri->li_next;
2991 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2992 break;
2993 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002996 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = NULL;
2999 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 lp->ll_li = lp->ll_li->li_next;
3003 ++lp->ll_n1;
3004 }
3005 if (ri != NULL)
3006 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 else if (lp->ll_empty2
3008 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 : lp->ll_n1 != lp->ll_n2)
3010 EMSG(_("E711: List value has not enough items"));
3011 }
3012 else
3013 {
3014 /*
3015 * Assign to a List or Dictionary item.
3016 */
3017 if (lp->ll_newkey != NULL)
3018 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 if (op != NULL && *op != '=')
3020 {
3021 EMSG2(_(e_letwrong), op);
3022 return;
3023 }
3024
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003026 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 if (di == NULL)
3028 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003029 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3030 {
3031 vim_free(di);
3032 return;
3033 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 else if (op != NULL && *op != '=')
3037 {
3038 tv_op(lp->ll_tv, rettv, op);
3039 return;
3040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 /*
3045 * Assign the value to the variable or list item.
3046 */
3047 if (copy)
3048 copy_tv(rettv, lp->ll_tv);
3049 else
3050 {
3051 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003052 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 }
3055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056}
3057
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3060 * Returns OK or FAIL.
3061 */
3062 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003063tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064{
3065 long n;
3066 char_u numbuf[NUMBUFLEN];
3067 char_u *s;
3068
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003069 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3070 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3071 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 {
3073 switch (tv1->v_type)
3074 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003075 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 case VAR_DICT:
3077 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003078 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003079 case VAR_JOB:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 break;
3081
3082 case VAR_LIST:
3083 if (*op != '+' || tv2->v_type != VAR_LIST)
3084 break;
3085 /* List += List */
3086 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3087 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3088 return OK;
3089
3090 case VAR_NUMBER:
3091 case VAR_STRING:
3092 if (tv2->v_type == VAR_LIST)
3093 break;
3094 if (*op == '+' || *op == '-')
3095 {
3096 /* nr += nr or nr -= nr*/
3097 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098#ifdef FEAT_FLOAT
3099 if (tv2->v_type == VAR_FLOAT)
3100 {
3101 float_T f = n;
3102
3103 if (*op == '+')
3104 f += tv2->vval.v_float;
3105 else
3106 f -= tv2->vval.v_float;
3107 clear_tv(tv1);
3108 tv1->v_type = VAR_FLOAT;
3109 tv1->vval.v_float = f;
3110 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003111 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003112#endif
3113 {
3114 if (*op == '+')
3115 n += get_tv_number(tv2);
3116 else
3117 n -= get_tv_number(tv2);
3118 clear_tv(tv1);
3119 tv1->v_type = VAR_NUMBER;
3120 tv1->vval.v_number = n;
3121 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 }
3123 else
3124 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 if (tv2->v_type == VAR_FLOAT)
3126 break;
3127
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 /* str .= str */
3129 s = get_tv_string(tv1);
3130 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3131 clear_tv(tv1);
3132 tv1->v_type = VAR_STRING;
3133 tv1->vval.v_string = s;
3134 }
3135 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136
3137#ifdef FEAT_FLOAT
3138 case VAR_FLOAT:
3139 {
3140 float_T f;
3141
3142 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3143 && tv2->v_type != VAR_NUMBER
3144 && tv2->v_type != VAR_STRING))
3145 break;
3146 if (tv2->v_type == VAR_FLOAT)
3147 f = tv2->vval.v_float;
3148 else
3149 f = get_tv_number(tv2);
3150 if (*op == '+')
3151 tv1->vval.v_float += f;
3152 else
3153 tv1->vval.v_float -= f;
3154 }
3155 return OK;
3156#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003157 }
3158 }
3159
3160 EMSG2(_(e_letwrong), op);
3161 return FAIL;
3162}
3163
3164/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165 * Add a watcher to a list.
3166 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003168list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169{
3170 lw->lw_next = l->lv_watch;
3171 l->lv_watch = lw;
3172}
3173
3174/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003175 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 * No warning when it isn't found...
3177 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180{
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182
3183 lwp = &l->lv_watch;
3184 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3185 {
3186 if (lw == lwrem)
3187 {
3188 *lwp = lw->lw_next;
3189 break;
3190 }
3191 lwp = &lw->lw_next;
3192 }
3193}
3194
3195/*
3196 * Just before removing an item from a list: advance watchers to the next
3197 * item.
3198 */
3199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003200list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201{
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203
3204 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3205 if (lw->lw_item == item)
3206 lw->lw_item = item->li_next;
3207}
3208
3209/*
3210 * Evaluate the expression used in a ":for var in expr" command.
3211 * "arg" points to "var".
3212 * Set "*errp" to TRUE for an error, FALSE otherwise;
3213 * Return a pointer that holds the info. Null when there is an error.
3214 */
3215 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003216eval_for_line(
3217 char_u *arg,
3218 int *errp,
3219 char_u **nextcmdp,
3220 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221{
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 typval_T tv;
3225 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226
3227 *errp = TRUE; /* default: there is an error */
3228
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 if (fi == NULL)
3231 return NULL;
3232
3233 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3234 if (expr == NULL)
3235 return fi;
3236
3237 expr = skipwhite(expr);
3238 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3239 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003240 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 return fi;
3242 }
3243
3244 if (skip)
3245 ++emsg_skip;
3246 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3247 {
3248 *errp = FALSE;
3249 if (!skip)
3250 {
3251 l = tv.vval.v_list;
3252 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003253 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003255 clear_tv(&tv);
3256 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 else
3258 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003259 /* No need to increment the refcount, it's already set for the
3260 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 fi->fi_list = l;
3262 list_add_watch(l, &fi->fi_lw);
3263 fi->fi_lw.lw_item = l->lv_first;
3264 }
3265 }
3266 }
3267 if (skip)
3268 --emsg_skip;
3269
3270 return fi;
3271}
3272
3273/*
3274 * Use the first item in a ":for" list. Advance to the next.
3275 * Assign the values to the variable (list). "arg" points to the first one.
3276 * Return TRUE when a valid item was found, FALSE when at end of list or
3277 * something wrong.
3278 */
3279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003280next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003282 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285
3286 item = fi->fi_lw.lw_item;
3287 if (item == NULL)
3288 result = FALSE;
3289 else
3290 {
3291 fi->fi_lw.lw_item = item->li_next;
3292 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3293 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3294 }
3295 return result;
3296}
3297
3298/*
3299 * Free the structure used to store info used by ":for".
3300 */
3301 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303{
Bram Moolenaar33570922005-01-25 22:26:29 +00003304 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003306 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003309 list_unref(fi->fi_list);
3310 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 vim_free(fi);
3312}
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3315
3316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317set_context_for_expression(
3318 expand_T *xp,
3319 char_u *arg,
3320 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 int got_eq = FALSE;
3323 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 if (cmdidx == CMD_let)
3327 {
3328 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003329 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 {
3331 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003332 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 {
3334 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003335 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 if (vim_iswhite(*p))
3337 break;
3338 }
3339 return;
3340 }
3341 }
3342 else
3343 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3344 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 while ((xp->xp_pattern = vim_strpbrk(arg,
3346 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3347 {
3348 c = *xp->xp_pattern;
3349 if (c == '&')
3350 {
3351 c = xp->xp_pattern[1];
3352 if (c == '&')
3353 {
3354 ++xp->xp_pattern;
3355 xp->xp_context = cmdidx != CMD_let || got_eq
3356 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3357 }
3358 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003361 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3362 xp->xp_pattern += 2;
3363
3364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366 else if (c == '$')
3367 {
3368 /* environment variable */
3369 xp->xp_context = EXPAND_ENV_VARS;
3370 }
3371 else if (c == '=')
3372 {
3373 got_eq = TRUE;
3374 xp->xp_context = EXPAND_EXPRESSION;
3375 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003376 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 && xp->xp_context == EXPAND_FUNCTIONS
3378 && vim_strchr(xp->xp_pattern, '(') == NULL)
3379 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003380 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 break;
3382 }
3383 else if (cmdidx != CMD_let || got_eq)
3384 {
3385 if (c == '"') /* string */
3386 {
3387 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3388 if (c == '\\' && xp->xp_pattern[1] != NUL)
3389 ++xp->xp_pattern;
3390 xp->xp_context = EXPAND_NOTHING;
3391 }
3392 else if (c == '\'') /* literal string */
3393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003394 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3396 /* skip */ ;
3397 xp->xp_context = EXPAND_NOTHING;
3398 }
3399 else if (c == '|')
3400 {
3401 if (xp->xp_pattern[1] == '|')
3402 {
3403 ++xp->xp_pattern;
3404 xp->xp_context = EXPAND_EXPRESSION;
3405 }
3406 else
3407 xp->xp_context = EXPAND_COMMANDS;
3408 }
3409 else
3410 xp->xp_context = EXPAND_EXPRESSION;
3411 }
3412 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003413 /* Doesn't look like something valid, expand as an expression
3414 * anyway. */
3415 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 arg = xp->xp_pattern;
3417 if (*arg != NUL)
3418 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3419 /* skip */ ;
3420 }
3421 xp->xp_pattern = arg;
3422}
3423
3424#endif /* FEAT_CMDL_COMPL */
3425
3426/*
3427 * ":1,25call func(arg1, arg2)" function call.
3428 */
3429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003430ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
3432 char_u *arg = eap->arg;
3433 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003435 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 linenr_T lnum;
3439 int doesrange;
3440 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003441 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003443 if (eap->skip)
3444 {
3445 /* trans_function_name() doesn't work well when skipping, use eval0()
3446 * instead to skip to any following command, e.g. for:
3447 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003448 ++emsg_skip;
3449 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3450 clear_tv(&rettv);
3451 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003452 return;
3453 }
3454
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003455 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003456 if (fudi.fd_newkey != NULL)
3457 {
3458 /* Still need to give an error message for missing key. */
3459 EMSG2(_(e_dictkey), fudi.fd_newkey);
3460 vim_free(fudi.fd_newkey);
3461 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003462 if (tofree == NULL)
3463 return;
3464
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003465 /* Increase refcount on dictionary, it could get deleted when evaluating
3466 * the arguments. */
3467 if (fudi.fd_dict != NULL)
3468 ++fudi.fd_dict->dv_refcount;
3469
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003470 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003472 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
Bram Moolenaar532c7802005-01-27 14:44:31 +00003474 /* Skip white space to allow ":call func ()". Not good, but required for
3475 * backward compatibility. */
3476 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003477 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479 if (*startarg != '(')
3480 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003481 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 goto end;
3483 }
3484
3485 /*
3486 * When skipping, evaluate the function once, to find the end of the
3487 * arguments.
3488 * When the function takes a range, this is discovered after the first
3489 * call, and the loop is broken.
3490 */
3491 if (eap->skip)
3492 {
3493 ++emsg_skip;
3494 lnum = eap->line2; /* do it once, also with an invalid range */
3495 }
3496 else
3497 lnum = eap->line1;
3498 for ( ; lnum <= eap->line2; ++lnum)
3499 {
3500 if (!eap->skip && eap->addr_count > 0)
3501 {
3502 curwin->w_cursor.lnum = lnum;
3503 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003504#ifdef FEAT_VIRTUALEDIT
3505 curwin->w_cursor.coladd = 0;
3506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003509 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003510 eap->line1, eap->line2, &doesrange,
3511 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 failed = TRUE;
3514 break;
3515 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003516
3517 /* Handle a function returning a Funcref, Dictionary or List. */
3518 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3519 {
3520 failed = TRUE;
3521 break;
3522 }
3523
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003524 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 if (doesrange || eap->skip)
3526 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003529 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003530 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003531 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (aborting())
3533 break;
3534 }
3535 if (eap->skip)
3536 --emsg_skip;
3537
3538 if (!failed)
3539 {
3540 /* Check for trailing illegal characters and a following command. */
3541 if (!ends_excmd(*arg))
3542 {
3543 emsg_severe = TRUE;
3544 EMSG(_(e_trailing));
3545 }
3546 else
3547 eap->nextcmd = check_nextcmd(arg);
3548 }
3549
3550end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003551 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003552 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553}
3554
3555/*
3556 * ":unlet[!] var1 ... " command.
3557 */
3558 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003559ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003561 ex_unletlock(eap, eap->arg, 0);
3562}
3563
3564/*
3565 * ":lockvar" and ":unlockvar" commands
3566 */
3567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571 int deep = 2;
3572
3573 if (eap->forceit)
3574 deep = -1;
3575 else if (vim_isdigit(*arg))
3576 {
3577 deep = getdigits(&arg);
3578 arg = skipwhite(arg);
3579 }
3580
3581 ex_unletlock(eap, arg, deep);
3582}
3583
3584/*
3585 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3586 */
3587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003588ex_unletlock(
3589 exarg_T *eap,
3590 char_u *argstart,
3591 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003592{
3593 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003596 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
3598 do
3599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003601 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003602 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 if (lv.ll_name == NULL)
3604 error = TRUE; /* error but continue parsing */
3605 if (name_end == NULL || (!vim_iswhite(*name_end)
3606 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 if (name_end != NULL)
3609 {
3610 emsg_severe = TRUE;
3611 EMSG(_(e_trailing));
3612 }
3613 if (!(eap->skip || error))
3614 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 break;
3616 }
3617
3618 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619 {
3620 if (eap->cmdidx == CMD_unlet)
3621 {
3622 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3623 error = TRUE;
3624 }
3625 else
3626 {
3627 if (do_lock_var(&lv, name_end, deep,
3628 eap->cmdidx == CMD_lockvar) == FAIL)
3629 error = TRUE;
3630 }
3631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 if (!eap->skip)
3634 clear_lval(&lv);
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 arg = skipwhite(name_end);
3637 } while (!ends_excmd(*arg));
3638
3639 eap->nextcmd = check_nextcmd(arg);
3640}
3641
Bram Moolenaar8c711452005-01-14 21:53:12 +00003642 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003643do_unlet_var(
3644 lval_T *lp,
3645 char_u *name_end,
3646 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003647{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 int ret = OK;
3649 int cc;
3650
3651 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 cc = *name_end;
3654 *name_end = NUL;
3655
3656 /* Normal name or expanded name. */
3657 if (check_changedtick(lp->ll_name))
3658 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003659 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003663 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003664 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003665 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003666 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 else if (lp->ll_range)
3669 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003670 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003671 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003672 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003673
3674 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3675 {
3676 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003678 return FAIL;
3679 ll_li = li;
3680 ++ll_n1;
3681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682
3683 /* Delete a range of List items. */
3684 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3685 {
3686 li = lp->ll_li->li_next;
3687 listitem_remove(lp->ll_list, lp->ll_li);
3688 lp->ll_li = li;
3689 ++lp->ll_n1;
3690 }
3691 }
3692 else
3693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 /* unlet a List item. */
3696 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003699 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 }
3701
3702 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003703}
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705/*
3706 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003707 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 */
3709 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 hashtab_T *ht;
3713 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003714 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003715 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003716 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 ht = find_var_ht(name, &varname);
3719 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003721 if (ht == &globvarht)
3722 d = &globvardict;
3723 else if (current_funccal != NULL
3724 && ht == &current_funccal->l_vars.dv_hashtab)
3725 d = &current_funccal->l_vars;
3726 else if (ht == &compat_hashtab)
3727 d = &vimvardict;
3728 else
3729 {
3730 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3731 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3732 }
3733 if (d == NULL)
3734 {
3735 EMSG2(_(e_intern2), "do_unlet()");
3736 return FAIL;
3737 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 hi = hash_find(ht, varname);
3739 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003740 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003741 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003742 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003743 || var_check_ro(di->di_flags, name, FALSE)
3744 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003745 return FAIL;
3746
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003747 delete_var(ht, hi);
3748 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003751 if (forceit)
3752 return OK;
3753 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 return FAIL;
3755}
3756
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003757/*
3758 * Lock or unlock variable indicated by "lp".
3759 * "deep" is the levels to go (-1 for unlimited);
3760 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3761 */
3762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003763do_lock_var(
3764 lval_T *lp,
3765 char_u *name_end,
3766 int deep,
3767 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768{
3769 int ret = OK;
3770 int cc;
3771 dictitem_T *di;
3772
3773 if (deep == 0) /* nothing to do */
3774 return OK;
3775
3776 if (lp->ll_tv == NULL)
3777 {
3778 cc = *name_end;
3779 *name_end = NUL;
3780
3781 /* Normal name or expanded name. */
3782 if (check_changedtick(lp->ll_name))
3783 ret = FAIL;
3784 else
3785 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003786 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003787 if (di == NULL)
3788 ret = FAIL;
3789 else
3790 {
3791 if (lock)
3792 di->di_flags |= DI_FLAGS_LOCK;
3793 else
3794 di->di_flags &= ~DI_FLAGS_LOCK;
3795 item_lock(&di->di_tv, deep, lock);
3796 }
3797 }
3798 *name_end = cc;
3799 }
3800 else if (lp->ll_range)
3801 {
3802 listitem_T *li = lp->ll_li;
3803
3804 /* (un)lock a range of List items. */
3805 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3806 {
3807 item_lock(&li->li_tv, deep, lock);
3808 li = li->li_next;
3809 ++lp->ll_n1;
3810 }
3811 }
3812 else if (lp->ll_list != NULL)
3813 /* (un)lock a List item. */
3814 item_lock(&lp->ll_li->li_tv, deep, lock);
3815 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003816 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003817 item_lock(&lp->ll_di->di_tv, deep, lock);
3818
3819 return ret;
3820}
3821
3822/*
3823 * Lock or unlock an item. "deep" is nr of levels to go.
3824 */
3825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003826item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003827{
3828 static int recurse = 0;
3829 list_T *l;
3830 listitem_T *li;
3831 dict_T *d;
3832 hashitem_T *hi;
3833 int todo;
3834
3835 if (recurse >= DICT_MAXNEST)
3836 {
3837 EMSG(_("E743: variable nested too deep for (un)lock"));
3838 return;
3839 }
3840 if (deep == 0)
3841 return;
3842 ++recurse;
3843
3844 /* lock/unlock the item itself */
3845 if (lock)
3846 tv->v_lock |= VAR_LOCKED;
3847 else
3848 tv->v_lock &= ~VAR_LOCKED;
3849
3850 switch (tv->v_type)
3851 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003852 case VAR_UNKNOWN:
3853 case VAR_NUMBER:
3854 case VAR_STRING:
3855 case VAR_FUNC:
3856 case VAR_FLOAT:
3857 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003858 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003859 break;
3860
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003861 case VAR_LIST:
3862 if ((l = tv->vval.v_list) != NULL)
3863 {
3864 if (lock)
3865 l->lv_lock |= VAR_LOCKED;
3866 else
3867 l->lv_lock &= ~VAR_LOCKED;
3868 if (deep < 0 || deep > 1)
3869 /* recursive: lock/unlock the items the List contains */
3870 for (li = l->lv_first; li != NULL; li = li->li_next)
3871 item_lock(&li->li_tv, deep - 1, lock);
3872 }
3873 break;
3874 case VAR_DICT:
3875 if ((d = tv->vval.v_dict) != NULL)
3876 {
3877 if (lock)
3878 d->dv_lock |= VAR_LOCKED;
3879 else
3880 d->dv_lock &= ~VAR_LOCKED;
3881 if (deep < 0 || deep > 1)
3882 {
3883 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003884 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003885 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3886 {
3887 if (!HASHITEM_EMPTY(hi))
3888 {
3889 --todo;
3890 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3891 }
3892 }
3893 }
3894 }
3895 }
3896 --recurse;
3897}
3898
Bram Moolenaara40058a2005-07-11 22:42:07 +00003899/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003900 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3901 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003902 */
3903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003904tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003905{
3906 return (tv->v_lock & VAR_LOCKED)
3907 || (tv->v_type == VAR_LIST
3908 && tv->vval.v_list != NULL
3909 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3910 || (tv->v_type == VAR_DICT
3911 && tv->vval.v_dict != NULL
3912 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3913}
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3916/*
3917 * Delete all "menutrans_" variables.
3918 */
3919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003920del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003923 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003926 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 {
3929 if (!HASHITEM_EMPTY(hi))
3930 {
3931 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3933 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003934 }
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937}
3938#endif
3939
3940#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3941
3942/*
3943 * Local string buffer for the next two functions to store a variable name
3944 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3945 * get_user_var_name().
3946 */
3947
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003948static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950static char_u *varnamebuf = NULL;
3951static int varnamebuflen = 0;
3952
3953/*
3954 * Function to concatenate a prefix and a variable name.
3955 */
3956 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003957cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
3959 int len;
3960
3961 len = (int)STRLEN(name) + 3;
3962 if (len > varnamebuflen)
3963 {
3964 vim_free(varnamebuf);
3965 len += 10; /* some additional space */
3966 varnamebuf = alloc(len);
3967 if (varnamebuf == NULL)
3968 {
3969 varnamebuflen = 0;
3970 return NULL;
3971 }
3972 varnamebuflen = len;
3973 }
3974 *varnamebuf = prefix;
3975 varnamebuf[1] = ':';
3976 STRCPY(varnamebuf + 2, name);
3977 return varnamebuf;
3978}
3979
3980/*
3981 * Function given to ExpandGeneric() to obtain the list of user defined
3982 * (global/buffer/window/built-in) variable names.
3983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003985get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003987 static long_u gdone;
3988 static long_u bdone;
3989 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003990#ifdef FEAT_WINDOWS
3991 static long_u tdone;
3992#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003993 static int vidx;
3994 static hashitem_T *hi;
3995 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
3997 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003998 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003999 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004000#ifdef FEAT_WINDOWS
4001 tdone = 0;
4002#endif
4003 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004004
4005 /* Global variables */
4006 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004008 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004010 else
4011 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 while (HASHITEM_EMPTY(hi))
4013 ++hi;
4014 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4015 return cat_prefix_varname('g', hi->hi_key);
4016 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004018
4019 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004020 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004023 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004025 else
4026 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 while (HASHITEM_EMPTY(hi))
4028 ++hi;
4029 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004031 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return (char_u *)"b:changedtick";
4035 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004036
4037 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004038 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004041 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004042 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004043 else
4044 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004045 while (HASHITEM_EMPTY(hi))
4046 ++hi;
4047 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004049
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004050#ifdef FEAT_WINDOWS
4051 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004052 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004053 if (tdone < ht->ht_used)
4054 {
4055 if (tdone++ == 0)
4056 hi = ht->ht_array;
4057 else
4058 ++hi;
4059 while (HASHITEM_EMPTY(hi))
4060 ++hi;
4061 return cat_prefix_varname('t', hi->hi_key);
4062 }
4063#endif
4064
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 /* v: variables */
4066 if (vidx < VV_LEN)
4067 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 vim_free(varnamebuf);
4070 varnamebuf = NULL;
4071 varnamebuflen = 0;
4072 return NULL;
4073}
4074
4075#endif /* FEAT_CMDL_COMPL */
4076
4077/*
4078 * types for expressions.
4079 */
4080typedef enum
4081{
4082 TYPE_UNKNOWN = 0
4083 , TYPE_EQUAL /* == */
4084 , TYPE_NEQUAL /* != */
4085 , TYPE_GREATER /* > */
4086 , TYPE_GEQUAL /* >= */
4087 , TYPE_SMALLER /* < */
4088 , TYPE_SEQUAL /* <= */
4089 , TYPE_MATCH /* =~ */
4090 , TYPE_NOMATCH /* !~ */
4091} exptype_T;
4092
4093/*
4094 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4097 */
4098
4099/*
4100 * Handle zero level expression.
4101 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004103 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * Return OK or FAIL.
4105 */
4106 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004107eval0(
4108 char_u *arg,
4109 typval_T *rettv,
4110 char_u **nextcmd,
4111 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113 int ret;
4114 char_u *p;
4115
4116 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (ret == FAIL || !ends_excmd(*p))
4119 {
4120 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 /*
4123 * Report the invalid expression unless the expression evaluation has
4124 * been cancelled due to an aborting error, an interrupt, or an
4125 * exception.
4126 */
4127 if (!aborting())
4128 EMSG2(_(e_invexpr2), arg);
4129 ret = FAIL;
4130 }
4131 if (nextcmd != NULL)
4132 *nextcmd = check_nextcmd(p);
4133
4134 return ret;
4135}
4136
4137/*
4138 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004139 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 *
4141 * "arg" must point to the first non-white of the expression.
4142 * "arg" is advanced to the next non-white after the recognized expression.
4143 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004144 * Note: "rettv.v_lock" is not set.
4145 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 * Return OK or FAIL.
4147 */
4148 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004149eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
4151 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004152 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154 /*
4155 * Get the first variable.
4156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 if ((*arg)[0] == '?')
4161 {
4162 result = FALSE;
4163 if (evaluate)
4164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 int error = FALSE;
4166
4167 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 if (error)
4171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173
4174 /*
4175 * Get the second variable.
4176 */
4177 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
4180
4181 /*
4182 * Check for the ":".
4183 */
4184 if ((*arg)[0] != ':')
4185 {
4186 EMSG(_("E109: Missing ':' after '?'"));
4187 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 return FAIL;
4190 }
4191
4192 /*
4193 * Get the third variable.
4194 */
4195 *arg = skipwhite(*arg + 1);
4196 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4197 {
4198 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 return FAIL;
4201 }
4202 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205
4206 return OK;
4207}
4208
4209/*
4210 * Handle first level expression:
4211 * expr2 || expr2 || expr2 logical OR
4212 *
4213 * "arg" must point to the first non-white of the expression.
4214 * "arg" is advanced to the next non-white after the recognized expression.
4215 *
4216 * Return OK or FAIL.
4217 */
4218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004219eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
Bram Moolenaar33570922005-01-25 22:26:29 +00004221 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 long result;
4223 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226 /*
4227 * Get the first variable.
4228 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return FAIL;
4231
4232 /*
4233 * Repeat until there is no following "||".
4234 */
4235 first = TRUE;
4236 result = FALSE;
4237 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4238 {
4239 if (evaluate && first)
4240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (error)
4245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 first = FALSE;
4247 }
4248
4249 /*
4250 * Get the second variable.
4251 */
4252 *arg = skipwhite(*arg + 2);
4253 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4254 return FAIL;
4255
4256 /*
4257 * Compute the result.
4258 */
4259 if (evaluate && !result)
4260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 if (evaluate)
4268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 rettv->v_type = VAR_NUMBER;
4270 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 }
4273
4274 return OK;
4275}
4276
4277/*
4278 * Handle second level expression:
4279 * expr3 && expr3 && expr3 logical AND
4280 *
4281 * "arg" must point to the first non-white of the expression.
4282 * "arg" is advanced to the next non-white after the recognized expression.
4283 *
4284 * Return OK or FAIL.
4285 */
4286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004287eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
Bram Moolenaar33570922005-01-25 22:26:29 +00004289 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 long result;
4291 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
4294 /*
4295 * Get the first variable.
4296 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return FAIL;
4299
4300 /*
4301 * Repeat until there is no following "&&".
4302 */
4303 first = TRUE;
4304 result = TRUE;
4305 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4306 {
4307 if (evaluate && first)
4308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 if (error)
4313 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 first = FALSE;
4315 }
4316
4317 /*
4318 * Get the second variable.
4319 */
4320 *arg = skipwhite(*arg + 2);
4321 if (eval4(arg, &var2, evaluate && result) == FAIL)
4322 return FAIL;
4323
4324 /*
4325 * Compute the result.
4326 */
4327 if (evaluate && result)
4328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (error)
4333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 if (evaluate)
4336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 rettv->v_type = VAR_NUMBER;
4338 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340 }
4341
4342 return OK;
4343}
4344
4345/*
4346 * Handle third level expression:
4347 * var1 == var2
4348 * var1 =~ var2
4349 * var1 != var2
4350 * var1 !~ var2
4351 * var1 > var2
4352 * var1 >= var2
4353 * var1 < var2
4354 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004355 * var1 is var2
4356 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 *
4358 * "arg" must point to the first non-white of the expression.
4359 * "arg" is advanced to the next non-white after the recognized expression.
4360 *
4361 * Return OK or FAIL.
4362 */
4363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004364eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
Bram Moolenaar33570922005-01-25 22:26:29 +00004366 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 char_u *p;
4368 int i;
4369 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004370 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 int len = 2;
4372 long n1, n2;
4373 char_u *s1, *s2;
4374 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4375 regmatch_T regmatch;
4376 int ic;
4377 char_u *save_cpo;
4378
4379 /*
4380 * Get the first variable.
4381 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 return FAIL;
4384
4385 p = *arg;
4386 switch (p[0])
4387 {
4388 case '=': if (p[1] == '=')
4389 type = TYPE_EQUAL;
4390 else if (p[1] == '~')
4391 type = TYPE_MATCH;
4392 break;
4393 case '!': if (p[1] == '=')
4394 type = TYPE_NEQUAL;
4395 else if (p[1] == '~')
4396 type = TYPE_NOMATCH;
4397 break;
4398 case '>': if (p[1] != '=')
4399 {
4400 type = TYPE_GREATER;
4401 len = 1;
4402 }
4403 else
4404 type = TYPE_GEQUAL;
4405 break;
4406 case '<': if (p[1] != '=')
4407 {
4408 type = TYPE_SMALLER;
4409 len = 1;
4410 }
4411 else
4412 type = TYPE_SEQUAL;
4413 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004414 case 'i': if (p[1] == 's')
4415 {
4416 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4417 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004418 i = p[len];
4419 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004420 {
4421 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4422 type_is = TRUE;
4423 }
4424 }
4425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427
4428 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004429 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 */
4431 if (type != TYPE_UNKNOWN)
4432 {
4433 /* extra question mark appended: ignore case */
4434 if (p[len] == '?')
4435 {
4436 ic = TRUE;
4437 ++len;
4438 }
4439 /* extra '#' appended: match case */
4440 else if (p[len] == '#')
4441 {
4442 ic = FALSE;
4443 ++len;
4444 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 else
4447 ic = p_ic;
4448
4449 /*
4450 * Get the second variable.
4451 */
4452 *arg = skipwhite(p + len);
4453 if (eval5(arg, &var2, evaluate) == FAIL)
4454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 return FAIL;
4457 }
4458
4459 if (evaluate)
4460 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004461 if (type_is && rettv->v_type != var2.v_type)
4462 {
4463 /* For "is" a different type always means FALSE, for "notis"
4464 * it means TRUE. */
4465 n1 = (type == TYPE_NEQUAL);
4466 }
4467 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4468 {
4469 if (type_is)
4470 {
4471 n1 = (rettv->v_type == var2.v_type
4472 && rettv->vval.v_list == var2.vval.v_list);
4473 if (type == TYPE_NEQUAL)
4474 n1 = !n1;
4475 }
4476 else if (rettv->v_type != var2.v_type
4477 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4478 {
4479 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004480 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004482 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 clear_tv(rettv);
4484 clear_tv(&var2);
4485 return FAIL;
4486 }
4487 else
4488 {
4489 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004490 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4491 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 }
4496
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004497 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4498 {
4499 if (type_is)
4500 {
4501 n1 = (rettv->v_type == var2.v_type
4502 && rettv->vval.v_dict == var2.vval.v_dict);
4503 if (type == TYPE_NEQUAL)
4504 n1 = !n1;
4505 }
4506 else if (rettv->v_type != var2.v_type
4507 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4508 {
4509 if (rettv->v_type != var2.v_type)
4510 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4511 else
4512 EMSG(_("E736: Invalid operation for Dictionary"));
4513 clear_tv(rettv);
4514 clear_tv(&var2);
4515 return FAIL;
4516 }
4517 else
4518 {
4519 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004520 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4521 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004522 if (type == TYPE_NEQUAL)
4523 n1 = !n1;
4524 }
4525 }
4526
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004527 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4528 {
4529 if (rettv->v_type != var2.v_type
4530 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4531 {
4532 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004533 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004535 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004536 clear_tv(rettv);
4537 clear_tv(&var2);
4538 return FAIL;
4539 }
4540 else
4541 {
4542 /* Compare two Funcrefs for being equal or unequal. */
4543 if (rettv->vval.v_string == NULL
4544 || var2.vval.v_string == NULL)
4545 n1 = FALSE;
4546 else
4547 n1 = STRCMP(rettv->vval.v_string,
4548 var2.vval.v_string) == 0;
4549 if (type == TYPE_NEQUAL)
4550 n1 = !n1;
4551 }
4552 }
4553
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004554#ifdef FEAT_FLOAT
4555 /*
4556 * If one of the two variables is a float, compare as a float.
4557 * When using "=~" or "!~", always compare as string.
4558 */
4559 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4560 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4561 {
4562 float_T f1, f2;
4563
4564 if (rettv->v_type == VAR_FLOAT)
4565 f1 = rettv->vval.v_float;
4566 else
4567 f1 = get_tv_number(rettv);
4568 if (var2.v_type == VAR_FLOAT)
4569 f2 = var2.vval.v_float;
4570 else
4571 f2 = get_tv_number(&var2);
4572 n1 = FALSE;
4573 switch (type)
4574 {
4575 case TYPE_EQUAL: n1 = (f1 == f2); break;
4576 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4577 case TYPE_GREATER: n1 = (f1 > f2); break;
4578 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4579 case TYPE_SMALLER: n1 = (f1 < f2); break;
4580 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4581 case TYPE_UNKNOWN:
4582 case TYPE_MATCH:
4583 case TYPE_NOMATCH: break; /* avoid gcc warning */
4584 }
4585 }
4586#endif
4587
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 /*
4589 * If one of the two variables is a number, compare as a number.
4590 * When using "=~" or "!~", always compare as string.
4591 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004592 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 n1 = get_tv_number(rettv);
4596 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 switch (type)
4598 {
4599 case TYPE_EQUAL: n1 = (n1 == n2); break;
4600 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4601 case TYPE_GREATER: n1 = (n1 > n2); break;
4602 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4603 case TYPE_SMALLER: n1 = (n1 < n2); break;
4604 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4605 case TYPE_UNKNOWN:
4606 case TYPE_MATCH:
4607 case TYPE_NOMATCH: break; /* avoid gcc warning */
4608 }
4609 }
4610 else
4611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 s1 = get_tv_string_buf(rettv, buf1);
4613 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4615 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4616 else
4617 i = 0;
4618 n1 = FALSE;
4619 switch (type)
4620 {
4621 case TYPE_EQUAL: n1 = (i == 0); break;
4622 case TYPE_NEQUAL: n1 = (i != 0); break;
4623 case TYPE_GREATER: n1 = (i > 0); break;
4624 case TYPE_GEQUAL: n1 = (i >= 0); break;
4625 case TYPE_SMALLER: n1 = (i < 0); break;
4626 case TYPE_SEQUAL: n1 = (i <= 0); break;
4627
4628 case TYPE_MATCH:
4629 case TYPE_NOMATCH:
4630 /* avoid 'l' flag in 'cpoptions' */
4631 save_cpo = p_cpo;
4632 p_cpo = (char_u *)"";
4633 regmatch.regprog = vim_regcomp(s2,
4634 RE_MAGIC + RE_STRING);
4635 regmatch.rm_ic = ic;
4636 if (regmatch.regprog != NULL)
4637 {
4638 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004639 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (type == TYPE_NOMATCH)
4641 n1 = !n1;
4642 }
4643 p_cpo = save_cpo;
4644 break;
4645
4646 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4647 }
4648 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004649 clear_tv(rettv);
4650 clear_tv(&var2);
4651 rettv->v_type = VAR_NUMBER;
4652 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 }
4655
4656 return OK;
4657}
4658
4659/*
4660 * Handle fourth level expression:
4661 * + number addition
4662 * - number subtraction
4663 * . string concatenation
4664 *
4665 * "arg" must point to the first non-white of the expression.
4666 * "arg" is advanced to the next non-white after the recognized expression.
4667 *
4668 * Return OK or FAIL.
4669 */
4670 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004671eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
Bram Moolenaar33570922005-01-25 22:26:29 +00004673 typval_T var2;
4674 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 int op;
4676 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004677#ifdef FEAT_FLOAT
4678 float_T f1 = 0, f2 = 0;
4679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 char_u *s1, *s2;
4681 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4682 char_u *p;
4683
4684 /*
4685 * Get the first variable.
4686 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004687 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 return FAIL;
4689
4690 /*
4691 * Repeat computing, until no '+', '-' or '.' is following.
4692 */
4693 for (;;)
4694 {
4695 op = **arg;
4696 if (op != '+' && op != '-' && op != '.')
4697 break;
4698
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004699 if ((op != '+' || rettv->v_type != VAR_LIST)
4700#ifdef FEAT_FLOAT
4701 && (op == '.' || rettv->v_type != VAR_FLOAT)
4702#endif
4703 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 {
4705 /* For "list + ...", an illegal use of the first operand as
4706 * a number cannot be determined before evaluating the 2nd
4707 * operand: if this is also a list, all is ok.
4708 * For "something . ...", "something - ..." or "non-list + ...",
4709 * we know that the first operand needs to be a string or number
4710 * without evaluating the 2nd operand. So check before to avoid
4711 * side effects after an error. */
4712 if (evaluate && get_tv_string_chk(rettv) == NULL)
4713 {
4714 clear_tv(rettv);
4715 return FAIL;
4716 }
4717 }
4718
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 /*
4720 * Get the second variable.
4721 */
4722 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004723 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004725 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return FAIL;
4727 }
4728
4729 if (evaluate)
4730 {
4731 /*
4732 * Compute the result.
4733 */
4734 if (op == '.')
4735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004736 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4737 s2 = get_tv_string_buf_chk(&var2, buf2);
4738 if (s2 == NULL) /* type error ? */
4739 {
4740 clear_tv(rettv);
4741 clear_tv(&var2);
4742 return FAIL;
4743 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004744 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
4746 rettv->v_type = VAR_STRING;
4747 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004749 else if (op == '+' && rettv->v_type == VAR_LIST
4750 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004751 {
4752 /* concatenate Lists */
4753 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4754 &var3) == FAIL)
4755 {
4756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 return FAIL;
4759 }
4760 clear_tv(rettv);
4761 *rettv = var3;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 else
4764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 int error = FALSE;
4766
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004767#ifdef FEAT_FLOAT
4768 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004769 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770 f1 = rettv->vval.v_float;
4771 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004772 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004773 else
4774#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004776 n1 = get_tv_number_chk(rettv, &error);
4777 if (error)
4778 {
4779 /* This can only happen for "list + non-list". For
4780 * "non-list + ..." or "something - ...", we returned
4781 * before evaluating the 2nd operand. */
4782 clear_tv(rettv);
4783 return FAIL;
4784 }
4785#ifdef FEAT_FLOAT
4786 if (var2.v_type == VAR_FLOAT)
4787 f1 = n1;
4788#endif
4789 }
4790#ifdef FEAT_FLOAT
4791 if (var2.v_type == VAR_FLOAT)
4792 {
4793 f2 = var2.vval.v_float;
4794 n2 = 0;
4795 }
4796 else
4797#endif
4798 {
4799 n2 = get_tv_number_chk(&var2, &error);
4800 if (error)
4801 {
4802 clear_tv(rettv);
4803 clear_tv(&var2);
4804 return FAIL;
4805 }
4806#ifdef FEAT_FLOAT
4807 if (rettv->v_type == VAR_FLOAT)
4808 f2 = n2;
4809#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004812
4813#ifdef FEAT_FLOAT
4814 /* If there is a float on either side the result is a float. */
4815 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4816 {
4817 if (op == '+')
4818 f1 = f1 + f2;
4819 else
4820 f1 = f1 - f2;
4821 rettv->v_type = VAR_FLOAT;
4822 rettv->vval.v_float = f1;
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004825#endif
4826 {
4827 if (op == '+')
4828 n1 = n1 + n2;
4829 else
4830 n1 = n1 - n2;
4831 rettv->v_type = VAR_NUMBER;
4832 rettv->vval.v_number = n1;
4833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
4837 }
4838 return OK;
4839}
4840
4841/*
4842 * Handle fifth level expression:
4843 * * number multiplication
4844 * / number division
4845 * % number modulo
4846 *
4847 * "arg" must point to the first non-white of the expression.
4848 * "arg" is advanced to the next non-white after the recognized expression.
4849 *
4850 * Return OK or FAIL.
4851 */
4852 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004853eval6(
4854 char_u **arg,
4855 typval_T *rettv,
4856 int evaluate,
4857 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
Bram Moolenaar33570922005-01-25 22:26:29 +00004859 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 int op;
4861 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862#ifdef FEAT_FLOAT
4863 int use_float = FALSE;
4864 float_T f1 = 0, f2;
4865#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004866 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
4868 /*
4869 * Get the first variable.
4870 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004871 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 return FAIL;
4873
4874 /*
4875 * Repeat computing, until no '*', '/' or '%' is following.
4876 */
4877 for (;;)
4878 {
4879 op = **arg;
4880 if (op != '*' && op != '/' && op != '%')
4881 break;
4882
4883 if (evaluate)
4884 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885#ifdef FEAT_FLOAT
4886 if (rettv->v_type == VAR_FLOAT)
4887 {
4888 f1 = rettv->vval.v_float;
4889 use_float = TRUE;
4890 n1 = 0;
4891 }
4892 else
4893#endif
4894 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004896 if (error)
4897 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 else
4900 n1 = 0;
4901
4902 /*
4903 * Get the second variable.
4904 */
4905 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004906 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 return FAIL;
4908
4909 if (evaluate)
4910 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911#ifdef FEAT_FLOAT
4912 if (var2.v_type == VAR_FLOAT)
4913 {
4914 if (!use_float)
4915 {
4916 f1 = n1;
4917 use_float = TRUE;
4918 }
4919 f2 = var2.vval.v_float;
4920 n2 = 0;
4921 }
4922 else
4923#endif
4924 {
4925 n2 = get_tv_number_chk(&var2, &error);
4926 clear_tv(&var2);
4927 if (error)
4928 return FAIL;
4929#ifdef FEAT_FLOAT
4930 if (use_float)
4931 f2 = n2;
4932#endif
4933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
4935 /*
4936 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004937 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939#ifdef FEAT_FLOAT
4940 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004942 if (op == '*')
4943 f1 = f1 * f2;
4944 else if (op == '/')
4945 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004946# ifdef VMS
4947 /* VMS crashes on divide by zero, work around it */
4948 if (f2 == 0.0)
4949 {
4950 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004951 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004952 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004953 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004954 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004955 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004956 }
4957 else
4958 f1 = f1 / f2;
4959# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004960 /* We rely on the floating point library to handle divide
4961 * by zero to result in "inf" and not a crash. */
4962 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004963# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004967 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004968 return FAIL;
4969 }
4970 rettv->v_type = VAR_FLOAT;
4971 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 if (op == '*')
4977 n1 = n1 * n2;
4978 else if (op == '/')
4979 {
4980 if (n2 == 0) /* give an error message? */
4981 {
4982 if (n1 == 0)
4983 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4984 else if (n1 < 0)
4985 n1 = -0x7fffffffL;
4986 else
4987 n1 = 0x7fffffffL;
4988 }
4989 else
4990 n1 = n1 / n2;
4991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 {
4994 if (n2 == 0) /* give an error message? */
4995 n1 = 0;
4996 else
4997 n1 = n1 % n2;
4998 }
4999 rettv->v_type = VAR_NUMBER;
5000 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003 }
5004
5005 return OK;
5006}
5007
5008/*
5009 * Handle sixth level expression:
5010 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005011 * "string" string constant
5012 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 * &option-name option value
5014 * @r register contents
5015 * identifier variable value
5016 * function() function call
5017 * $VAR environment variable
5018 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005019 * [expr, expr] List
5020 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 *
5022 * Also handle:
5023 * ! in front logical NOT
5024 * - in front unary minus
5025 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 * trailing [] subscript in String or List
5027 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 *
5029 * "arg" must point to the first non-white of the expression.
5030 * "arg" is advanced to the next non-white after the recognized expression.
5031 *
5032 * Return OK or FAIL.
5033 */
5034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005035eval7(
5036 char_u **arg,
5037 typval_T *rettv,
5038 int evaluate,
5039 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 long n;
5042 int len;
5043 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 char_u *start_leader, *end_leader;
5045 int ret = OK;
5046 char_u *alias;
5047
5048 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005049 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005050 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
5054 /*
5055 * Skip '!' and '-' characters. They are handled later.
5056 */
5057 start_leader = *arg;
5058 while (**arg == '!' || **arg == '-' || **arg == '+')
5059 *arg = skipwhite(*arg + 1);
5060 end_leader = *arg;
5061
5062 switch (**arg)
5063 {
5064 /*
5065 * Number constant.
5066 */
5067 case '0':
5068 case '1':
5069 case '2':
5070 case '3':
5071 case '4':
5072 case '5':
5073 case '6':
5074 case '7':
5075 case '8':
5076 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005077 {
5078#ifdef FEAT_FLOAT
5079 char_u *p = skipdigits(*arg + 1);
5080 int get_float = FALSE;
5081
5082 /* We accept a float when the format matches
5083 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005084 * strict to avoid backwards compatibility problems.
5085 * Don't look for a float after the "." operator, so that
5086 * ":let vers = 1.2.3" doesn't fail. */
5087 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005089 get_float = TRUE;
5090 p = skipdigits(p + 2);
5091 if (*p == 'e' || *p == 'E')
5092 {
5093 ++p;
5094 if (*p == '-' || *p == '+')
5095 ++p;
5096 if (!vim_isdigit(*p))
5097 get_float = FALSE;
5098 else
5099 p = skipdigits(p + 1);
5100 }
5101 if (ASCII_ISALPHA(*p) || *p == '.')
5102 get_float = FALSE;
5103 }
5104 if (get_float)
5105 {
5106 float_T f;
5107
5108 *arg += string2float(*arg, &f);
5109 if (evaluate)
5110 {
5111 rettv->v_type = VAR_FLOAT;
5112 rettv->vval.v_float = f;
5113 }
5114 }
5115 else
5116#endif
5117 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005118 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005119 *arg += len;
5120 if (evaluate)
5121 {
5122 rettv->v_type = VAR_NUMBER;
5123 rettv->vval.v_number = n;
5124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 /*
5130 * String constant: "string".
5131 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005132 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 break;
5134
5135 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 break;
5140
5141 /*
5142 * List: [expr, expr]
5143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146
5147 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 * Dictionary: {key: val, key: val}
5149 */
5150 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5151 break;
5152
5153 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005154 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005156 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158
5159 /*
5160 * Environment variable: $VAR.
5161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164
5165 /*
5166 * Register contents: @r.
5167 */
5168 case '@': ++*arg;
5169 if (evaluate)
5170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005172 rettv->vval.v_string = get_reg_contents(**arg,
5173 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175 if (**arg != NUL)
5176 ++*arg;
5177 break;
5178
5179 /*
5180 * nested expression: (expression).
5181 */
5182 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (**arg == ')')
5185 ++*arg;
5186 else if (ret == OK)
5187 {
5188 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 ret = FAIL;
5191 }
5192 break;
5193
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 break;
5196 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197
5198 if (ret == NOTDONE)
5199 {
5200 /*
5201 * Must be a variable or function name.
5202 * Can also be a curly-braces kind of name: {expr}.
5203 */
5204 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005205 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 if (alias != NULL)
5207 s = alias;
5208
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005209 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 ret = FAIL;
5211 else
5212 {
5213 if (**arg == '(') /* recursive! */
5214 {
5215 /* If "s" is the name of a variable of type VAR_FUNC
5216 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005217 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218
5219 /* Invoke the function. */
5220 ret = get_func_tv(s, len, rettv, arg,
5221 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005222 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005223
5224 /* If evaluate is FALSE rettv->v_type was not set in
5225 * get_func_tv, but it's needed in handle_subscript() to parse
5226 * what follows. So set it here. */
5227 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5228 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005229 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005230 rettv->v_type = VAR_FUNC;
5231 }
5232
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 /* Stop the expression evaluation when immediately
5234 * aborting on error, or when an interrupt occurred or
5235 * an exception was thrown but not caught. */
5236 if (aborting())
5237 {
5238 if (ret == OK)
5239 clear_tv(rettv);
5240 ret = FAIL;
5241 }
5242 }
5243 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005244 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005245 else
5246 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005248 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 }
5250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 *arg = skipwhite(*arg);
5252
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005253 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5254 * expr(expr). */
5255 if (ret == OK)
5256 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
5258 /*
5259 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5260 */
5261 if (ret == OK && evaluate && end_leader > start_leader)
5262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005264 int val = 0;
5265#ifdef FEAT_FLOAT
5266 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005267
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005268 if (rettv->v_type == VAR_FLOAT)
5269 f = rettv->vval.v_float;
5270 else
5271#endif
5272 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005273 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005275 clear_tv(rettv);
5276 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005278 else
5279 {
5280 while (end_leader > start_leader)
5281 {
5282 --end_leader;
5283 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 {
5285#ifdef FEAT_FLOAT
5286 if (rettv->v_type == VAR_FLOAT)
5287 f = !f;
5288 else
5289#endif
5290 val = !val;
5291 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005293 {
5294#ifdef FEAT_FLOAT
5295 if (rettv->v_type == VAR_FLOAT)
5296 f = -f;
5297 else
5298#endif
5299 val = -val;
5300 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005301 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005302#ifdef FEAT_FLOAT
5303 if (rettv->v_type == VAR_FLOAT)
5304 {
5305 clear_tv(rettv);
5306 rettv->vval.v_float = f;
5307 }
5308 else
5309#endif
5310 {
5311 clear_tv(rettv);
5312 rettv->v_type = VAR_NUMBER;
5313 rettv->vval.v_number = val;
5314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 }
5317
5318 return ret;
5319}
5320
5321/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005322 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5323 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5325 */
5326 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005327eval_index(
5328 char_u **arg,
5329 typval_T *rettv,
5330 int evaluate,
5331 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332{
5333 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005334 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336 long len = -1;
5337 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340
Bram Moolenaara03f2332016-02-06 18:09:59 +01005341 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005343 case VAR_FUNC:
5344 if (verbose)
5345 EMSG(_("E695: Cannot index a Funcref"));
5346 return FAIL;
5347 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005348#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005349 if (verbose)
5350 EMSG(_(e_float_as_string));
5351 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005352#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005353 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005354 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005355 if (verbose)
5356 EMSG(_("E909: Cannot index a special variable"));
5357 return FAIL;
5358 case VAR_UNKNOWN:
5359 if (evaluate)
5360 return FAIL;
5361 /* FALLTHROUGH */
5362
5363 case VAR_STRING:
5364 case VAR_NUMBER:
5365 case VAR_LIST:
5366 case VAR_DICT:
5367 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005368 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005370 init_tv(&var1);
5371 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 /*
5375 * dict.name
5376 */
5377 key = *arg + 1;
5378 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5379 ;
5380 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 }
5384 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 /*
5387 * something[idx]
5388 *
5389 * Get the (first) variable from inside the [].
5390 */
5391 *arg = skipwhite(*arg + 1);
5392 if (**arg == ':')
5393 empty1 = TRUE;
5394 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5395 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005396 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5397 {
5398 /* not a number or string */
5399 clear_tv(&var1);
5400 return FAIL;
5401 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402
5403 /*
5404 * Get the second variable from inside the [:].
5405 */
5406 if (**arg == ':')
5407 {
5408 range = TRUE;
5409 *arg = skipwhite(*arg + 1);
5410 if (**arg == ']')
5411 empty2 = TRUE;
5412 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005414 if (!empty1)
5415 clear_tv(&var1);
5416 return FAIL;
5417 }
5418 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5419 {
5420 /* not a number or string */
5421 if (!empty1)
5422 clear_tv(&var1);
5423 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 return FAIL;
5425 }
5426 }
5427
5428 /* Check for the ']'. */
5429 if (**arg != ']')
5430 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005431 if (verbose)
5432 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 clear_tv(&var1);
5434 if (range)
5435 clear_tv(&var2);
5436 return FAIL;
5437 }
5438 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 }
5440
5441 if (evaluate)
5442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 n1 = 0;
5444 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 n1 = get_tv_number(&var1);
5447 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 }
5449 if (range)
5450 {
5451 if (empty2)
5452 n2 = -1;
5453 else
5454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005455 n2 = get_tv_number(&var2);
5456 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458 }
5459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005462 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005463 case VAR_FUNC:
5464 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005465 case VAR_SPECIAL:
5466 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005467 break; /* not evaluating, skipping over subscript */
5468
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 case VAR_NUMBER:
5470 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005471 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 len = (long)STRLEN(s);
5473 if (range)
5474 {
5475 /* The resulting variable is a substring. If the indexes
5476 * are out of range the result is empty. */
5477 if (n1 < 0)
5478 {
5479 n1 = len + n1;
5480 if (n1 < 0)
5481 n1 = 0;
5482 }
5483 if (n2 < 0)
5484 n2 = len + n2;
5485 else if (n2 >= len)
5486 n2 = len;
5487 if (n1 >= len || n2 < 0 || n1 > n2)
5488 s = NULL;
5489 else
5490 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5491 }
5492 else
5493 {
5494 /* The resulting variable is a string of a single
5495 * character. If the index is too big or negative the
5496 * result is empty. */
5497 if (n1 >= len || n1 < 0)
5498 s = NULL;
5499 else
5500 s = vim_strnsave(s + n1, 1);
5501 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 clear_tv(rettv);
5503 rettv->v_type = VAR_STRING;
5504 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 break;
5506
5507 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 if (n1 < 0)
5510 n1 = len + n1;
5511 if (!empty1 && (n1 < 0 || n1 >= len))
5512 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005513 /* For a range we allow invalid values and return an empty
5514 * list. A list index out of range is an error. */
5515 if (!range)
5516 {
5517 if (verbose)
5518 EMSGN(_(e_listidx), n1);
5519 return FAIL;
5520 }
5521 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 }
5523 if (range)
5524 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005525 list_T *l;
5526 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527
5528 if (n2 < 0)
5529 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005530 else if (n2 >= len)
5531 n2 = len - 1;
5532 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005533 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 l = list_alloc();
5535 if (l == NULL)
5536 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 n1 <= n2; ++n1)
5539 {
5540 if (list_append_tv(l, &item->li_tv) == FAIL)
5541 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005542 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 return FAIL;
5544 }
5545 item = item->li_next;
5546 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 clear_tv(rettv);
5548 rettv->v_type = VAR_LIST;
5549 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005550 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 }
5552 else
5553 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005554 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 clear_tv(rettv);
5556 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 }
5558 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559
5560 case VAR_DICT:
5561 if (range)
5562 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005563 if (verbose)
5564 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005565 if (len == -1)
5566 clear_tv(&var1);
5567 return FAIL;
5568 }
5569 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005570 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571
5572 if (len == -1)
5573 {
5574 key = get_tv_string(&var1);
5575 if (*key == NUL)
5576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005577 if (verbose)
5578 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 clear_tv(&var1);
5580 return FAIL;
5581 }
5582 }
5583
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005584 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005586 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005587 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588 if (len == -1)
5589 clear_tv(&var1);
5590 if (item == NULL)
5591 return FAIL;
5592
5593 copy_tv(&item->di_tv, &var1);
5594 clear_tv(rettv);
5595 *rettv = var1;
5596 }
5597 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 }
5599 }
5600
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 return OK;
5602}
5603
5604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 * Get an option value.
5606 * "arg" points to the '&' or '+' before the option name.
5607 * "arg" is advanced to character after the option name.
5608 * Return OK or FAIL.
5609 */
5610 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005611get_option_tv(
5612 char_u **arg,
5613 typval_T *rettv, /* when NULL, only check if option exists */
5614 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 char_u *option_end;
5617 long numval;
5618 char_u *stringval;
5619 int opt_type;
5620 int c;
5621 int working = (**arg == '+'); /* has("+option") */
5622 int ret = OK;
5623 int opt_flags;
5624
5625 /*
5626 * Isolate the option name and find its value.
5627 */
5628 option_end = find_option_end(arg, &opt_flags);
5629 if (option_end == NULL)
5630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 EMSG2(_("E112: Option name missing: %s"), *arg);
5633 return FAIL;
5634 }
5635
5636 if (!evaluate)
5637 {
5638 *arg = option_end;
5639 return OK;
5640 }
5641
5642 c = *option_end;
5643 *option_end = NUL;
5644 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005645 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
5647 if (opt_type == -3) /* invalid name */
5648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 EMSG2(_("E113: Unknown option: %s"), *arg);
5651 ret = FAIL;
5652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
5655 if (opt_type == -2) /* hidden string option */
5656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 rettv->v_type = VAR_STRING;
5658 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660 else if (opt_type == -1) /* hidden number option */
5661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005662 rettv->v_type = VAR_NUMBER;
5663 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665 else if (opt_type == 1) /* number option */
5666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 rettv->v_type = VAR_NUMBER;
5668 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670 else /* string option */
5671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 rettv->v_type = VAR_STRING;
5673 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 }
5676 else if (working && (opt_type == -2 || opt_type == -1))
5677 ret = FAIL;
5678
5679 *option_end = c; /* put back for error messages */
5680 *arg = option_end;
5681
5682 return ret;
5683}
5684
5685/*
5686 * Allocate a variable for a string constant.
5687 * Return OK or FAIL.
5688 */
5689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005690get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 char_u *p;
5693 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 int extra = 0;
5695
5696 /*
5697 * Find the end of the string, skipping backslashed characters.
5698 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005699 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
5701 if (*p == '\\' && p[1] != NUL)
5702 {
5703 ++p;
5704 /* A "\<x>" form occupies at least 4 characters, and produces up
5705 * to 6 characters: reserve space for 2 extra */
5706 if (*p == '<')
5707 extra += 2;
5708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
5710
5711 if (*p != '"')
5712 {
5713 EMSG2(_("E114: Missing quote: %s"), *arg);
5714 return FAIL;
5715 }
5716
5717 /* If only parsing, set *arg and return here */
5718 if (!evaluate)
5719 {
5720 *arg = p + 1;
5721 return OK;
5722 }
5723
5724 /*
5725 * Copy the string into allocated memory, handling backslashed
5726 * characters.
5727 */
5728 name = alloc((unsigned)(p - *arg + extra));
5729 if (name == NULL)
5730 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 rettv->v_type = VAR_STRING;
5732 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
5736 if (*p == '\\')
5737 {
5738 switch (*++p)
5739 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 case 'b': *name++ = BS; ++p; break;
5741 case 'e': *name++ = ESC; ++p; break;
5742 case 'f': *name++ = FF; ++p; break;
5743 case 'n': *name++ = NL; ++p; break;
5744 case 'r': *name++ = CAR; ++p; break;
5745 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
5747 case 'X': /* hex: "\x1", "\x12" */
5748 case 'x':
5749 case 'u': /* Unicode: "\u0023" */
5750 case 'U':
5751 if (vim_isxdigit(p[1]))
5752 {
5753 int n, nr;
5754 int c = toupper(*p);
5755
5756 if (c == 'X')
5757 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005758 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005760 else
5761 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 nr = 0;
5763 while (--n >= 0 && vim_isxdigit(p[1]))
5764 {
5765 ++p;
5766 nr = (nr << 4) + hex2nr(*p);
5767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769#ifdef FEAT_MBYTE
5770 /* For "\u" store the number according to
5771 * 'encoding'. */
5772 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 else
5775#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 break;
5779
5780 /* octal: "\1", "\12", "\123" */
5781 case '0':
5782 case '1':
5783 case '2':
5784 case '3':
5785 case '4':
5786 case '5':
5787 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 case '7': *name = *p++ - '0';
5789 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 *name = (*name << 3) + *p++ - '0';
5792 if (*p >= '0' && *p <= '7')
5793 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 break;
5797
5798 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 if (extra != 0)
5801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 break;
5804 }
5805 /* FALLTHROUGH */
5806
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 break;
5809 }
5810 }
5811 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 *arg = p + 1;
5817
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 return OK;
5819}
5820
5821/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 * Return OK or FAIL.
5824 */
5825 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005826get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005830 int reduce = 0;
5831
5832 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834 */
5835 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 break;
5841 ++reduce;
5842 ++p;
5843 }
5844 }
5845
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 return FAIL;
5850 }
5851
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 if (!evaluate)
5854 {
5855 *arg = p + 1;
5856 return OK;
5857 }
5858
5859 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 */
5862 str = alloc((unsigned)((p - *arg) - reduce));
5863 if (str == NULL)
5864 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 rettv->v_type = VAR_STRING;
5866 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 break;
5874 ++p;
5875 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 *arg = p + 1;
5880
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 return OK;
5882}
5883
5884/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 * Allocate a variable for a List and fill it from "*arg".
5886 * Return OK or FAIL.
5887 */
5888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005889get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890{
Bram Moolenaar33570922005-01-25 22:26:29 +00005891 list_T *l = NULL;
5892 typval_T tv;
5893 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894
5895 if (evaluate)
5896 {
5897 l = list_alloc();
5898 if (l == NULL)
5899 return FAIL;
5900 }
5901
5902 *arg = skipwhite(*arg + 1);
5903 while (**arg != ']' && **arg != NUL)
5904 {
5905 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5906 goto failret;
5907 if (evaluate)
5908 {
5909 item = listitem_alloc();
5910 if (item != NULL)
5911 {
5912 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005913 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 list_append(l, item);
5915 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005916 else
5917 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 }
5919
5920 if (**arg == ']')
5921 break;
5922 if (**arg != ',')
5923 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 goto failret;
5926 }
5927 *arg = skipwhite(*arg + 1);
5928 }
5929
5930 if (**arg != ']')
5931 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933failret:
5934 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005935 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 return FAIL;
5937 }
5938
5939 *arg = skipwhite(*arg + 1);
5940 if (evaluate)
5941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005942 rettv->v_type = VAR_LIST;
5943 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 ++l->lv_refcount;
5945 }
5946
5947 return OK;
5948}
5949
5950/*
5951 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005952 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005954 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005955list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005957 list_T *l;
5958
5959 l = (list_T *)alloc_clear(sizeof(list_T));
5960 if (l != NULL)
5961 {
5962 /* Prepend the list to the list of lists for garbage collection. */
5963 if (first_list != NULL)
5964 first_list->lv_used_prev = l;
5965 l->lv_used_prev = NULL;
5966 l->lv_used_next = first_list;
5967 first_list = l;
5968 }
5969 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970}
5971
5972/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005973 * Allocate an empty list for a return value.
5974 * Returns OK or FAIL.
5975 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005976 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005977rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005978{
5979 list_T *l = list_alloc();
5980
5981 if (l == NULL)
5982 return FAIL;
5983
5984 rettv->vval.v_list = l;
5985 rettv->v_type = VAR_LIST;
5986 ++l->lv_refcount;
5987 return OK;
5988}
5989
5990/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 * Unreference a list: decrement the reference count and free it when it
5992 * becomes zero.
5993 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005994 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005995list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005997 if (l != NULL && --l->lv_refcount <= 0)
5998 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999}
6000
6001/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006002 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 * Ignores the reference count.
6004 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006006list_free(
6007 list_T *l,
6008 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009{
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006012 /* Remove the list from the list of lists for garbage collection. */
6013 if (l->lv_used_prev == NULL)
6014 first_list = l->lv_used_next;
6015 else
6016 l->lv_used_prev->lv_used_next = l->lv_used_next;
6017 if (l->lv_used_next != NULL)
6018 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6019
Bram Moolenaard9fba312005-06-26 22:34:35 +00006020 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006022 /* Remove the item before deleting it. */
6023 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006024 if (recurse || (item->li_tv.v_type != VAR_LIST
6025 && item->li_tv.v_type != VAR_DICT))
6026 clear_tv(&item->li_tv);
6027 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 }
6029 vim_free(l);
6030}
6031
6032/*
6033 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006034 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006036 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006037listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038{
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040}
6041
6042/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006043 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006046listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006048 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 vim_free(item);
6050}
6051
6052/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006053 * Remove a list item from a List and free it. Also clears the value.
6054 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006056listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006057{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006058 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006059 listitem_free(item);
6060}
6061
6062/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 * Get the number of items in a list.
6064 */
6065 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006066list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 if (l == NULL)
6069 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006070 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071}
6072
6073/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006074 * Return TRUE when two lists have exactly the same values.
6075 */
6076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006077list_equal(
6078 list_T *l1,
6079 list_T *l2,
6080 int ic, /* ignore case for strings */
6081 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082{
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006084
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006085 if (l1 == NULL || l2 == NULL)
6086 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 if (l1 == l2)
6088 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006089 if (list_len(l1) != list_len(l2))
6090 return FALSE;
6091
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 for (item1 = l1->lv_first, item2 = l2->lv_first;
6093 item1 != NULL && item2 != NULL;
6094 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 return FALSE;
6097 return item1 == NULL && item2 == NULL;
6098}
6099
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006100/*
6101 * Return the dictitem that an entry in a hashtable points to.
6102 */
6103 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006104dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006105{
6106 return HI2DI(hi);
6107}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006108
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110 * Return TRUE when two dictionaries have exactly the same key/values.
6111 */
6112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006113dict_equal(
6114 dict_T *d1,
6115 dict_T *d2,
6116 int ic, /* ignore case for strings */
6117 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006118{
Bram Moolenaar33570922005-01-25 22:26:29 +00006119 hashitem_T *hi;
6120 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006121 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006122
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006123 if (d1 == NULL || d2 == NULL)
6124 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006125 if (d1 == d2)
6126 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006127 if (dict_len(d1) != dict_len(d2))
6128 return FALSE;
6129
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006130 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006133 if (!HASHITEM_EMPTY(hi))
6134 {
6135 item2 = dict_find(d2, hi->hi_key, -1);
6136 if (item2 == NULL)
6137 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006138 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 return FALSE;
6140 --todo;
6141 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006142 }
6143 return TRUE;
6144}
6145
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146static int tv_equal_recurse_limit;
6147
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006149 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006150 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006151 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006152 */
6153 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006154tv_equal(
6155 typval_T *tv1,
6156 typval_T *tv2,
6157 int ic, /* ignore case */
6158 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006159{
6160 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006161 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006162 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006163 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006165 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006166 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006167
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006168 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006169 * recursiveness to a limit. We guess they are equal then.
6170 * A fixed limit has the problem of still taking an awful long time.
6171 * Reduce the limit every time running into it. That should work fine for
6172 * deeply linked structures that are not recursively linked and catch
6173 * recursiveness quickly. */
6174 if (!recursive)
6175 tv_equal_recurse_limit = 1000;
6176 if (recursive_cnt >= tv_equal_recurse_limit)
6177 {
6178 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006179 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006181
6182 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 ++recursive_cnt;
6186 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6187 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006188 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006189
6190 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 ++recursive_cnt;
6192 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6193 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006194 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195
6196 case VAR_FUNC:
6197 return (tv1->vval.v_string != NULL
6198 && tv2->vval.v_string != NULL
6199 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6200
6201 case VAR_NUMBER:
6202 return tv1->vval.v_number == tv2->vval.v_number;
6203
6204 case VAR_STRING:
6205 s1 = get_tv_string_buf(tv1, buf1);
6206 s2 = get_tv_string_buf(tv2, buf2);
6207 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006208
6209 case VAR_SPECIAL:
6210 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006211
6212 case VAR_FLOAT:
6213#ifdef FEAT_FLOAT
6214 return tv1->vval.v_float == tv2->vval.v_float;
6215#endif
6216 case VAR_JOB:
6217#ifdef FEAT_JOB
6218 return tv1->vval.v_job == tv2->vval.v_job;
6219#endif
6220 case VAR_UNKNOWN:
6221 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006222 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006223
Bram Moolenaara03f2332016-02-06 18:09:59 +01006224 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6225 * does not equal anything, not even itself. */
6226 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227}
6228
6229/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006230 * Locate item with index "n" in list "l" and return it.
6231 * A negative index is counted from the end; -1 is the last item.
6232 * Returns NULL when "n" is out of range.
6233 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006234 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006235list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236{
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006238 long idx;
6239
6240 if (l == NULL)
6241 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006242
6243 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006245 n = l->lv_len + n;
6246
6247 /* Check for index out of range. */
6248 if (n < 0 || n >= l->lv_len)
6249 return NULL;
6250
6251 /* When there is a cached index may start search from there. */
6252 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006254 if (n < l->lv_idx / 2)
6255 {
6256 /* closest to the start of the list */
6257 item = l->lv_first;
6258 idx = 0;
6259 }
6260 else if (n > (l->lv_idx + l->lv_len) / 2)
6261 {
6262 /* closest to the end of the list */
6263 item = l->lv_last;
6264 idx = l->lv_len - 1;
6265 }
6266 else
6267 {
6268 /* closest to the cached index */
6269 item = l->lv_idx_item;
6270 idx = l->lv_idx;
6271 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272 }
6273 else
6274 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006275 if (n < l->lv_len / 2)
6276 {
6277 /* closest to the start of the list */
6278 item = l->lv_first;
6279 idx = 0;
6280 }
6281 else
6282 {
6283 /* closest to the end of the list */
6284 item = l->lv_last;
6285 idx = l->lv_len - 1;
6286 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006288
6289 while (n > idx)
6290 {
6291 /* search forward */
6292 item = item->li_next;
6293 ++idx;
6294 }
6295 while (n < idx)
6296 {
6297 /* search backward */
6298 item = item->li_prev;
6299 --idx;
6300 }
6301
6302 /* cache the used index */
6303 l->lv_idx = idx;
6304 l->lv_idx_item = item;
6305
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006306 return item;
6307}
6308
6309/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006310 * Get list item "l[idx]" as a number.
6311 */
6312 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006313list_find_nr(
6314 list_T *l,
6315 long idx,
6316 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006317{
6318 listitem_T *li;
6319
6320 li = list_find(l, idx);
6321 if (li == NULL)
6322 {
6323 if (errorp != NULL)
6324 *errorp = TRUE;
6325 return -1L;
6326 }
6327 return get_tv_number_chk(&li->li_tv, errorp);
6328}
6329
6330/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006331 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6332 */
6333 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006334list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006335{
6336 listitem_T *li;
6337
6338 li = list_find(l, idx - 1);
6339 if (li == NULL)
6340 {
6341 EMSGN(_(e_listidx), idx);
6342 return NULL;
6343 }
6344 return get_tv_string(&li->li_tv);
6345}
6346
6347/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006348 * Locate "item" list "l" and return its index.
6349 * Returns -1 when "item" is not in the list.
6350 */
6351 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006352list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006353{
6354 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006355 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006356
6357 if (l == NULL)
6358 return -1;
6359 idx = 0;
6360 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6361 ++idx;
6362 if (li == NULL)
6363 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006364 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006365}
6366
6367/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 * Append item "item" to the end of list "l".
6369 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006370 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006371list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372{
6373 if (l->lv_last == NULL)
6374 {
6375 /* empty list */
6376 l->lv_first = item;
6377 l->lv_last = item;
6378 item->li_prev = NULL;
6379 }
6380 else
6381 {
6382 l->lv_last->li_next = item;
6383 item->li_prev = l->lv_last;
6384 l->lv_last = item;
6385 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006386 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 item->li_next = NULL;
6388}
6389
6390/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006391 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006392 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006395list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006397 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398
Bram Moolenaar05159a02005-02-26 23:04:13 +00006399 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006401 copy_tv(tv, &li->li_tv);
6402 list_append(l, li);
6403 return OK;
6404}
6405
6406/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006407 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006408 * Return FAIL when out of memory.
6409 */
6410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006411list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006412{
6413 listitem_T *li = listitem_alloc();
6414
6415 if (li == NULL)
6416 return FAIL;
6417 li->li_tv.v_type = VAR_DICT;
6418 li->li_tv.v_lock = 0;
6419 li->li_tv.vval.v_dict = dict;
6420 list_append(list, li);
6421 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 return OK;
6423}
6424
6425/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006426 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006427 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006428 * Returns FAIL when out of memory.
6429 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006431list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006432{
6433 listitem_T *li = listitem_alloc();
6434
6435 if (li == NULL)
6436 return FAIL;
6437 list_append(l, li);
6438 li->li_tv.v_type = VAR_STRING;
6439 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006440 if (str == NULL)
6441 li->li_tv.vval.v_string = NULL;
6442 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006443 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006444 return FAIL;
6445 return OK;
6446}
6447
6448/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006449 * Append "n" to list "l".
6450 * Returns FAIL when out of memory.
6451 */
6452 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006453list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006454{
6455 listitem_T *li;
6456
6457 li = listitem_alloc();
6458 if (li == NULL)
6459 return FAIL;
6460 li->li_tv.v_type = VAR_NUMBER;
6461 li->li_tv.v_lock = 0;
6462 li->li_tv.vval.v_number = n;
6463 list_append(l, li);
6464 return OK;
6465}
6466
6467/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469 * If "item" is NULL append at the end.
6470 * Return FAIL when out of memory.
6471 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006473list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474{
Bram Moolenaar33570922005-01-25 22:26:29 +00006475 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476
6477 if (ni == NULL)
6478 return FAIL;
6479 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006480 list_insert(l, ni, item);
6481 return OK;
6482}
6483
6484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006485list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006486{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006487 if (item == NULL)
6488 /* Append new item at end of list. */
6489 list_append(l, ni);
6490 else
6491 {
6492 /* Insert new item before existing item. */
6493 ni->li_prev = item->li_prev;
6494 ni->li_next = item;
6495 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006496 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006498 ++l->lv_idx;
6499 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006503 l->lv_idx_item = NULL;
6504 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006506 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508}
6509
6510/*
6511 * Extend "l1" with "l2".
6512 * If "bef" is NULL append at the end, otherwise insert before this item.
6513 * Returns FAIL when out of memory.
6514 */
6515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006516list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517{
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006519 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006521 /* We also quit the loop when we have inserted the original item count of
6522 * the list, avoid a hang when we extend a list with itself. */
6523 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6525 return FAIL;
6526 return OK;
6527}
6528
6529/*
6530 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6531 * Return FAIL when out of memory.
6532 */
6533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535{
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006538 if (l1 == NULL || l2 == NULL)
6539 return FAIL;
6540
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006542 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 if (l == NULL)
6544 return FAIL;
6545 tv->v_type = VAR_LIST;
6546 tv->vval.v_list = l;
6547
6548 /* append all items from the second list */
6549 return list_extend(l, l2, NULL);
6550}
6551
6552/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 * Returns NULL when out of memory.
6557 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006558 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560{
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 list_T *copy;
6562 listitem_T *item;
6563 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564
6565 if (orig == NULL)
6566 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567
6568 copy = list_alloc();
6569 if (copy != NULL)
6570 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006571 if (copyID != 0)
6572 {
6573 /* Do this before adding the items, because one of the items may
6574 * refer back to this list. */
6575 orig->lv_copyID = copyID;
6576 orig->lv_copylist = copy;
6577 }
6578 for (item = orig->lv_first; item != NULL && !got_int;
6579 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 {
6581 ni = listitem_alloc();
6582 if (ni == NULL)
6583 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 {
6586 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6587 {
6588 vim_free(ni);
6589 break;
6590 }
6591 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006593 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 list_append(copy, ni);
6595 }
6596 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 if (item != NULL)
6598 {
6599 list_unref(copy);
6600 copy = NULL;
6601 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 }
6603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604 return copy;
6605}
6606
6607/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006608 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006609 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006610 * This used to be called list_remove, but that conflicts with a Sun header
6611 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006614vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006615{
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006618 /* notify watchers */
6619 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006621 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006622 list_fix_watch(l, ip);
6623 if (ip == item2)
6624 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626
6627 if (item2->li_next == NULL)
6628 l->lv_last = item->li_prev;
6629 else
6630 item2->li_next->li_prev = item->li_prev;
6631 if (item->li_prev == NULL)
6632 l->lv_first = item2->li_next;
6633 else
6634 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006635 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636}
6637
6638/*
6639 * Return an allocated string with the string representation of a list.
6640 * May return NULL.
6641 */
6642 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006643list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644{
6645 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646
6647 if (tv->vval.v_list == NULL)
6648 return NULL;
6649 ga_init2(&ga, (int)sizeof(char), 80);
6650 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006651 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 {
6653 vim_free(ga.ga_data);
6654 return NULL;
6655 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656 ga_append(&ga, ']');
6657 ga_append(&ga, NUL);
6658 return (char_u *)ga.ga_data;
6659}
6660
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006661typedef struct join_S {
6662 char_u *s;
6663 char_u *tofree;
6664} join_T;
6665
6666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006667list_join_inner(
6668 garray_T *gap, /* to store the result in */
6669 list_T *l,
6670 char_u *sep,
6671 int echo_style,
6672 int copyID,
6673 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006674{
6675 int i;
6676 join_T *p;
6677 int len;
6678 int sumlen = 0;
6679 int first = TRUE;
6680 char_u *tofree;
6681 char_u numbuf[NUMBUFLEN];
6682 listitem_T *item;
6683 char_u *s;
6684
6685 /* Stringify each item in the list. */
6686 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6687 {
6688 if (echo_style)
6689 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6690 else
6691 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6692 if (s == NULL)
6693 return FAIL;
6694
6695 len = (int)STRLEN(s);
6696 sumlen += len;
6697
Bram Moolenaarcde88542015-08-11 19:14:00 +02006698 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6700 if (tofree != NULL || s != numbuf)
6701 {
6702 p->s = s;
6703 p->tofree = tofree;
6704 }
6705 else
6706 {
6707 p->s = vim_strnsave(s, len);
6708 p->tofree = p->s;
6709 }
6710
6711 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006712 if (did_echo_string_emsg) /* recursion error, bail out */
6713 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006714 }
6715
6716 /* Allocate result buffer with its total size, avoid re-allocation and
6717 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6718 if (join_gap->ga_len >= 2)
6719 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6720 if (ga_grow(gap, sumlen + 2) == FAIL)
6721 return FAIL;
6722
6723 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6724 {
6725 if (first)
6726 first = FALSE;
6727 else
6728 ga_concat(gap, sep);
6729 p = ((join_T *)join_gap->ga_data) + i;
6730
6731 if (p->s != NULL)
6732 ga_concat(gap, p->s);
6733 line_breakcheck();
6734 }
6735
6736 return OK;
6737}
6738
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006739/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006740 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006741 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006742 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006745list_join(
6746 garray_T *gap,
6747 list_T *l,
6748 char_u *sep,
6749 int echo_style,
6750 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006751{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006752 garray_T join_ga;
6753 int retval;
6754 join_T *p;
6755 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006756
Bram Moolenaard39a7512015-04-16 22:51:22 +02006757 if (l->lv_len < 1)
6758 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006759 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6760 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6761
6762 /* Dispose each item in join_ga. */
6763 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006764 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006765 p = (join_T *)join_ga.ga_data;
6766 for (i = 0; i < join_ga.ga_len; ++i)
6767 {
6768 vim_free(p->tofree);
6769 ++p;
6770 }
6771 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006772 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006773
6774 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006775}
6776
6777/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006778 * Return the next (unique) copy ID.
6779 * Used for serializing nested structures.
6780 */
6781 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006782get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006783{
6784 current_copyID += COPYID_INC;
6785 return current_copyID;
6786}
6787
6788/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006789 * Garbage collection for lists and dictionaries.
6790 *
6791 * We use reference counts to be able to free most items right away when they
6792 * are no longer used. But for composite items it's possible that it becomes
6793 * unused while the reference count is > 0: When there is a recursive
6794 * reference. Example:
6795 * :let l = [1, 2, 3]
6796 * :let d = {9: l}
6797 * :let l[1] = d
6798 *
6799 * Since this is quite unusual we handle this with garbage collection: every
6800 * once in a while find out which lists and dicts are not referenced from any
6801 * variable.
6802 *
6803 * Here is a good reference text about garbage collection (refers to Python
6804 * but it applies to all reference-counting mechanisms):
6805 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006806 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006807
6808/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 * Do garbage collection for lists and dicts.
6810 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006811 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006813garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006814{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006815 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006816 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006817 buf_T *buf;
6818 win_T *wp;
6819 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006820 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006821 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006822 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006823#ifdef FEAT_WINDOWS
6824 tabpage_T *tp;
6825#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006827 /* Only do this once. */
6828 want_garbage_collect = FALSE;
6829 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006830 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006831
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006832 /* We advance by two because we add one for items referenced through
6833 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006834 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006836 /*
6837 * 1. Go through all accessible variables and mark all lists and dicts
6838 * with copyID.
6839 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006840
6841 /* Don't free variables in the previous_funccal list unless they are only
6842 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006843 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6845 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006846 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6847 NULL);
6848 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6849 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006850 }
6851
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 /* script-local variables */
6853 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006854 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855
6856 /* buffer-local variables */
6857 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006858 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6859 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860
6861 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006862 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006863 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6864 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006865#ifdef FEAT_AUTOCMD
6866 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006867 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6868 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006869#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006871#ifdef FEAT_WINDOWS
6872 /* tabpage-local variables */
6873 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6875 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006876#endif
6877
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880
6881 /* function-local variables */
6882 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6883 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6885 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 }
6887
Bram Moolenaard812df62008-11-09 12:46:09 +00006888 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006890
Bram Moolenaar1dced572012-04-05 16:54:08 +02006891#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006893#endif
6894
Bram Moolenaardb913952012-06-29 12:54:53 +02006895#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006897#endif
6898
6899#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006901#endif
6902
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006903#ifdef FEAT_CHANNEL
6904 abort = abort || set_ref_in_channel(copyID);
6905#endif
6906
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006908 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 /*
6910 * 2. Free lists and dictionaries that are not referenced.
6911 */
6912 did_free = free_unref_items(copyID);
6913
6914 /*
6915 * 3. Check if any funccal can be freed now.
6916 */
6917 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006918 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 if (can_free_funccal(*pfc, copyID))
6920 {
6921 fc = *pfc;
6922 *pfc = fc->caller;
6923 free_funccal(fc, TRUE);
6924 did_free = TRUE;
6925 did_free_funccal = TRUE;
6926 }
6927 else
6928 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006929 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 if (did_free_funccal)
6931 /* When a funccal was freed some more items might be garbage
6932 * collected, so run again. */
6933 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 else if (p_verbose > 0)
6936 {
6937 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6938 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006939
6940 return did_free;
6941}
6942
6943/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006944 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006945 */
6946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006947free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006949 dict_T *dd, *dd_next;
6950 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951 int did_free = FALSE;
6952
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006955 */
6956 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006957 {
6958 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006961 /* Free the Dictionary and ordinary items it contains, but don't
6962 * recurse into Lists and Dictionaries, they will be in the list
6963 * of dicts or list of lists. */
6964 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006967 dd = dd_next;
6968 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969
6970 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 * Go through the list of lists and free items without the copyID.
6972 * But don't free a list that has a watcher (used in a for loop), these
6973 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 */
6975 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006976 {
6977 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6979 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006981 /* Free the List and ordinary items it contains, but don't recurse
6982 * into Lists and Dictionaries, they will be in the list of dicts
6983 * or list of lists. */
6984 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006987 ll = ll_next;
6988 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006989
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 return did_free;
6991}
6992
6993/*
6994 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006995 * "list_stack" is used to add lists to be marked. Can be NULL.
6996 *
6997 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006999 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007000set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001{
7002 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007005 hashtab_T *cur_ht;
7006 ht_stack_T *ht_stack = NULL;
7007 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007009 cur_ht = ht;
7010 for (;;)
7011 {
7012 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007014 /* Mark each item in the hashtab. If the item contains a hashtab
7015 * it is added to ht_stack, if it contains a list it is added to
7016 * list_stack. */
7017 todo = (int)cur_ht->ht_used;
7018 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7019 if (!HASHITEM_EMPTY(hi))
7020 {
7021 --todo;
7022 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7023 &ht_stack, list_stack);
7024 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007026
7027 if (ht_stack == NULL)
7028 break;
7029
7030 /* take an item from the stack */
7031 cur_ht = ht_stack->ht;
7032 tempitem = ht_stack;
7033 ht_stack = ht_stack->prev;
7034 free(tempitem);
7035 }
7036
7037 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038}
7039
7040/*
7041 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7043 *
7044 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007047set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 listitem_T *li;
7050 int abort = FALSE;
7051 list_T *cur_l;
7052 list_stack_T *list_stack = NULL;
7053 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007054
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 cur_l = l;
7056 for (;;)
7057 {
7058 if (!abort)
7059 /* Mark each item in the list. If the item contains a hashtab
7060 * it is added to ht_stack, if it contains a list it is added to
7061 * list_stack. */
7062 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7063 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7064 ht_stack, &list_stack);
7065 if (list_stack == NULL)
7066 break;
7067
7068 /* take an item from the stack */
7069 cur_l = list_stack->list;
7070 tempitem = list_stack;
7071 list_stack = list_stack->prev;
7072 free(tempitem);
7073 }
7074
7075 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076}
7077
7078/*
7079 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 * "list_stack" is used to add lists to be marked. Can be NULL.
7081 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7082 *
7083 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007086set_ref_in_item(
7087 typval_T *tv,
7088 int copyID,
7089 ht_stack_T **ht_stack,
7090 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007091{
7092 dict_T *dd;
7093 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007095
Bram Moolenaara03f2332016-02-06 18:09:59 +01007096 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007097 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007098 dd = tv->vval.v_dict;
7099 if (dd != NULL && dd->dv_copyID != copyID)
7100 {
7101 /* Didn't see this dict yet. */
7102 dd->dv_copyID = copyID;
7103 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007104 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007105 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7106 }
7107 else
7108 {
7109 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7110 if (newitem == NULL)
7111 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007112 else
7113 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007114 newitem->ht = &dd->dv_hashtab;
7115 newitem->prev = *ht_stack;
7116 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007117 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007118 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007119 }
7120 }
7121 else if (tv->v_type == VAR_LIST)
7122 {
7123 ll = tv->vval.v_list;
7124 if (ll != NULL && ll->lv_copyID != copyID)
7125 {
7126 /* Didn't see this list yet. */
7127 ll->lv_copyID = copyID;
7128 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007129 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007130 abort = set_ref_in_list(ll, copyID, ht_stack);
7131 }
7132 else
7133 {
7134 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007136 if (newitem == NULL)
7137 abort = TRUE;
7138 else
7139 {
7140 newitem->list = ll;
7141 newitem->prev = *list_stack;
7142 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007143 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007144 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007145 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007146 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007147 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007148}
7149
7150/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007151 * Allocate an empty header for a dictionary.
7152 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007153 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007154dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155{
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007160 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007161 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007162 if (first_dict != NULL)
7163 first_dict->dv_used_prev = d;
7164 d->dv_used_next = first_dict;
7165 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007166 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007167
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007169 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007170 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007171 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007173 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175}
7176
7177/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007178 * Allocate an empty dict for a return value.
7179 * Returns OK or FAIL.
7180 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007181 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007182rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007183{
7184 dict_T *d = dict_alloc();
7185
7186 if (d == NULL)
7187 return FAIL;
7188
7189 rettv->vval.v_dict = d;
7190 rettv->v_type = VAR_DICT;
7191 ++d->dv_refcount;
7192 return OK;
7193}
7194
7195
7196/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197 * Unreference a Dictionary: decrement the reference count and free it when it
7198 * becomes zero.
7199 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007201dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007203 if (d != NULL && --d->dv_refcount <= 0)
7204 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205}
7206
7207/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007208 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209 * Ignores the reference count.
7210 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007212dict_free(
7213 dict_T *d,
7214 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007217 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007218 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007220 /* Remove the dict from the list of dicts for garbage collection. */
7221 if (d->dv_used_prev == NULL)
7222 first_dict = d->dv_used_next;
7223 else
7224 d->dv_used_prev->dv_used_next = d->dv_used_next;
7225 if (d->dv_used_next != NULL)
7226 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7227
7228 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007229 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007230 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007231 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007233 if (!HASHITEM_EMPTY(hi))
7234 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007235 /* Remove the item before deleting it, just in case there is
7236 * something recursive causing trouble. */
7237 di = HI2DI(hi);
7238 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007239 if (recurse || (di->di_tv.v_type != VAR_LIST
7240 && di->di_tv.v_type != VAR_DICT))
7241 clear_tv(&di->di_tv);
7242 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007243 --todo;
7244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 vim_free(d);
7248}
7249
7250/*
7251 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252 * The "key" is copied to the new item.
7253 * Note that the value of the item "di_tv" still needs to be initialized!
7254 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007256 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007257dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258{
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007260
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007261 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007262 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007263 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007265 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007266 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007267 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268}
7269
7270/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007271 * Make a copy of a Dictionary item.
7272 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007274dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275{
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007278 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7279 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280 if (di != NULL)
7281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007283 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284 copy_tv(&org->di_tv, &di->di_tv);
7285 }
7286 return di;
7287}
7288
7289/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 * Remove item "item" from Dictionary "dict" and free it.
7291 */
7292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007293dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294{
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 if (HASHITEM_EMPTY(hi))
7299 EMSG2(_(e_intern2), "dictitem_remove()");
7300 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 dictitem_free(item);
7303}
7304
7305/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 * Free a dict item. Also clears the value.
7307 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007309dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007312 if (item->di_flags & DI_FLAGS_ALLOC)
7313 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314}
7315
7316/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7318 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007319 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 * Returns NULL when out of memory.
7321 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007323dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324{
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 dict_T *copy;
7326 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329
7330 if (orig == NULL)
7331 return NULL;
7332
7333 copy = dict_alloc();
7334 if (copy != NULL)
7335 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007336 if (copyID != 0)
7337 {
7338 orig->dv_copyID = copyID;
7339 orig->dv_copydict = copy;
7340 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007341 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007342 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007346 --todo;
7347
7348 di = dictitem_alloc(hi->hi_key);
7349 if (di == NULL)
7350 break;
7351 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007352 {
7353 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7354 copyID) == FAIL)
7355 {
7356 vim_free(di);
7357 break;
7358 }
7359 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 else
7361 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7362 if (dict_add(copy, di) == FAIL)
7363 {
7364 dictitem_free(di);
7365 break;
7366 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007371 if (todo > 0)
7372 {
7373 dict_unref(copy);
7374 copy = NULL;
7375 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 }
7377
7378 return copy;
7379}
7380
7381/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007383 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007384 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007386dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387{
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389}
7390
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007392 * Add a number or string entry to dictionary "d".
7393 * When "str" is NULL use number "nr", otherwise use "str".
7394 * Returns FAIL when out of memory and when key already exists.
7395 */
7396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007397dict_add_nr_str(
7398 dict_T *d,
7399 char *key,
7400 long nr,
7401 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007402{
7403 dictitem_T *item;
7404
7405 item = dictitem_alloc((char_u *)key);
7406 if (item == NULL)
7407 return FAIL;
7408 item->di_tv.v_lock = 0;
7409 if (str == NULL)
7410 {
7411 item->di_tv.v_type = VAR_NUMBER;
7412 item->di_tv.vval.v_number = nr;
7413 }
7414 else
7415 {
7416 item->di_tv.v_type = VAR_STRING;
7417 item->di_tv.vval.v_string = vim_strsave(str);
7418 }
7419 if (dict_add(d, item) == FAIL)
7420 {
7421 dictitem_free(item);
7422 return FAIL;
7423 }
7424 return OK;
7425}
7426
7427/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007428 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007429 * Returns FAIL when out of memory and when key already exists.
7430 */
7431 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007432dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007433{
7434 dictitem_T *item;
7435
7436 item = dictitem_alloc((char_u *)key);
7437 if (item == NULL)
7438 return FAIL;
7439 item->di_tv.v_lock = 0;
7440 item->di_tv.v_type = VAR_LIST;
7441 item->di_tv.vval.v_list = list;
7442 if (dict_add(d, item) == FAIL)
7443 {
7444 dictitem_free(item);
7445 return FAIL;
7446 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007447 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007448 return OK;
7449}
7450
7451/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007452 * Get the number of items in a Dictionary.
7453 */
7454 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007455dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007456{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 if (d == NULL)
7458 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007459 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007460}
7461
7462/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 * Find item "key[len]" in Dictionary "d".
7464 * If "len" is negative use strlen(key).
7465 * Returns NULL when not found.
7466 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007467 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007468dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007469{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007470#define AKEYLEN 200
7471 char_u buf[AKEYLEN];
7472 char_u *akey;
7473 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007476 if (len < 0)
7477 akey = key;
7478 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007479 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 tofree = akey = vim_strnsave(key, len);
7481 if (akey == NULL)
7482 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007483 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007484 else
7485 {
7486 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007487 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 akey = buf;
7489 }
7490
Bram Moolenaar33570922005-01-25 22:26:29 +00007491 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492 vim_free(tofree);
7493 if (HASHITEM_EMPTY(hi))
7494 return NULL;
7495 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496}
7497
7498/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007499 * Get a string item from a dictionary.
7500 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007501 * Returns NULL if the entry doesn't exist or out of memory.
7502 */
7503 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007504get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007505{
7506 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007507 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007508
7509 di = dict_find(d, key, -1);
7510 if (di == NULL)
7511 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007512 s = get_tv_string(&di->di_tv);
7513 if (save && s != NULL)
7514 s = vim_strsave(s);
7515 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007516}
7517
7518/*
7519 * Get a number item from a dictionary.
7520 * Returns 0 if the entry doesn't exist or out of memory.
7521 */
7522 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007523get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007524{
7525 dictitem_T *di;
7526
7527 di = dict_find(d, key, -1);
7528 if (di == NULL)
7529 return 0;
7530 return get_tv_number(&di->di_tv);
7531}
7532
7533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 * Return an allocated string with the string representation of a Dictionary.
7535 * May return NULL.
7536 */
7537 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007538dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539{
7540 garray_T ga;
7541 int first = TRUE;
7542 char_u *tofree;
7543 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007549 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 return NULL;
7551 ga_init2(&ga, (int)sizeof(char), 80);
7552 ga_append(&ga, '{');
7553
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007554 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007555 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 --todo;
7560
7561 if (first)
7562 first = FALSE;
7563 else
7564 ga_concat(&ga, (char_u *)", ");
7565
7566 tofree = string_quote(hi->hi_key, FALSE);
7567 if (tofree != NULL)
7568 {
7569 ga_concat(&ga, tofree);
7570 vim_free(tofree);
7571 }
7572 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007573 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 if (s != NULL)
7575 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007577 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007578 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007579 line_breakcheck();
7580
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007583 if (todo > 0)
7584 {
7585 vim_free(ga.ga_data);
7586 return NULL;
7587 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588
7589 ga_append(&ga, '}');
7590 ga_append(&ga, NUL);
7591 return (char_u *)ga.ga_data;
7592}
7593
7594/*
7595 * Allocate a variable for a Dictionary and fill it from "*arg".
7596 * Return OK or FAIL. Returns NOTDONE for {expr}.
7597 */
7598 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007599get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600{
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 dict_T *d = NULL;
7602 typval_T tvkey;
7603 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007604 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007607 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608
7609 /*
7610 * First check if it's not a curly-braces thing: {expr}.
7611 * Must do this without evaluating, otherwise a function may be called
7612 * twice. Unfortunately this means we need to call eval1() twice for the
7613 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007616 if (*start != '}')
7617 {
7618 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7619 return FAIL;
7620 if (*start == '}')
7621 return NOTDONE;
7622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623
7624 if (evaluate)
7625 {
7626 d = dict_alloc();
7627 if (d == NULL)
7628 return FAIL;
7629 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 tvkey.v_type = VAR_UNKNOWN;
7631 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632
7633 *arg = skipwhite(*arg + 1);
7634 while (**arg != '}' && **arg != NUL)
7635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 goto failret;
7638 if (**arg != ':')
7639 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007640 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007641 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642 goto failret;
7643 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007644 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007646 key = get_tv_string_buf_chk(&tvkey, buf);
7647 if (key == NULL || *key == NUL)
7648 {
7649 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7650 if (key != NULL)
7651 EMSG(_(e_emptykey));
7652 clear_tv(&tvkey);
7653 goto failret;
7654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656
7657 *arg = skipwhite(*arg + 1);
7658 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7659 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007660 if (evaluate)
7661 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 goto failret;
7663 }
7664 if (evaluate)
7665 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007666 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 if (item != NULL)
7668 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007669 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007670 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 clear_tv(&tv);
7672 goto failret;
7673 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007674 item = dictitem_alloc(key);
7675 clear_tv(&tvkey);
7676 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007679 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007680 if (dict_add(d, item) == FAIL)
7681 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 }
7683 }
7684
7685 if (**arg == '}')
7686 break;
7687 if (**arg != ',')
7688 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007689 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 goto failret;
7691 }
7692 *arg = skipwhite(*arg + 1);
7693 }
7694
7695 if (**arg != '}')
7696 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007697 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698failret:
7699 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007700 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701 return FAIL;
7702 }
7703
7704 *arg = skipwhite(*arg + 1);
7705 if (evaluate)
7706 {
7707 rettv->v_type = VAR_DICT;
7708 rettv->vval.v_dict = d;
7709 ++d->dv_refcount;
7710 }
7711
7712 return OK;
7713}
7714
Bram Moolenaar835dc632016-02-07 14:27:38 +01007715#ifdef FEAT_JOB
7716 static void
7717job_free(job_T *job)
7718{
7719 /* TODO: free any handles */
7720
7721 vim_free(job);
7722}
7723
7724 static void
7725job_unref(job_T *job)
7726{
7727 if (job != NULL && --job->jv_refcount <= 0)
7728 job_free(job);
7729}
7730
7731/*
7732 * Allocate a job. Sets the refcount to one.
7733 */
7734 static job_T *
7735job_alloc(void)
7736{
7737 job_T *job;
7738
7739 job = (job_T *)alloc_clear(sizeof(job_T));
7740 if (job != NULL)
7741 {
7742 job->jv_refcount = 1;
7743 }
7744 return job;
7745}
7746
7747#endif
7748
Bram Moolenaar17a13432016-01-24 14:22:10 +01007749 static char *
7750get_var_special_name(int nr)
7751{
7752 switch (nr)
7753 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007754 case VVAL_FALSE: return "v:false";
7755 case VVAL_TRUE: return "v:true";
7756 case VVAL_NONE: return "v:none";
7757 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007758 }
7759 EMSG2(_(e_intern2), "get_var_special_name()");
7760 return "42";
7761}
7762
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007764 * Return a string with the string representation of a variable.
7765 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007766 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007767 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007768 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007769 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007770 */
7771 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007772echo_string(
7773 typval_T *tv,
7774 char_u **tofree,
7775 char_u *numbuf,
7776 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007777{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007778 static int recurse = 0;
7779 char_u *r = NULL;
7780
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007782 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007783 if (!did_echo_string_emsg)
7784 {
7785 /* Only give this message once for a recursive call to avoid
7786 * flooding the user with errors. And stop iterating over lists
7787 * and dicts. */
7788 did_echo_string_emsg = TRUE;
7789 EMSG(_("E724: variable nested too deep for displaying"));
7790 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007791 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007792 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007793 }
7794 ++recurse;
7795
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796 switch (tv->v_type)
7797 {
7798 case VAR_FUNC:
7799 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007800 r = tv->vval.v_string;
7801 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007802
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804 if (tv->vval.v_list == NULL)
7805 {
7806 *tofree = NULL;
7807 r = NULL;
7808 }
7809 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7810 {
7811 *tofree = NULL;
7812 r = (char_u *)"[...]";
7813 }
7814 else
7815 {
7816 tv->vval.v_list->lv_copyID = copyID;
7817 *tofree = list2string(tv, copyID);
7818 r = *tofree;
7819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007821
Bram Moolenaar8c711452005-01-14 21:53:12 +00007822 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007823 if (tv->vval.v_dict == NULL)
7824 {
7825 *tofree = NULL;
7826 r = NULL;
7827 }
7828 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7829 {
7830 *tofree = NULL;
7831 r = (char_u *)"{...}";
7832 }
7833 else
7834 {
7835 tv->vval.v_dict->dv_copyID = copyID;
7836 *tofree = dict2string(tv, copyID);
7837 r = *tofree;
7838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007839 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 case VAR_STRING:
7842 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007843 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007844 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 *tofree = NULL;
7846 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007850#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007851 *tofree = NULL;
7852 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7853 r = numbuf;
7854 break;
7855#endif
7856
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007857 case VAR_SPECIAL:
7858 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007859 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007860 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007861 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007862
Bram Moolenaar8502c702014-06-17 12:51:16 +02007863 if (--recurse == 0)
7864 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007865 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007866}
7867
7868/*
7869 * Return a string with the string representation of a variable.
7870 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7871 * "numbuf" is used for a number.
7872 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007873 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007874 */
7875 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007876tv2string(
7877 typval_T *tv,
7878 char_u **tofree,
7879 char_u *numbuf,
7880 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007881{
7882 switch (tv->v_type)
7883 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884 case VAR_FUNC:
7885 *tofree = string_quote(tv->vval.v_string, TRUE);
7886 return *tofree;
7887 case VAR_STRING:
7888 *tofree = string_quote(tv->vval.v_string, FALSE);
7889 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007890#ifdef FEAT_FLOAT
7891 case VAR_FLOAT:
7892 *tofree = NULL;
7893 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7894 return numbuf;
7895#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007898 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007899 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007900 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007901 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007904 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007905}
7906
7907/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007908 * Return string "str" in ' quotes, doubling ' characters.
7909 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007910 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 */
7912 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007913string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914{
Bram Moolenaar33570922005-01-25 22:26:29 +00007915 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916 char_u *p, *r, *s;
7917
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 len = (function ? 13 : 3);
7919 if (str != NULL)
7920 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007921 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 for (p = str; *p != NUL; mb_ptr_adv(p))
7923 if (*p == '\'')
7924 ++len;
7925 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 s = r = alloc(len);
7927 if (r != NULL)
7928 {
7929 if (function)
7930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007932 r += 10;
7933 }
7934 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 if (str != NULL)
7937 for (p = str; *p != NUL; )
7938 {
7939 if (*p == '\'')
7940 *r++ = '\'';
7941 MB_COPY_CHAR(p, r);
7942 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 if (function)
7945 *r++ = ')';
7946 *r++ = NUL;
7947 }
7948 return s;
7949}
7950
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007951#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007952/*
7953 * Convert the string "text" to a floating point number.
7954 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7955 * this always uses a decimal point.
7956 * Returns the length of the text that was consumed.
7957 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007959string2float(
7960 char_u *text,
7961 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007962{
7963 char *s = (char *)text;
7964 float_T f;
7965
7966 f = strtod(s, &s);
7967 *value = f;
7968 return (int)((char_u *)s - text);
7969}
7970#endif
7971
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 * Get the value of an environment variable.
7974 * "arg" is pointing to the '$'. It is advanced to after the name.
7975 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007976 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 */
7978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007979get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 char_u *string = NULL;
7982 int len;
7983 int cc;
7984 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007985 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986
7987 ++*arg;
7988 name = *arg;
7989 len = get_env_len(arg);
7990 if (evaluate)
7991 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007992 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007993 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007994
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007995 cc = name[len];
7996 name[len] = NUL;
7997 /* first try vim_getenv(), fast for normal environment vars */
7998 string = vim_getenv(name, &mustfree);
7999 if (string != NULL && *string != NUL)
8000 {
8001 if (!mustfree)
8002 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008004 else
8005 {
8006 if (mustfree)
8007 vim_free(string);
8008
8009 /* next try expanding things like $VIM and ${HOME} */
8010 string = expand_env_save(name - 1);
8011 if (string != NULL && *string == '$')
8012 {
8013 vim_free(string);
8014 string = NULL;
8015 }
8016 }
8017 name[len] = cc;
8018
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019 rettv->v_type = VAR_STRING;
8020 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
8022
8023 return OK;
8024}
8025
8026/*
8027 * Array with names and number of arguments of all internal functions
8028 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8029 */
8030static struct fst
8031{
8032 char *f_name; /* function name */
8033 char f_min_argc; /* minimal number of arguments */
8034 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008035 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008036 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037} functions[] =
8038{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008039#ifdef FEAT_FLOAT
8040 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008041 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008043 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008044 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008045 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 {"append", 2, 2, f_append},
8047 {"argc", 0, 0, f_argc},
8048 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008049 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008050 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008051#ifdef FEAT_FLOAT
8052 {"asin", 1, 1, f_asin}, /* WJMc */
8053#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008054 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008055 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008056 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008057 {"assert_false", 1, 2, f_assert_false},
8058 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008059#ifdef FEAT_FLOAT
8060 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008061 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008064 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {"bufexists", 1, 1, f_bufexists},
8066 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8067 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8068 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8069 {"buflisted", 1, 1, f_buflisted},
8070 {"bufloaded", 1, 1, f_bufloaded},
8071 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008072 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"bufwinnr", 1, 1, f_bufwinnr},
8074 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008075 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008076 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008077 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#ifdef FEAT_FLOAT
8079 {"ceil", 1, 1, f_ceil},
8080#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008081#ifdef FEAT_CHANNEL
8082 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008083 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008084 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8085 {"ch_sendraw", 2, 3, f_ch_sendraw},
8086#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008087 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008088 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008090 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008092#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008093 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008094 {"complete_add", 1, 1, f_complete_add},
8095 {"complete_check", 0, 0, f_complete_check},
8096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008098 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#ifdef FEAT_FLOAT
8100 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008101 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008103 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008105 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008106 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008107 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008109 {"diff_filler", 1, 1, f_diff_filler},
8110 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008111 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008113 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"eventhandler", 0, 0, f_eventhandler},
8115 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008116 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008118#ifdef FEAT_FLOAT
8119 {"exp", 1, 1, f_exp},
8120#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008121 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008122 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008123 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8125 {"filereadable", 1, 1, f_filereadable},
8126 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008127 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008128 {"finddir", 1, 3, f_finddir},
8129 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008130#ifdef FEAT_FLOAT
8131 {"float2nr", 1, 1, f_float2nr},
8132 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008133 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008134#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008135 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"fnamemodify", 2, 2, f_fnamemodify},
8137 {"foldclosed", 1, 1, f_foldclosed},
8138 {"foldclosedend", 1, 1, f_foldclosedend},
8139 {"foldlevel", 1, 1, f_foldlevel},
8140 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008141 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008144 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008145 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008146 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008147 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"getchar", 0, 1, f_getchar},
8149 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008150 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"getcmdline", 0, 0, f_getcmdline},
8152 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008153 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008154 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008155 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008156 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008157 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008158 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"getfsize", 1, 1, f_getfsize},
8160 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008161 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008162 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008163 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008164 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008165 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008166 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008167 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008168 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008170 {"gettabvar", 2, 3, f_gettabvar},
8171 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"getwinposx", 0, 0, f_getwinposx},
8173 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008174 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008175 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008176 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008177 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008179 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008180 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008181 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8183 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8184 {"histadd", 2, 2, f_histadd},
8185 {"histdel", 1, 2, f_histdel},
8186 {"histget", 1, 2, f_histget},
8187 {"histnr", 1, 1, f_histnr},
8188 {"hlID", 1, 1, f_hlID},
8189 {"hlexists", 1, 1, f_hlexists},
8190 {"hostname", 0, 0, f_hostname},
8191 {"iconv", 3, 3, f_iconv},
8192 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008193 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008194 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008196 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {"inputrestore", 0, 0, f_inputrestore},
8198 {"inputsave", 0, 0, f_inputsave},
8199 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008200 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008201 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008203 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008204 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008205#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +01008206 {"job_start", 1, 2, f_job_start},
8207 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008208 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008209#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210 {"join", 1, 2, f_join},
Bram Moolenaar595e64e2016-02-07 19:19:53 +01008211 {"jsdecode", 1, 1, f_jsdecode},
8212 {"jsencode", 1, 1, f_jsencode},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008213 {"jsondecode", 1, 1, f_jsondecode},
8214 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008215 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008217 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"libcall", 3, 3, f_libcall},
8219 {"libcallnr", 3, 3, f_libcallnr},
8220 {"line", 1, 1, f_line},
8221 {"line2byte", 1, 1, f_line2byte},
8222 {"lispindent", 1, 1, f_lispindent},
8223 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008224#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008225 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008226 {"log10", 1, 1, f_log10},
8227#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008228#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008229 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008230#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008231 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008232 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008233 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008234 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008235 {"matchadd", 2, 5, f_matchadd},
8236 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008237 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008238 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008239 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008240 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008241 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008242 {"max", 1, 1, f_max},
8243 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008244#ifdef vim_mkdir
8245 {"mkdir", 1, 3, f_mkdir},
8246#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008248#ifdef FEAT_MZSCHEME
8249 {"mzeval", 1, 1, f_mzeval},
8250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008252 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008253 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008254 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008255#ifdef FEAT_PERL
8256 {"perleval", 1, 1, f_perleval},
8257#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008258#ifdef FEAT_FLOAT
8259 {"pow", 2, 2, f_pow},
8260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008262 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008263 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008264#ifdef FEAT_PYTHON3
8265 {"py3eval", 1, 1, f_py3eval},
8266#endif
8267#ifdef FEAT_PYTHON
8268 {"pyeval", 1, 1, f_pyeval},
8269#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008270 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008271 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008272 {"reltime", 0, 2, f_reltime},
8273 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {"remote_expr", 2, 3, f_remote_expr},
8275 {"remote_foreground", 1, 1, f_remote_foreground},
8276 {"remote_peek", 1, 2, f_remote_peek},
8277 {"remote_read", 1, 1, f_remote_read},
8278 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008279 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008281 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008283 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008284#ifdef FEAT_FLOAT
8285 {"round", 1, 1, f_round},
8286#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008287 {"screenattr", 2, 2, f_screenattr},
8288 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008289 {"screencol", 0, 0, f_screencol},
8290 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008291 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008292 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008293 {"searchpair", 3, 7, f_searchpair},
8294 {"searchpairpos", 3, 7, f_searchpairpos},
8295 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"server2client", 2, 2, f_server2client},
8297 {"serverlist", 0, 0, f_serverlist},
8298 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008299 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"setcmdpos", 1, 1, f_setcmdpos},
8301 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008302 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008303 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008304 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008305 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008307 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008308 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008310#ifdef FEAT_CRYPT
8311 {"sha256", 1, 1, f_sha256},
8312#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008313 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008314 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008316#ifdef FEAT_FLOAT
8317 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008318 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008319#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008320 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008321 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008322 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008323 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008324 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008325#ifdef FEAT_FLOAT
8326 {"sqrt", 1, 1, f_sqrt},
8327 {"str2float", 1, 1, f_str2float},
8328#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008329 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008330 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008331 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332#ifdef HAVE_STRFTIME
8333 {"strftime", 1, 2, f_strftime},
8334#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008336 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {"strlen", 1, 1, f_strlen},
8338 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008339 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008341 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008342 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"substitute", 4, 4, f_substitute},
8344 {"synID", 3, 3, f_synID},
8345 {"synIDattr", 2, 3, f_synIDattr},
8346 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008347 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008348 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008349 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008350 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008351 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008352 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008353 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008354 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008355 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008356#ifdef FEAT_FLOAT
8357 {"tan", 1, 1, f_tan},
8358 {"tanh", 1, 1, f_tanh},
8359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008361 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"tolower", 1, 1, f_tolower},
8363 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008364 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008365#ifdef FEAT_FLOAT
8366 {"trunc", 1, 1, f_trunc},
8367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008369 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008370 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008371 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008372 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"virtcol", 1, 1, f_virtcol},
8374 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008375 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {"winbufnr", 1, 1, f_winbufnr},
8377 {"wincol", 0, 0, f_wincol},
8378 {"winheight", 1, 1, f_winheight},
8379 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008380 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008382 {"winrestview", 1, 1, f_winrestview},
8383 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008385 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008386 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008387 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388};
8389
8390#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8391
8392/*
8393 * Function given to ExpandGeneric() to obtain the list of internal
8394 * or user defined function names.
8395 */
8396 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008397get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
8399 static int intidx = -1;
8400 char_u *name;
8401
8402 if (idx == 0)
8403 intidx = -1;
8404 if (intidx < 0)
8405 {
8406 name = get_user_func_name(xp, idx);
8407 if (name != NULL)
8408 return name;
8409 }
8410 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8411 {
8412 STRCPY(IObuff, functions[intidx].f_name);
8413 STRCAT(IObuff, "(");
8414 if (functions[intidx].f_max_argc == 0)
8415 STRCAT(IObuff, ")");
8416 return IObuff;
8417 }
8418
8419 return NULL;
8420}
8421
8422/*
8423 * Function given to ExpandGeneric() to obtain the list of internal or
8424 * user defined variable or function names.
8425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008427get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 static int intidx = -1;
8430 char_u *name;
8431
8432 if (idx == 0)
8433 intidx = -1;
8434 if (intidx < 0)
8435 {
8436 name = get_function_name(xp, idx);
8437 if (name != NULL)
8438 return name;
8439 }
8440 return get_user_var_name(xp, ++intidx);
8441}
8442
8443#endif /* FEAT_CMDL_COMPL */
8444
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008445#if defined(EBCDIC) || defined(PROTO)
8446/*
8447 * Compare struct fst by function name.
8448 */
8449 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008450compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008451{
8452 struct fst *p1 = (struct fst *)s1;
8453 struct fst *p2 = (struct fst *)s2;
8454
8455 return STRCMP(p1->f_name, p2->f_name);
8456}
8457
8458/*
8459 * Sort the function table by function name.
8460 * The sorting of the table above is ASCII dependant.
8461 * On machines using EBCDIC we have to sort it.
8462 */
8463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008464sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008465{
8466 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8467
8468 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8469}
8470#endif
8471
8472
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473/*
8474 * Find internal function in table above.
8475 * Return index, or -1 if not found
8476 */
8477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008478find_internal_func(
8479 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480{
8481 int first = 0;
8482 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8483 int cmp;
8484 int x;
8485
8486 /*
8487 * Find the function name in the table. Binary search.
8488 */
8489 while (first <= last)
8490 {
8491 x = first + ((unsigned)(last - first) >> 1);
8492 cmp = STRCMP(name, functions[x].f_name);
8493 if (cmp < 0)
8494 last = x - 1;
8495 else if (cmp > 0)
8496 first = x + 1;
8497 else
8498 return x;
8499 }
8500 return -1;
8501}
8502
8503/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008504 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8505 * name it contains, otherwise return "name".
8506 */
8507 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008508deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509{
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008511 int cc;
8512
8513 cc = name[*lenp];
8514 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008515 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008519 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 {
8521 *lenp = 0;
8522 return (char_u *)""; /* just in case */
8523 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008524 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008525 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 }
8527
8528 return name;
8529}
8530
8531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 * Allocate a variable for the result of a function.
8533 * Return OK or FAIL.
8534 */
8535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008536get_func_tv(
8537 char_u *name, /* name of the function */
8538 int len, /* length of "name" */
8539 typval_T *rettv,
8540 char_u **arg, /* argument, pointing to the '(' */
8541 linenr_T firstline, /* first line of range */
8542 linenr_T lastline, /* last line of range */
8543 int *doesrange, /* return: function handled range */
8544 int evaluate,
8545 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 char_u *argp;
8548 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008549 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 int argcount = 0; /* number of arguments found */
8551
8552 /*
8553 * Get the arguments.
8554 */
8555 argp = *arg;
8556 while (argcount < MAX_FUNC_ARGS)
8557 {
8558 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8559 if (*argp == ')' || *argp == ',' || *argp == NUL)
8560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8562 {
8563 ret = FAIL;
8564 break;
8565 }
8566 ++argcount;
8567 if (*argp != ',')
8568 break;
8569 }
8570 if (*argp == ')')
8571 ++argp;
8572 else
8573 ret = FAIL;
8574
8575 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008576 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008577 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008579 {
8580 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008581 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008583 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585
8586 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588
8589 *arg = skipwhite(argp);
8590 return ret;
8591}
8592
8593
8594/*
8595 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008596 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008597 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008599 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600call_func(
8601 char_u *funcname, /* name of the function */
8602 int len, /* length of "name" */
8603 typval_T *rettv, /* return value goes here */
8604 int argcount, /* number of "argvars" */
8605 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008606 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008607 linenr_T firstline, /* first line of range */
8608 linenr_T lastline, /* last line of range */
8609 int *doesrange, /* return: function handled range */
8610 int evaluate,
8611 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612{
8613 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614#define ERROR_UNKNOWN 0
8615#define ERROR_TOOMANY 1
8616#define ERROR_TOOFEW 2
8617#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008618#define ERROR_DICT 4
8619#define ERROR_NONE 5
8620#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 int error = ERROR_NONE;
8622 int i;
8623 int llen;
8624 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625#define FLEN_FIXED 40
8626 char_u fname_buf[FLEN_FIXED + 1];
8627 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008628 char_u *name;
8629
8630 /* Make a copy of the name, if it comes from a funcref variable it could
8631 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008632 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008633 if (name == NULL)
8634 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635
8636 /*
8637 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8638 * Change <SNR>123_name() to K_SNR 123_name().
8639 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 llen = eval_fname_script(name);
8642 if (llen > 0)
8643 {
8644 fname_buf[0] = K_SPECIAL;
8645 fname_buf[1] = KS_EXTRA;
8646 fname_buf[2] = (int)KE_SNR;
8647 i = 3;
8648 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8649 {
8650 if (current_SID <= 0)
8651 error = ERROR_SCRIPT;
8652 else
8653 {
8654 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8655 i = (int)STRLEN(fname_buf);
8656 }
8657 }
8658 if (i + STRLEN(name + llen) < FLEN_FIXED)
8659 {
8660 STRCPY(fname_buf + i, name + llen);
8661 fname = fname_buf;
8662 }
8663 else
8664 {
8665 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8666 if (fname == NULL)
8667 error = ERROR_OTHER;
8668 else
8669 {
8670 mch_memmove(fname, fname_buf, (size_t)i);
8671 STRCPY(fname + i, name + llen);
8672 }
8673 }
8674 }
8675 else
8676 fname = name;
8677
8678 *doesrange = FALSE;
8679
8680
8681 /* execute the function if no errors detected and executing */
8682 if (evaluate && error == ERROR_NONE)
8683 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008684 char_u *rfname = fname;
8685
8686 /* Ignore "g:" before a function name. */
8687 if (fname[0] == 'g' && fname[1] == ':')
8688 rfname = fname + 2;
8689
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008690 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8691 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 error = ERROR_UNKNOWN;
8693
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008694 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 {
8696 /*
8697 * User defined function.
8698 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008699 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008700
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008702 /* Trigger FuncUndefined event, may load the function. */
8703 if (fp == NULL
8704 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008705 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008706 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008708 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008709 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 }
8711#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008712 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008713 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008714 {
8715 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008716 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008717 }
8718
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 if (fp != NULL)
8720 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008721 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008723 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008725 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008727 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008728 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 else
8730 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008731 int did_save_redo = FALSE;
8732
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 /*
8734 * Call the user function.
8735 * Save and restore search patterns, script variables and
8736 * redo buffer.
8737 */
8738 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008739#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008740 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008741#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008742 {
8743 saveRedobuff();
8744 did_save_redo = TRUE;
8745 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008746 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008748 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008749 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8750 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8751 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008752 /* Function was unreferenced while being used, free it
8753 * now. */
8754 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008755 if (did_save_redo)
8756 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 restore_search_patterns();
8758 error = ERROR_NONE;
8759 }
8760 }
8761 }
8762 else
8763 {
8764 /*
8765 * Find the function name in the table, call its implementation.
8766 */
8767 i = find_internal_func(fname);
8768 if (i >= 0)
8769 {
8770 if (argcount < functions[i].f_min_argc)
8771 error = ERROR_TOOFEW;
8772 else if (argcount > functions[i].f_max_argc)
8773 error = ERROR_TOOMANY;
8774 else
8775 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008776 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 error = ERROR_NONE;
8779 }
8780 }
8781 }
8782 /*
8783 * The function call (or "FuncUndefined" autocommand sequence) might
8784 * have been aborted by an error, an interrupt, or an explicitly thrown
8785 * exception that has not been caught so far. This situation can be
8786 * tested for by calling aborting(). For an error in an internal
8787 * function or for the "E132" error in call_user_func(), however, the
8788 * throw point at which the "force_abort" flag (temporarily reset by
8789 * emsg()) is normally updated has not been reached yet. We need to
8790 * update that flag first to make aborting() reliable.
8791 */
8792 update_force_abort();
8793 }
8794 if (error == ERROR_NONE)
8795 ret = OK;
8796
8797 /*
8798 * Report an error unless the argument evaluation or function call has been
8799 * cancelled due to an aborting error, an interrupt, or an exception.
8800 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008801 if (!aborting())
8802 {
8803 switch (error)
8804 {
8805 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008806 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008807 break;
8808 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008809 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008810 break;
8811 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008812 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 name);
8814 break;
8815 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008816 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008817 name);
8818 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008819 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008820 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008821 name);
8822 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008823 }
8824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 if (fname != name && fname != fname_buf)
8827 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008828 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
8830 return ret;
8831}
8832
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008833/*
8834 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008835 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008836 */
8837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008838emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008839{
8840 char_u *p;
8841
8842 if (*name == K_SPECIAL)
8843 p = concat_str((char_u *)"<SNR>", name + 3);
8844 else
8845 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008846 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008847 if (p != name)
8848 vim_free(p);
8849}
8850
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008851/*
8852 * Return TRUE for a non-zero Number and a non-empty String.
8853 */
8854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008855non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008856{
8857 return ((argvars[0].v_type == VAR_NUMBER
8858 && argvars[0].vval.v_number != 0)
8859 || (argvars[0].v_type == VAR_STRING
8860 && argvars[0].vval.v_string != NULL
8861 && *argvars[0].vval.v_string != NUL));
8862}
8863
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864/*********************************************
8865 * Implementation of the built-in functions
8866 */
8867
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008868#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008869static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008870
8871/*
8872 * Get the float value of "argvars[0]" into "f".
8873 * Returns FAIL when the argument is not a Number or Float.
8874 */
8875 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008876get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008877{
8878 if (argvars[0].v_type == VAR_FLOAT)
8879 {
8880 *f = argvars[0].vval.v_float;
8881 return OK;
8882 }
8883 if (argvars[0].v_type == VAR_NUMBER)
8884 {
8885 *f = (float_T)argvars[0].vval.v_number;
8886 return OK;
8887 }
8888 EMSG(_("E808: Number or Float required"));
8889 return FAIL;
8890}
8891
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008892/*
8893 * "abs(expr)" function
8894 */
8895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008896f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008897{
8898 if (argvars[0].v_type == VAR_FLOAT)
8899 {
8900 rettv->v_type = VAR_FLOAT;
8901 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8902 }
8903 else
8904 {
8905 varnumber_T n;
8906 int error = FALSE;
8907
8908 n = get_tv_number_chk(&argvars[0], &error);
8909 if (error)
8910 rettv->vval.v_number = -1;
8911 else if (n > 0)
8912 rettv->vval.v_number = n;
8913 else
8914 rettv->vval.v_number = -n;
8915 }
8916}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008917
8918/*
8919 * "acos()" function
8920 */
8921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008922f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008923{
8924 float_T f;
8925
8926 rettv->v_type = VAR_FLOAT;
8927 if (get_float_arg(argvars, &f) == OK)
8928 rettv->vval.v_float = acos(f);
8929 else
8930 rettv->vval.v_float = 0.0;
8931}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008932#endif
8933
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008935 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 */
8937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008938f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
Bram Moolenaar33570922005-01-25 22:26:29 +00008940 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008943 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008945 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008946 && !tv_check_lock(l->lv_lock,
8947 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008948 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008950 }
8951 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008952 EMSG(_(e_listreq));
8953}
8954
8955/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008956 * "alloc_fail(id, countdown, repeat)" function
8957 */
8958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008959f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008960{
8961 if (argvars[0].v_type != VAR_NUMBER
8962 || argvars[0].vval.v_number <= 0
8963 || argvars[1].v_type != VAR_NUMBER
8964 || argvars[1].vval.v_number < 0
8965 || argvars[2].v_type != VAR_NUMBER)
8966 EMSG(_(e_invarg));
8967 else
8968 {
8969 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008970 if (alloc_fail_id >= aid_last)
8971 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008972 alloc_fail_countdown = argvars[1].vval.v_number;
8973 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008974 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008975 }
8976}
8977
8978/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008979 * "and(expr, expr)" function
8980 */
8981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008982f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008983{
8984 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8985 & get_tv_number_chk(&argvars[1], NULL);
8986}
8987
8988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989 * "append(lnum, string/list)" function
8990 */
8991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008992f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008993{
8994 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008995 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 list_T *l = NULL;
8997 listitem_T *li = NULL;
8998 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008999 long added = 0;
9000
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009001 /* When coming here from Insert mode, sync undo, so that this can be
9002 * undone separately from what was previously inserted. */
9003 if (u_sync_once == 2)
9004 {
9005 u_sync_once = 1; /* notify that u_sync() was called */
9006 u_sync(TRUE);
9007 }
9008
Bram Moolenaar0d660222005-01-07 21:51:51 +00009009 lnum = get_tv_lnum(argvars);
9010 if (lnum >= 0
9011 && lnum <= curbuf->b_ml.ml_line_count
9012 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009013 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009014 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009015 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009016 l = argvars[1].vval.v_list;
9017 if (l == NULL)
9018 return;
9019 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009021 for (;;)
9022 {
9023 if (l == NULL)
9024 tv = &argvars[1]; /* append a string */
9025 else if (li == NULL)
9026 break; /* end of list */
9027 else
9028 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009029 line = get_tv_string_chk(tv);
9030 if (line == NULL) /* type error */
9031 {
9032 rettv->vval.v_number = 1; /* Failed */
9033 break;
9034 }
9035 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009036 ++added;
9037 if (l == NULL)
9038 break;
9039 li = li->li_next;
9040 }
9041
9042 appended_lines_mark(lnum, added);
9043 if (curwin->w_cursor.lnum > lnum)
9044 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009046 else
9047 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048}
9049
9050/*
9051 * "argc()" function
9052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009054f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057}
9058
9059/*
9060 * "argidx()" function
9061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009063f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066}
9067
9068/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009069 * "arglistid()" function
9070 */
9071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009072f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009073{
9074 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009075
9076 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009077 wp = find_tabwin(&argvars[0], &argvars[1]);
9078 if (wp != NULL)
9079 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009080}
9081
9082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 * "argv(nr)" function
9084 */
9085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009086f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087{
9088 int idx;
9089
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009090 if (argvars[0].v_type != VAR_UNKNOWN)
9091 {
9092 idx = get_tv_number_chk(&argvars[0], NULL);
9093 if (idx >= 0 && idx < ARGCOUNT)
9094 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9095 else
9096 rettv->vval.v_string = NULL;
9097 rettv->v_type = VAR_STRING;
9098 }
9099 else if (rettv_list_alloc(rettv) == OK)
9100 for (idx = 0; idx < ARGCOUNT; ++idx)
9101 list_append_string(rettv->vval.v_list,
9102 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103}
9104
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009105static void prepare_assert_error(garray_T*gap);
9106static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9107static void assert_error(garray_T *gap);
9108static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009109
9110/*
9111 * Prepare "gap" for an assert error and add the sourcing position.
9112 */
9113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009114prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009115{
9116 char buf[NUMBUFLEN];
9117
9118 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009119 if (sourcing_name != NULL)
9120 {
9121 ga_concat(gap, sourcing_name);
9122 if (sourcing_lnum > 0)
9123 ga_concat(gap, (char_u *)" ");
9124 }
9125 if (sourcing_lnum > 0)
9126 {
9127 sprintf(buf, "line %ld", (long)sourcing_lnum);
9128 ga_concat(gap, (char_u *)buf);
9129 }
9130 if (sourcing_name != NULL || sourcing_lnum > 0)
9131 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009132}
9133
9134/*
9135 * Fill "gap" with information about an assert error.
9136 */
9137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009138fill_assert_error(
9139 garray_T *gap,
9140 typval_T *opt_msg_tv,
9141 char_u *exp_str,
9142 typval_T *exp_tv,
9143 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009144{
9145 char_u numbuf[NUMBUFLEN];
9146 char_u *tofree;
9147
9148 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9149 {
9150 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9151 vim_free(tofree);
9152 }
9153 else
9154 {
9155 ga_concat(gap, (char_u *)"Expected ");
9156 if (exp_str == NULL)
9157 {
9158 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9159 vim_free(tofree);
9160 }
9161 else
9162 ga_concat(gap, exp_str);
9163 ga_concat(gap, (char_u *)" but got ");
9164 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9165 vim_free(tofree);
9166 }
9167}
Bram Moolenaar43345542015-11-29 17:35:35 +01009168
9169/*
9170 * Add an assert error to v:errors.
9171 */
9172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009173assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009174{
9175 struct vimvar *vp = &vimvars[VV_ERRORS];
9176
9177 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9178 /* Make sure v:errors is a list. */
9179 set_vim_var_list(VV_ERRORS, list_alloc());
9180 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9181}
9182
Bram Moolenaar43345542015-11-29 17:35:35 +01009183/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009184 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009185 */
9186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009187f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009188{
9189 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009190
9191 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9192 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009193 prepare_assert_error(&ga);
9194 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9195 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009196 ga_clear(&ga);
9197 }
9198}
9199
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009200/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009201 * "assert_exception(string[, msg])" function
9202 */
9203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009204f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009205{
9206 garray_T ga;
9207 char *error;
9208
9209 error = (char *)get_tv_string_chk(&argvars[0]);
9210 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9211 {
9212 prepare_assert_error(&ga);
9213 ga_concat(&ga, (char_u *)"v:exception is not set");
9214 assert_error(&ga);
9215 ga_clear(&ga);
9216 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009217 else if (error != NULL
9218 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009219 {
9220 prepare_assert_error(&ga);
9221 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9222 &vimvars[VV_EXCEPTION].vv_tv);
9223 assert_error(&ga);
9224 ga_clear(&ga);
9225 }
9226}
9227
9228/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009229 * "assert_fails(cmd [, error])" function
9230 */
9231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009232f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009233{
9234 char_u *cmd = get_tv_string_chk(&argvars[0]);
9235 garray_T ga;
9236
9237 called_emsg = FALSE;
9238 suppress_errthrow = TRUE;
9239 emsg_silent = TRUE;
9240 do_cmdline_cmd(cmd);
9241 if (!called_emsg)
9242 {
9243 prepare_assert_error(&ga);
9244 ga_concat(&ga, (char_u *)"command did not fail: ");
9245 ga_concat(&ga, cmd);
9246 assert_error(&ga);
9247 ga_clear(&ga);
9248 }
9249 else if (argvars[1].v_type != VAR_UNKNOWN)
9250 {
9251 char_u buf[NUMBUFLEN];
9252 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9253
9254 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9255 {
9256 prepare_assert_error(&ga);
9257 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9258 &vimvars[VV_ERRMSG].vv_tv);
9259 assert_error(&ga);
9260 ga_clear(&ga);
9261 }
9262 }
9263
9264 called_emsg = FALSE;
9265 suppress_errthrow = FALSE;
9266 emsg_silent = FALSE;
9267 emsg_on_display = FALSE;
9268 set_vim_var_string(VV_ERRMSG, NULL, 0);
9269}
9270
9271/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009272 * Common for assert_true() and assert_false().
9273 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009275assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009276{
9277 int error = FALSE;
9278 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009279
Bram Moolenaar37127922016-02-06 20:29:28 +01009280 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009281 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009282 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009283 if (argvars[0].v_type != VAR_NUMBER
9284 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9285 || error)
9286 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009287 prepare_assert_error(&ga);
9288 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009289 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009290 NULL, &argvars[0]);
9291 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009292 ga_clear(&ga);
9293 }
9294}
9295
9296/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009297 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009298 */
9299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009300f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009301{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009302 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009303}
9304
9305/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009306 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009307 */
9308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009309f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009310{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009311 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009312}
9313
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009314#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009315/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009316 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009317 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009319f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009320{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009321 float_T f;
9322
9323 rettv->v_type = VAR_FLOAT;
9324 if (get_float_arg(argvars, &f) == OK)
9325 rettv->vval.v_float = asin(f);
9326 else
9327 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009328}
9329
9330/*
9331 * "atan()" function
9332 */
9333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009334f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009335{
9336 float_T f;
9337
9338 rettv->v_type = VAR_FLOAT;
9339 if (get_float_arg(argvars, &f) == OK)
9340 rettv->vval.v_float = atan(f);
9341 else
9342 rettv->vval.v_float = 0.0;
9343}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009344
9345/*
9346 * "atan2()" function
9347 */
9348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009349f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009350{
9351 float_T fx, fy;
9352
9353 rettv->v_type = VAR_FLOAT;
9354 if (get_float_arg(argvars, &fx) == OK
9355 && get_float_arg(&argvars[1], &fy) == OK)
9356 rettv->vval.v_float = atan2(fx, fy);
9357 else
9358 rettv->vval.v_float = 0.0;
9359}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009360#endif
9361
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362/*
9363 * "browse(save, title, initdir, default)" function
9364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009366f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
9368#ifdef FEAT_BROWSE
9369 int save;
9370 char_u *title;
9371 char_u *initdir;
9372 char_u *defname;
9373 char_u buf[NUMBUFLEN];
9374 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009375 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009377 save = get_tv_number_chk(&argvars[0], &error);
9378 title = get_tv_string_chk(&argvars[1]);
9379 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9380 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009382 if (error || title == NULL || initdir == NULL || defname == NULL)
9383 rettv->vval.v_string = NULL;
9384 else
9385 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009386 do_browse(save ? BROWSE_SAVE : 0,
9387 title, defname, NULL, initdir, NULL, curbuf);
9388#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009389 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009390#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009392}
9393
9394/*
9395 * "browsedir(title, initdir)" function
9396 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009398f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009399{
9400#ifdef FEAT_BROWSE
9401 char_u *title;
9402 char_u *initdir;
9403 char_u buf[NUMBUFLEN];
9404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009405 title = get_tv_string_chk(&argvars[0]);
9406 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 if (title == NULL || initdir == NULL)
9409 rettv->vval.v_string = NULL;
9410 else
9411 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009412 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009414 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417}
9418
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009419static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009420
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421/*
9422 * Find a buffer by number or exact name.
9423 */
9424 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009425find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426{
9427 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009429 if (avar->v_type == VAR_NUMBER)
9430 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009431 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009433 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009434 if (buf == NULL)
9435 {
9436 /* No full path name match, try a match with a URL or a "nofile"
9437 * buffer, these don't use the full path. */
9438 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9439 if (buf->b_fname != NULL
9440 && (path_with_url(buf->b_fname)
9441#ifdef FEAT_QUICKFIX
9442 || bt_nofile(buf)
9443#endif
9444 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009445 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009446 break;
9447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 }
9449 return buf;
9450}
9451
9452/*
9453 * "bufexists(expr)" function
9454 */
9455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009456f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459}
9460
9461/*
9462 * "buflisted(expr)" function
9463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466{
9467 buf_T *buf;
9468
9469 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471}
9472
9473/*
9474 * "bufloaded(expr)" function
9475 */
9476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009477f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478{
9479 buf_T *buf;
9480
9481 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009485static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009486
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487/*
9488 * Get buffer by number or pattern.
9489 */
9490 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 int save_magic;
9495 char_u *save_cpo;
9496 buf_T *buf;
9497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 if (tv->v_type == VAR_NUMBER)
9499 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009500 if (tv->v_type != VAR_STRING)
9501 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 if (name == NULL || *name == NUL)
9503 return curbuf;
9504 if (name[0] == '$' && name[1] == NUL)
9505 return lastbuf;
9506
9507 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9508 save_magic = p_magic;
9509 p_magic = TRUE;
9510 save_cpo = p_cpo;
9511 p_cpo = (char_u *)"";
9512
9513 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009514 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515
9516 p_magic = save_magic;
9517 p_cpo = save_cpo;
9518
9519 /* If not found, try expanding the name, like done for bufexists(). */
9520 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522
9523 return buf;
9524}
9525
9526/*
9527 * "bufname(expr)" function
9528 */
9529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009530f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
9532 buf_T *buf;
9533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009536 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 --emsg_off;
9543}
9544
9545/*
9546 * "bufnr(expr)" function
9547 */
9548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009549f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009552 int error = FALSE;
9553 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009557 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009558 --emsg_off;
9559
9560 /* If the buffer isn't found and the second argument is not zero create a
9561 * new buffer. */
9562 if (buf == NULL
9563 && argvars[1].v_type != VAR_UNKNOWN
9564 && get_tv_number_chk(&argvars[1], &error) != 0
9565 && !error
9566 && (name = get_tv_string_chk(&argvars[0])) != NULL
9567 && !error)
9568 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9569
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574}
9575
9576/*
9577 * "bufwinnr(nr)" function
9578 */
9579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009580f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582#ifdef FEAT_WINDOWS
9583 win_T *wp;
9584 int winnr = 0;
9585#endif
9586 buf_T *buf;
9587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009590 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591#ifdef FEAT_WINDOWS
9592 for (wp = firstwin; wp; wp = wp->w_next)
9593 {
9594 ++winnr;
9595 if (wp->w_buffer == buf)
9596 break;
9597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601#endif
9602 --emsg_off;
9603}
9604
9605/*
9606 * "byte2line(byte)" function
9607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009609f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613#else
9614 long boff = 0;
9615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 (linenr_T)0, &boff);
9622#endif
9623}
9624
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009626byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009627{
9628#ifdef FEAT_MBYTE
9629 char_u *t;
9630#endif
9631 char_u *str;
9632 long idx;
9633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 str = get_tv_string_chk(&argvars[0]);
9635 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009637 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009638 return;
9639
9640#ifdef FEAT_MBYTE
9641 t = str;
9642 for ( ; idx > 0; idx--)
9643 {
9644 if (*t == NUL) /* EOL reached */
9645 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009646 if (enc_utf8 && comp)
9647 t += utf_ptr2len(t);
9648 else
9649 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009650 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009651 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009652#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009653 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009655#endif
9656}
9657
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009658/*
9659 * "byteidx()" function
9660 */
9661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009662f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009663{
9664 byteidx(argvars, rettv, FALSE);
9665}
9666
9667/*
9668 * "byteidxcomp()" function
9669 */
9670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009671f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009672{
9673 byteidx(argvars, rettv, TRUE);
9674}
9675
Bram Moolenaardb913952012-06-29 12:54:53 +02009676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009677func_call(
9678 char_u *name,
9679 typval_T *args,
9680 dict_T *selfdict,
9681 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009682{
9683 listitem_T *item;
9684 typval_T argv[MAX_FUNC_ARGS + 1];
9685 int argc = 0;
9686 int dummy;
9687 int r = 0;
9688
9689 for (item = args->vval.v_list->lv_first; item != NULL;
9690 item = item->li_next)
9691 {
9692 if (argc == MAX_FUNC_ARGS)
9693 {
9694 EMSG(_("E699: Too many arguments"));
9695 break;
9696 }
9697 /* Make a copy of each argument. This is needed to be able to set
9698 * v_lock to VAR_FIXED in the copy without changing the original list.
9699 */
9700 copy_tv(&item->li_tv, &argv[argc++]);
9701 }
9702
9703 if (item == NULL)
9704 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9705 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9706 &dummy, TRUE, selfdict);
9707
9708 /* Free the arguments. */
9709 while (argc > 0)
9710 clear_tv(&argv[--argc]);
9711
9712 return r;
9713}
9714
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009715/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009716 * "call(func, arglist)" function
9717 */
9718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009719f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009720{
9721 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009722 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009723
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009724 if (argvars[1].v_type != VAR_LIST)
9725 {
9726 EMSG(_(e_listreq));
9727 return;
9728 }
9729 if (argvars[1].vval.v_list == NULL)
9730 return;
9731
9732 if (argvars[0].v_type == VAR_FUNC)
9733 func = argvars[0].vval.v_string;
9734 else
9735 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009736 if (*func == NUL)
9737 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009738
Bram Moolenaare9a41262005-01-15 22:18:47 +00009739 if (argvars[2].v_type != VAR_UNKNOWN)
9740 {
9741 if (argvars[2].v_type != VAR_DICT)
9742 {
9743 EMSG(_(e_dictreq));
9744 return;
9745 }
9746 selfdict = argvars[2].vval.v_dict;
9747 }
9748
Bram Moolenaardb913952012-06-29 12:54:53 +02009749 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009750}
9751
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009752#ifdef FEAT_FLOAT
9753/*
9754 * "ceil({float})" function
9755 */
9756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009757f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009758{
9759 float_T f;
9760
9761 rettv->v_type = VAR_FLOAT;
9762 if (get_float_arg(argvars, &f) == OK)
9763 rettv->vval.v_float = ceil(f);
9764 else
9765 rettv->vval.v_float = 0.0;
9766}
9767#endif
9768
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009769#ifdef FEAT_CHANNEL
9770/*
9771 * Get the channel index from the handle argument.
9772 * Returns -1 if the handle is invalid or the channel is closed.
9773 */
9774 static int
9775get_channel_arg(typval_T *tv)
9776{
9777 int ch_idx;
9778
9779 if (tv->v_type != VAR_NUMBER)
9780 {
9781 EMSG2(_(e_invarg2), get_tv_string(tv));
9782 return -1;
9783 }
9784 ch_idx = tv->vval.v_number;
9785
9786 if (!channel_is_open(ch_idx))
9787 {
9788 EMSGN(_("E906: not an open channel"), ch_idx);
9789 return -1;
9790 }
9791 return ch_idx;
9792}
9793
9794/*
9795 * "ch_close()" function
9796 */
9797 static void
9798f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9799{
9800 int ch_idx = get_channel_arg(&argvars[0]);
9801
9802 if (ch_idx >= 0)
9803 channel_close(ch_idx);
9804}
9805
9806/*
9807 * Get a callback from "arg". It can be a Funcref or a function name.
9808 * When "arg" is zero return an empty string.
9809 * Return NULL for an invalid argument.
9810 */
9811 static char_u *
9812get_callback(typval_T *arg)
9813{
9814 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9815 return arg->vval.v_string;
9816 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9817 return (char_u *)"";
9818 EMSG(_("E999: Invalid callback argument"));
9819 return NULL;
9820}
9821
9822/*
9823 * "ch_open()" function
9824 */
9825 static void
9826f_ch_open(typval_T *argvars, typval_T *rettv)
9827{
9828 char_u *address;
9829 char_u *mode;
9830 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009831 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009832 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009833 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009834 int waittime = 0;
9835 int timeout = 2000;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009836 ch_mode_T ch_mode = MODE_JSON;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009837 int ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009838
9839 /* default: fail */
9840 rettv->vval.v_number = -1;
9841
9842 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009843 if (argvars[1].v_type != VAR_UNKNOWN
9844 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009845 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009846 EMSG(_(e_invarg));
9847 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009848 }
9849
9850 /* parse address */
9851 p = vim_strchr(address, ':');
9852 if (p == NULL)
9853 {
9854 EMSG2(_(e_invarg2), address);
9855 return;
9856 }
9857 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009858 port = strtol((char *)p, &rest, 10);
9859 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009860 {
9861 p[-1] = ':';
9862 EMSG2(_(e_invarg2), address);
9863 return;
9864 }
9865
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009866 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009867 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009868 /* parse argdict */
9869 dict_T *dict = argvars[1].vval.v_dict;
9870
9871 if (dict_find(dict, (char_u *)"mode", -1) != NULL)
9872 {
9873 mode = get_dict_string(dict, (char_u *)"mode", FALSE);
9874 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009875 ch_mode = MODE_RAW;
9876 else if (STRCMP(mode, "js") == 0)
9877 ch_mode = MODE_JS;
9878 else if (STRCMP(mode, "json") == 0)
9879 ch_mode = MODE_JSON;
9880 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009881 {
9882 EMSG2(_(e_invarg2), mode);
9883 return;
9884 }
9885 }
9886 if (dict_find(dict, (char_u *)"waittime", -1) != NULL)
9887 waittime = get_dict_number(dict, (char_u *)"waittime");
9888 if (dict_find(dict, (char_u *)"timeout", -1) != NULL)
9889 timeout = get_dict_number(dict, (char_u *)"timeout");
9890 if (dict_find(dict, (char_u *)"callback", -1) != NULL)
9891 callback = get_dict_string(dict, (char_u *)"callback", FALSE);
9892 }
9893 if (waittime < 0 || timeout < 0)
9894 {
9895 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009896 return;
9897 }
9898
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009899 ch_idx = channel_open((char *)address, port, waittime, NULL);
9900 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009901 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009902 channel_set_json_mode(ch_idx, ch_mode);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009903 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009904 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009905 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009906 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009907 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009908}
9909
9910/*
9911 * common for "sendexpr()" and "sendraw()"
9912 * Returns the channel index if the caller should read the response.
9913 * Otherwise returns -1.
9914 */
9915 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009916send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009917{
9918 int ch_idx;
9919 char_u *callback = NULL;
9920
9921 ch_idx = get_channel_arg(&argvars[0]);
9922 if (ch_idx < 0)
9923 return -1;
9924
9925 if (argvars[2].v_type != VAR_UNKNOWN)
9926 {
9927 callback = get_callback(&argvars[2]);
9928 if (callback == NULL)
9929 return -1;
9930 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009931 /* Set the callback. An empty callback means no callback and not reading
9932 * the response. */
9933 if (callback != NULL && *callback != NUL)
9934 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009935
9936 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9937 return ch_idx;
9938 return -1;
9939}
9940
9941/*
9942 * "ch_sendexpr()" function
9943 */
9944 static void
9945f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9946{
9947 char_u *text;
9948 typval_T *listtv;
9949 int ch_idx;
9950 int id;
9951
9952 /* return an empty string by default */
9953 rettv->v_type = VAR_STRING;
9954 rettv->vval.v_string = NULL;
9955
9956 id = channel_get_id();
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009957 text = json_encode_nr_expr(id, &argvars[1], 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009958 if (text == NULL)
9959 return;
9960
Bram Moolenaara07fec92016-02-05 21:04:08 +01009961 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009962 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009963 if (ch_idx >= 0)
9964 {
9965 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9966 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009967 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009968
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009969 /* Move the item from the list and then change the type to
9970 * avoid the value being freed. */
9971 *rettv = list->lv_last->li_tv;
9972 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009973 clear_tv(listtv);
9974 }
9975 }
9976}
9977
9978/*
9979 * "ch_sendraw()" function
9980 */
9981 static void
9982f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9983{
9984 char_u buf[NUMBUFLEN];
9985 char_u *text;
9986 int ch_idx;
9987
9988 /* return an empty string by default */
9989 rettv->v_type = VAR_STRING;
9990 rettv->vval.v_string = NULL;
9991
9992 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +01009993 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009994 if (ch_idx >= 0)
9995 rettv->vval.v_string = channel_read_block(ch_idx);
9996}
9997#endif
9998
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009999/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010000 * "changenr()" function
10001 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010003f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010004{
10005 rettv->vval.v_number = curbuf->b_u_seq_cur;
10006}
10007
10008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 * "char2nr(string)" function
10010 */
10011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010012f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013{
10014#ifdef FEAT_MBYTE
10015 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010016 {
10017 int utf8 = 0;
10018
10019 if (argvars[1].v_type != VAR_UNKNOWN)
10020 utf8 = get_tv_number_chk(&argvars[1], NULL);
10021
10022 if (utf8)
10023 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10024 else
10025 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 else
10028#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030}
10031
10032/*
10033 * "cindent(lnum)" function
10034 */
10035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010036f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037{
10038#ifdef FEAT_CINDENT
10039 pos_T pos;
10040 linenr_T lnum;
10041
10042 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10045 {
10046 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 curwin->w_cursor = pos;
10049 }
10050 else
10051#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053}
10054
10055/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010056 * "clearmatches()" function
10057 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010059f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010060{
10061#ifdef FEAT_SEARCH_EXTRA
10062 clear_matches(curwin);
10063#endif
10064}
10065
10066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 * "col(string)" function
10068 */
10069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010070f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071{
10072 colnr_T col = 0;
10073 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010074 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010076 fp = var2fpos(&argvars[0], FALSE, &fnum);
10077 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 {
10079 if (fp->col == MAXCOL)
10080 {
10081 /* '> can be MAXCOL, get the length of the line then */
10082 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010083 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 else
10085 col = MAXCOL;
10086 }
10087 else
10088 {
10089 col = fp->col + 1;
10090#ifdef FEAT_VIRTUALEDIT
10091 /* col(".") when the cursor is on the NUL at the end of the line
10092 * because of "coladd" can be seen as an extra column. */
10093 if (virtual_active() && fp == &curwin->w_cursor)
10094 {
10095 char_u *p = ml_get_cursor();
10096
10097 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10098 curwin->w_virtcol - curwin->w_cursor.coladd))
10099 {
10100# ifdef FEAT_MBYTE
10101 int l;
10102
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010103 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 col += l;
10105# else
10106 if (*p != NUL && p[1] == NUL)
10107 ++col;
10108# endif
10109 }
10110 }
10111#endif
10112 }
10113 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115}
10116
Bram Moolenaar572cb562005-08-05 21:35:02 +000010117#if defined(FEAT_INS_EXPAND)
10118/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010119 * "complete()" function
10120 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010122f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010123{
10124 int startcol;
10125
10126 if ((State & INSERT) == 0)
10127 {
10128 EMSG(_("E785: complete() can only be used in Insert mode"));
10129 return;
10130 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010131
10132 /* Check for undo allowed here, because if something was already inserted
10133 * the line was already saved for undo and this check isn't done. */
10134 if (!undo_allowed())
10135 return;
10136
Bram Moolenaarade00832006-03-10 21:46:58 +000010137 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10138 {
10139 EMSG(_(e_invarg));
10140 return;
10141 }
10142
10143 startcol = get_tv_number_chk(&argvars[0], NULL);
10144 if (startcol <= 0)
10145 return;
10146
10147 set_completion(startcol - 1, argvars[1].vval.v_list);
10148}
10149
10150/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010151 * "complete_add()" function
10152 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010154f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010155{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010156 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010157}
10158
10159/*
10160 * "complete_check()" function
10161 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010163f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010164{
10165 int saved = RedrawingDisabled;
10166
10167 RedrawingDisabled = 0;
10168 ins_compl_check_keys(0);
10169 rettv->vval.v_number = compl_interrupted;
10170 RedrawingDisabled = saved;
10171}
10172#endif
10173
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174/*
10175 * "confirm(message, buttons[, default [, type]])" function
10176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010178f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10181 char_u *message;
10182 char_u *buttons = NULL;
10183 char_u buf[NUMBUFLEN];
10184 char_u buf2[NUMBUFLEN];
10185 int def = 1;
10186 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 char_u *typestr;
10188 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 message = get_tv_string_chk(&argvars[0]);
10191 if (message == NULL)
10192 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010193 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10196 if (buttons == NULL)
10197 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010198 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010200 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010201 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10204 if (typestr == NULL)
10205 error = TRUE;
10206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 switch (TOUPPER_ASC(*typestr))
10209 {
10210 case 'E': type = VIM_ERROR; break;
10211 case 'Q': type = VIM_QUESTION; break;
10212 case 'I': type = VIM_INFO; break;
10213 case 'W': type = VIM_WARNING; break;
10214 case 'G': type = VIM_GENERIC; break;
10215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 }
10217 }
10218 }
10219 }
10220
10221 if (buttons == NULL || *buttons == NUL)
10222 buttons = (char_u *)_("&Ok");
10223
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010224 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010225 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010226 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227#endif
10228}
10229
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010230/*
10231 * "copy()" function
10232 */
10233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010234f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010235{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010236 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010237}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010239#ifdef FEAT_FLOAT
10240/*
10241 * "cos()" function
10242 */
10243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010244f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010245{
10246 float_T f;
10247
10248 rettv->v_type = VAR_FLOAT;
10249 if (get_float_arg(argvars, &f) == OK)
10250 rettv->vval.v_float = cos(f);
10251 else
10252 rettv->vval.v_float = 0.0;
10253}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010254
10255/*
10256 * "cosh()" function
10257 */
10258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010259f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010260{
10261 float_T f;
10262
10263 rettv->v_type = VAR_FLOAT;
10264 if (get_float_arg(argvars, &f) == OK)
10265 rettv->vval.v_float = cosh(f);
10266 else
10267 rettv->vval.v_float = 0.0;
10268}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010269#endif
10270
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010272 * "count()" function
10273 */
10274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010275f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010277 long n = 0;
10278 int ic = FALSE;
10279
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010281 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010282 listitem_T *li;
10283 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010284 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010285
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286 if ((l = argvars[0].vval.v_list) != NULL)
10287 {
10288 li = l->lv_first;
10289 if (argvars[2].v_type != VAR_UNKNOWN)
10290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 int error = FALSE;
10292
10293 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294 if (argvars[3].v_type != VAR_UNKNOWN)
10295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010296 idx = get_tv_number_chk(&argvars[3], &error);
10297 if (!error)
10298 {
10299 li = list_find(l, idx);
10300 if (li == NULL)
10301 EMSGN(_(e_listidx), idx);
10302 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010304 if (error)
10305 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010306 }
10307
10308 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010309 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010310 ++n;
10311 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010312 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 else if (argvars[0].v_type == VAR_DICT)
10314 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010315 int todo;
10316 dict_T *d;
10317 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010319 if ((d = argvars[0].vval.v_dict) != NULL)
10320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010321 int error = FALSE;
10322
Bram Moolenaare9a41262005-01-15 22:18:47 +000010323 if (argvars[2].v_type != VAR_UNKNOWN)
10324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010325 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010326 if (argvars[3].v_type != VAR_UNKNOWN)
10327 EMSG(_(e_invarg));
10328 }
10329
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010330 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010331 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010332 {
10333 if (!HASHITEM_EMPTY(hi))
10334 {
10335 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010336 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010337 ++n;
10338 }
10339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010340 }
10341 }
10342 else
10343 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010344 rettv->vval.v_number = n;
10345}
10346
10347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10349 *
10350 * Checks the existence of a cscope connection.
10351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010353f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354{
10355#ifdef FEAT_CSCOPE
10356 int num = 0;
10357 char_u *dbpath = NULL;
10358 char_u *prepend = NULL;
10359 char_u buf[NUMBUFLEN];
10360
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010361 if (argvars[0].v_type != VAR_UNKNOWN
10362 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010364 num = (int)get_tv_number(&argvars[0]);
10365 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010366 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010367 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 }
10369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010370 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371#endif
10372}
10373
10374/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010375 * "cursor(lnum, col)" function, or
10376 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010378 * Moves the cursor to the specified line and column.
10379 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010382f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383{
10384 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010385#ifdef FEAT_VIRTUALEDIT
10386 long coladd = 0;
10387#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010388 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010390 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010391 if (argvars[1].v_type == VAR_UNKNOWN)
10392 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010393 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010394 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010395
Bram Moolenaar493c1782014-05-28 14:34:46 +020010396 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010397 {
10398 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010399 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010400 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010401 line = pos.lnum;
10402 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010403#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010404 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010405#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010406 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010407 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010408 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010409 set_curswant = FALSE;
10410 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010411 }
10412 else
10413 {
10414 line = get_tv_lnum(argvars);
10415 col = get_tv_number_chk(&argvars[1], NULL);
10416#ifdef FEAT_VIRTUALEDIT
10417 if (argvars[2].v_type != VAR_UNKNOWN)
10418 coladd = get_tv_number_chk(&argvars[2], NULL);
10419#endif
10420 }
10421 if (line < 0 || col < 0
10422#ifdef FEAT_VIRTUALEDIT
10423 || coladd < 0
10424#endif
10425 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010426 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 if (line > 0)
10428 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 if (col > 0)
10430 curwin->w_cursor.col = col - 1;
10431#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010432 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433#endif
10434
10435 /* Make sure the cursor is in a valid position. */
10436 check_cursor();
10437#ifdef FEAT_MBYTE
10438 /* Correct cursor for multi-byte character. */
10439 if (has_mbyte)
10440 mb_adjust_cursor();
10441#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010442
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010443 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010444 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445}
10446
10447/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010448 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 */
10450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010451f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010453 int noref = 0;
10454
10455 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010456 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010457 if (noref < 0 || noref > 1)
10458 EMSG(_(e_invarg));
10459 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010460 {
10461 current_copyID += COPYID_INC;
10462 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464}
10465
10466/*
10467 * "delete()" function
10468 */
10469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010470f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010472 char_u nbuf[NUMBUFLEN];
10473 char_u *name;
10474 char_u *flags;
10475
10476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010478 return;
10479
10480 name = get_tv_string(&argvars[0]);
10481 if (name == NULL || *name == NUL)
10482 {
10483 EMSG(_(e_invarg));
10484 return;
10485 }
10486
10487 if (argvars[1].v_type != VAR_UNKNOWN)
10488 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010490 flags = (char_u *)"";
10491
10492 if (*flags == NUL)
10493 /* delete a file */
10494 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10495 else if (STRCMP(flags, "d") == 0)
10496 /* delete an empty directory */
10497 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10498 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010499 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010500 rettv->vval.v_number = delete_recursive(name);
10501 else
10502 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503}
10504
10505/*
10506 * "did_filetype()" function
10507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010509f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510{
10511#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513#endif
10514}
10515
10516/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010517 * "diff_filler()" function
10518 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010520f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010521{
10522#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010524#endif
10525}
10526
10527/*
10528 * "diff_hlID()" function
10529 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010531f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010532{
10533#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010535 static linenr_T prev_lnum = 0;
10536 static int changedtick = 0;
10537 static int fnum = 0;
10538 static int change_start = 0;
10539 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010540 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010541 int filler_lines;
10542 int col;
10543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 if (lnum < 0) /* ignore type error in {lnum} arg */
10545 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010546 if (lnum != prev_lnum
10547 || changedtick != curbuf->b_changedtick
10548 || fnum != curbuf->b_fnum)
10549 {
10550 /* New line, buffer, change: need to get the values. */
10551 filler_lines = diff_check(curwin, lnum);
10552 if (filler_lines < 0)
10553 {
10554 if (filler_lines == -1)
10555 {
10556 change_start = MAXCOL;
10557 change_end = -1;
10558 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10559 hlID = HLF_ADD; /* added line */
10560 else
10561 hlID = HLF_CHD; /* changed line */
10562 }
10563 else
10564 hlID = HLF_ADD; /* added line */
10565 }
10566 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010567 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010568 prev_lnum = lnum;
10569 changedtick = curbuf->b_changedtick;
10570 fnum = curbuf->b_fnum;
10571 }
10572
10573 if (hlID == HLF_CHD || hlID == HLF_TXD)
10574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010576 if (col >= change_start && col <= change_end)
10577 hlID = HLF_TXD; /* changed text */
10578 else
10579 hlID = HLF_CHD; /* changed line */
10580 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010581 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010582#endif
10583}
10584
10585/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010586 * "empty({expr})" function
10587 */
10588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010589f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010590{
10591 int n;
10592
10593 switch (argvars[0].v_type)
10594 {
10595 case VAR_STRING:
10596 case VAR_FUNC:
10597 n = argvars[0].vval.v_string == NULL
10598 || *argvars[0].vval.v_string == NUL;
10599 break;
10600 case VAR_NUMBER:
10601 n = argvars[0].vval.v_number == 0;
10602 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010603 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010604#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010605 n = argvars[0].vval.v_float == 0.0;
10606 break;
10607#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010608 case VAR_LIST:
10609 n = argvars[0].vval.v_list == NULL
10610 || argvars[0].vval.v_list->lv_first == NULL;
10611 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010612 case VAR_DICT:
10613 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010614 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010615 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010616 case VAR_SPECIAL:
10617 n = argvars[0].vval.v_number != VVAL_TRUE;
10618 break;
10619
Bram Moolenaar835dc632016-02-07 14:27:38 +010010620 case VAR_JOB:
10621#ifdef FEAT_JOB
10622 n = argvars[0].vval.v_job->jv_status != JOB_STARTED;
10623 break;
10624#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010625 case VAR_UNKNOWN:
10626 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10627 n = TRUE;
10628 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010629 }
10630
10631 rettv->vval.v_number = n;
10632}
10633
10634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 * "escape({string}, {chars})" function
10636 */
10637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010638f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639{
10640 char_u buf[NUMBUFLEN];
10641
Bram Moolenaar758711c2005-02-02 23:11:38 +000010642 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10643 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010644 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645}
10646
10647/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010648 * "eval()" function
10649 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010651f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010652{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010653 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010655 s = get_tv_string_chk(&argvars[0]);
10656 if (s != NULL)
10657 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010658
Bram Moolenaar615b9972015-01-14 17:15:05 +010010659 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010660 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10661 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010662 if (p != NULL && !aborting())
10663 EMSG2(_(e_invexpr2), p);
10664 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010666 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010667 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010668 else if (*s != NUL)
10669 EMSG(_(e_trailing));
10670}
10671
10672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 * "eventhandler()" function
10674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010676f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
10681/*
10682 * "executable()" function
10683 */
10684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010685f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010687 char_u *name = get_tv_string(&argvars[0]);
10688
10689 /* Check in $PATH and also check directly if there is a directory name. */
10690 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10691 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010692}
10693
10694/*
10695 * "exepath()" function
10696 */
10697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010698f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010699{
10700 char_u *p = NULL;
10701
Bram Moolenaarb5971142015-03-21 17:32:19 +010010702 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010703 rettv->v_type = VAR_STRING;
10704 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705}
10706
10707/*
10708 * "exists()" function
10709 */
10710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010711f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712{
10713 char_u *p;
10714 char_u *name;
10715 int n = FALSE;
10716 int len = 0;
10717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 if (*p == '$') /* environment variable */
10720 {
10721 /* first try "normal" environment variables (fast) */
10722 if (mch_getenv(p + 1) != NULL)
10723 n = TRUE;
10724 else
10725 {
10726 /* try expanding things like $VIM and ${HOME} */
10727 p = expand_env_save(p);
10728 if (p != NULL && *p != '$')
10729 n = TRUE;
10730 vim_free(p);
10731 }
10732 }
10733 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010736 if (*skipwhite(p) != NUL)
10737 n = FALSE; /* trailing garbage */
10738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 else if (*p == '*') /* internal or user defined function */
10740 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010741 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 }
10743 else if (*p == ':')
10744 {
10745 n = cmd_exists(p + 1);
10746 }
10747 else if (*p == '#')
10748 {
10749#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010750 if (p[1] == '#')
10751 n = autocmd_supported(p + 2);
10752 else
10753 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754#endif
10755 }
10756 else /* internal variable */
10757 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010758 char_u *tofree;
10759 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010761 /* get_name_len() takes care of expanding curly braces */
10762 name = p;
10763 len = get_name_len(&p, &tofree, TRUE, FALSE);
10764 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010766 if (tofree != NULL)
10767 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010768 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010769 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010771 /* handle d.key, l[idx], f(expr) */
10772 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10773 if (n)
10774 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 }
10776 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010777 if (*p != NUL)
10778 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010780 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 }
10782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010783 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784}
10785
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010786#ifdef FEAT_FLOAT
10787/*
10788 * "exp()" function
10789 */
10790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010791f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010792{
10793 float_T f;
10794
10795 rettv->v_type = VAR_FLOAT;
10796 if (get_float_arg(argvars, &f) == OK)
10797 rettv->vval.v_float = exp(f);
10798 else
10799 rettv->vval.v_float = 0.0;
10800}
10801#endif
10802
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803/*
10804 * "expand()" function
10805 */
10806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010807f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808{
10809 char_u *s;
10810 int len;
10811 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010812 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010814 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010815 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010818 if (argvars[1].v_type != VAR_UNKNOWN
10819 && argvars[2].v_type != VAR_UNKNOWN
10820 && get_tv_number_chk(&argvars[2], &error)
10821 && !error)
10822 {
10823 rettv->v_type = VAR_LIST;
10824 rettv->vval.v_list = NULL;
10825 }
10826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 if (*s == '%' || *s == '#' || *s == '<')
10829 {
10830 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010831 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010833 if (rettv->v_type == VAR_LIST)
10834 {
10835 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10836 list_append_string(rettv->vval.v_list, result, -1);
10837 else
10838 vim_free(result);
10839 }
10840 else
10841 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 }
10843 else
10844 {
10845 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010846 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010847 if (argvars[1].v_type != VAR_UNKNOWN
10848 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010849 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010850 if (!error)
10851 {
10852 ExpandInit(&xpc);
10853 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010854 if (p_wic)
10855 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010856 if (rettv->v_type == VAR_STRING)
10857 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10858 options, WILD_ALL);
10859 else if (rettv_list_alloc(rettv) != FAIL)
10860 {
10861 int i;
10862
10863 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10864 for (i = 0; i < xpc.xp_numfiles; i++)
10865 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10866 ExpandCleanup(&xpc);
10867 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 }
10869 else
10870 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 }
10872}
10873
10874/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010875 * Go over all entries in "d2" and add them to "d1".
10876 * When "action" is "error" then a duplicate key is an error.
10877 * When "action" is "force" then a duplicate key is overwritten.
10878 * Otherwise duplicate keys are ignored ("action" is "keep").
10879 */
10880 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010881dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010882{
10883 dictitem_T *di1;
10884 hashitem_T *hi2;
10885 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010886 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010887
10888 todo = (int)d2->dv_hashtab.ht_used;
10889 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10890 {
10891 if (!HASHITEM_EMPTY(hi2))
10892 {
10893 --todo;
10894 di1 = dict_find(d1, hi2->hi_key, -1);
10895 if (d1->dv_scope != 0)
10896 {
10897 /* Disallow replacing a builtin function in l: and g:.
10898 * Check the key to be valid when adding to any
10899 * scope. */
10900 if (d1->dv_scope == VAR_DEF_SCOPE
10901 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10902 && var_check_func_name(hi2->hi_key,
10903 di1 == NULL))
10904 break;
10905 if (!valid_varname(hi2->hi_key))
10906 break;
10907 }
10908 if (di1 == NULL)
10909 {
10910 di1 = dictitem_copy(HI2DI(hi2));
10911 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10912 dictitem_free(di1);
10913 }
10914 else if (*action == 'e')
10915 {
10916 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10917 break;
10918 }
10919 else if (*action == 'f' && HI2DI(hi2) != di1)
10920 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010921 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10922 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010923 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010924 clear_tv(&di1->di_tv);
10925 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10926 }
10927 }
10928 }
10929}
10930
10931/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010932 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010933 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010934 */
10935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010936f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010937{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010938 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010939
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010941 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010942 list_T *l1, *l2;
10943 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010945 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010946
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947 l1 = argvars[0].vval.v_list;
10948 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010949 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010950 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951 {
10952 if (argvars[2].v_type != VAR_UNKNOWN)
10953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 before = get_tv_number_chk(&argvars[2], &error);
10955 if (error)
10956 return; /* type error; errmsg already given */
10957
Bram Moolenaar758711c2005-02-02 23:11:38 +000010958 if (before == l1->lv_len)
10959 item = NULL;
10960 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010962 item = list_find(l1, before);
10963 if (item == NULL)
10964 {
10965 EMSGN(_(e_listidx), before);
10966 return;
10967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 }
10969 }
10970 else
10971 item = NULL;
10972 list_extend(l1, l2, item);
10973
Bram Moolenaare9a41262005-01-15 22:18:47 +000010974 copy_tv(&argvars[0], rettv);
10975 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010977 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10978 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010979 dict_T *d1, *d2;
10980 char_u *action;
10981 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982
10983 d1 = argvars[0].vval.v_dict;
10984 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010985 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010986 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010987 {
10988 /* Check the third argument. */
10989 if (argvars[2].v_type != VAR_UNKNOWN)
10990 {
10991 static char *(av[]) = {"keep", "force", "error"};
10992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010993 action = get_tv_string_chk(&argvars[2]);
10994 if (action == NULL)
10995 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010996 for (i = 0; i < 3; ++i)
10997 if (STRCMP(action, av[i]) == 0)
10998 break;
10999 if (i == 3)
11000 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011001 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011002 return;
11003 }
11004 }
11005 else
11006 action = (char_u *)"force";
11007
Bram Moolenaara9922d62013-05-30 13:01:18 +020011008 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 copy_tv(&argvars[0], rettv);
11011 }
11012 }
11013 else
11014 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011015}
11016
11017/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011018 * "feedkeys()" function
11019 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011021f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011022{
11023 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011024 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011025 char_u *keys, *flags;
11026 char_u nbuf[NUMBUFLEN];
11027 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011028 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011029 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011030
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011031 /* This is not allowed in the sandbox. If the commands would still be
11032 * executed in the sandbox it would be OK, but it probably happens later,
11033 * when "sandbox" is no longer set. */
11034 if (check_secure())
11035 return;
11036
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011037 keys = get_tv_string(&argvars[0]);
11038 if (*keys != NUL)
11039 {
11040 if (argvars[1].v_type != VAR_UNKNOWN)
11041 {
11042 flags = get_tv_string_buf(&argvars[1], nbuf);
11043 for ( ; *flags != NUL; ++flags)
11044 {
11045 switch (*flags)
11046 {
11047 case 'n': remap = FALSE; break;
11048 case 'm': remap = TRUE; break;
11049 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011050 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011051 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011052 }
11053 }
11054 }
11055
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011056 /* Need to escape K_SPECIAL and CSI before putting the string in the
11057 * typeahead buffer. */
11058 keys_esc = vim_strsave_escape_csi(keys);
11059 if (keys_esc != NULL)
11060 {
11061 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011062 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011063 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011064 if (vgetc_busy)
11065 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011066 if (execute)
11067 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011068 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011069 }
11070}
11071
11072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 * "filereadable()" function
11074 */
11075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011076f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011078 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 char_u *p;
11080 int n;
11081
Bram Moolenaarc236c162008-07-13 17:41:49 +000011082#ifndef O_NONBLOCK
11083# define O_NONBLOCK 0
11084#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011086 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11087 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 {
11089 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011090 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 }
11092 else
11093 n = FALSE;
11094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096}
11097
11098/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011099 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 * rights to write into.
11101 */
11102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011103f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011105 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106}
11107
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011108 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011109findfilendir(
11110 typval_T *argvars UNUSED,
11111 typval_T *rettv,
11112 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011113{
11114#ifdef FEAT_SEARCHPATH
11115 char_u *fname;
11116 char_u *fresult = NULL;
11117 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11118 char_u *p;
11119 char_u pathbuf[NUMBUFLEN];
11120 int count = 1;
11121 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011122 int error = FALSE;
11123#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011124
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011125 rettv->vval.v_string = NULL;
11126 rettv->v_type = VAR_STRING;
11127
11128#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011130
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011131 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011133 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11134 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011135 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 else
11137 {
11138 if (*p != NUL)
11139 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011142 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011143 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011144 }
11145
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011146 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11147 error = TRUE;
11148
11149 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011151 do
11152 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011153 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011154 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 fresult = find_file_in_path_option(first ? fname : NULL,
11156 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011157 0, first, path,
11158 find_what,
11159 curbuf->b_ffname,
11160 find_what == FINDFILE_DIR
11161 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011162 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011163
11164 if (fresult != NULL && rettv->v_type == VAR_LIST)
11165 list_append_string(rettv->vval.v_list, fresult, -1);
11166
11167 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011168 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011169
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011170 if (rettv->v_type == VAR_STRING)
11171 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011172#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011173}
11174
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011175static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11176static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011177
11178/*
11179 * Implementation of map() and filter().
11180 */
11181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011182filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011183{
11184 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011185 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011186 listitem_T *li, *nli;
11187 list_T *l = NULL;
11188 dictitem_T *di;
11189 hashtab_T *ht;
11190 hashitem_T *hi;
11191 dict_T *d = NULL;
11192 typval_T save_val;
11193 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011194 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011195 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011196 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011197 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011198 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011199 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011200 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011201
Bram Moolenaare9a41262005-01-15 22:18:47 +000011202 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011203 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011204 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011205 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011206 return;
11207 }
11208 else if (argvars[0].v_type == VAR_DICT)
11209 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011210 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011211 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011212 return;
11213 }
11214 else
11215 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011216 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011217 return;
11218 }
11219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220 expr = get_tv_string_buf_chk(&argvars[1], buf);
11221 /* On type errors, the preceding call has already displayed an error
11222 * message. Avoid a misleading error message for an empty string that
11223 * was not passed as argument. */
11224 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 prepare_vimvar(VV_VAL, &save_val);
11227 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011228
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011229 /* We reset "did_emsg" to be able to detect whether an error
11230 * occurred during evaluation of the expression. */
11231 save_did_emsg = did_emsg;
11232 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011233
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011234 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011237 vimvars[VV_KEY].vv_type = VAR_STRING;
11238
11239 ht = &d->dv_hashtab;
11240 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011241 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011242 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011244 if (!HASHITEM_EMPTY(hi))
11245 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011246 int r;
11247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011248 --todo;
11249 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011250 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011251 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11252 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011253 break;
11254 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011255 r = filter_map_one(&di->di_tv, expr, map, &rem);
11256 clear_tv(&vimvars[VV_KEY].vv_tv);
11257 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011258 break;
11259 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011260 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011261 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11262 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011263 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011264 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011266 }
11267 }
11268 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 }
11270 else
11271 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011272 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011274 for (li = l->lv_first; li != NULL; li = nli)
11275 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011276 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011277 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011278 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011279 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011280 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011281 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011282 break;
11283 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011285 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011286 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011287 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011288
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011289 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011290 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011291
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011292 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011293 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011294
11295 copy_tv(&argvars[0], rettv);
11296}
11297
11298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011299filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011300{
Bram Moolenaar33570922005-01-25 22:26:29 +000011301 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011302 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011303 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011304
Bram Moolenaar33570922005-01-25 22:26:29 +000011305 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011306 s = expr;
11307 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011308 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011309 if (*s != NUL) /* check for trailing chars after expr */
11310 {
11311 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011312 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011313 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011314 }
11315 if (map)
11316 {
11317 /* map(): replace the list item value */
11318 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011319 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011320 *tv = rettv;
11321 }
11322 else
11323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 int error = FALSE;
11325
Bram Moolenaare9a41262005-01-15 22:18:47 +000011326 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011328 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011329 /* On type error, nothing has been removed; return FAIL to stop the
11330 * loop. The error message was given by get_tv_number_chk(). */
11331 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011332 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011333 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011334 retval = OK;
11335theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011336 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011337 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011338}
11339
11340/*
11341 * "filter()" function
11342 */
11343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011344f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011345{
11346 filter_map(argvars, rettv, FALSE);
11347}
11348
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011350 * "finddir({fname}[, {path}[, {count}]])" function
11351 */
11352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011353f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011354{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011355 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011356}
11357
11358/*
11359 * "findfile({fname}[, {path}[, {count}]])" function
11360 */
11361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011362f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011363{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011364 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011365}
11366
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011367#ifdef FEAT_FLOAT
11368/*
11369 * "float2nr({float})" function
11370 */
11371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011372f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011373{
11374 float_T f;
11375
11376 if (get_float_arg(argvars, &f) == OK)
11377 {
11378 if (f < -0x7fffffff)
11379 rettv->vval.v_number = -0x7fffffff;
11380 else if (f > 0x7fffffff)
11381 rettv->vval.v_number = 0x7fffffff;
11382 else
11383 rettv->vval.v_number = (varnumber_T)f;
11384 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011385}
11386
11387/*
11388 * "floor({float})" function
11389 */
11390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011391f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011392{
11393 float_T f;
11394
11395 rettv->v_type = VAR_FLOAT;
11396 if (get_float_arg(argvars, &f) == OK)
11397 rettv->vval.v_float = floor(f);
11398 else
11399 rettv->vval.v_float = 0.0;
11400}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011401
11402/*
11403 * "fmod()" function
11404 */
11405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011406f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011407{
11408 float_T fx, fy;
11409
11410 rettv->v_type = VAR_FLOAT;
11411 if (get_float_arg(argvars, &fx) == OK
11412 && get_float_arg(&argvars[1], &fy) == OK)
11413 rettv->vval.v_float = fmod(fx, fy);
11414 else
11415 rettv->vval.v_float = 0.0;
11416}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011417#endif
11418
Bram Moolenaar0d660222005-01-07 21:51:51 +000011419/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011420 * "fnameescape({string})" function
11421 */
11422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011423f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011424{
11425 rettv->vval.v_string = vim_strsave_fnameescape(
11426 get_tv_string(&argvars[0]), FALSE);
11427 rettv->v_type = VAR_STRING;
11428}
11429
11430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 * "fnamemodify({fname}, {mods})" function
11432 */
11433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011434f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435{
11436 char_u *fname;
11437 char_u *mods;
11438 int usedlen = 0;
11439 int len;
11440 char_u *fbuf = NULL;
11441 char_u buf[NUMBUFLEN];
11442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 fname = get_tv_string_chk(&argvars[0]);
11444 mods = get_tv_string_buf_chk(&argvars[1], buf);
11445 if (fname == NULL || mods == NULL)
11446 fname = NULL;
11447 else
11448 {
11449 len = (int)STRLEN(fname);
11450 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 vim_free(fbuf);
11459}
11460
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011461static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462
11463/*
11464 * "foldclosed()" function
11465 */
11466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011467foldclosed_both(
11468 typval_T *argvars UNUSED,
11469 typval_T *rettv,
11470 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471{
11472#ifdef FEAT_FOLDING
11473 linenr_T lnum;
11474 linenr_T first, last;
11475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11478 {
11479 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11480 {
11481 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 return;
11486 }
11487 }
11488#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490}
11491
11492/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011493 * "foldclosed()" function
11494 */
11495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011496f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497{
11498 foldclosed_both(argvars, rettv, FALSE);
11499}
11500
11501/*
11502 * "foldclosedend()" function
11503 */
11504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011505f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011506{
11507 foldclosed_both(argvars, rettv, TRUE);
11508}
11509
11510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 * "foldlevel()" function
11512 */
11513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011514f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515{
11516#ifdef FEAT_FOLDING
11517 linenr_T lnum;
11518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523}
11524
11525/*
11526 * "foldtext()" function
11527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011529f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531#ifdef FEAT_FOLDING
11532 linenr_T lnum;
11533 char_u *s;
11534 char_u *r;
11535 int len;
11536 char *txt;
11537#endif
11538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 rettv->v_type = VAR_STRING;
11540 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011542 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11543 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11544 <= curbuf->b_ml.ml_line_count
11545 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 {
11547 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11549 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 {
11551 if (!linewhite(lnum))
11552 break;
11553 ++lnum;
11554 }
11555
11556 /* Find interesting text in this line. */
11557 s = skipwhite(ml_get(lnum));
11558 /* skip C comment-start */
11559 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011562 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011564 {
11565 s = skipwhite(ml_get(lnum + 1));
11566 if (*s == '*')
11567 s = skipwhite(s + 1);
11568 }
11569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 txt = _("+-%s%3ld lines: ");
11571 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011572 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 + 20 /* for %3ld */
11574 + STRLEN(s))); /* concatenated */
11575 if (r != NULL)
11576 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011577 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11578 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11579 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 len = (int)STRLEN(r);
11581 STRCAT(r, s);
11582 /* remove 'foldmarker' and 'commentstring' */
11583 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 }
11586 }
11587#endif
11588}
11589
11590/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011591 * "foldtextresult(lnum)" function
11592 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011594f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011595{
11596#ifdef FEAT_FOLDING
11597 linenr_T lnum;
11598 char_u *text;
11599 char_u buf[51];
11600 foldinfo_T foldinfo;
11601 int fold_count;
11602#endif
11603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604 rettv->v_type = VAR_STRING;
11605 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011606#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011608 /* treat illegal types and illegal string values for {lnum} the same */
11609 if (lnum < 0)
11610 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011611 fold_count = foldedCount(curwin, lnum, &foldinfo);
11612 if (fold_count > 0)
11613 {
11614 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11615 &foldinfo, buf);
11616 if (text == buf)
11617 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011618 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011619 }
11620#endif
11621}
11622
11623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 * "foreground()" function
11625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011627f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629#ifdef FEAT_GUI
11630 if (gui.in_use)
11631 gui_mch_set_foreground();
11632#else
11633# ifdef WIN32
11634 win32_set_foreground();
11635# endif
11636#endif
11637}
11638
11639/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011640 * "function()" function
11641 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011643f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011644{
11645 char_u *s;
11646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011648 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011649 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011650 /* Don't check an autoload name for existence here. */
11651 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011652 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011653 else
11654 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011655 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011656 {
11657 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011658 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011659
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011660 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11661 * also be called from another script. Using trans_function_name()
11662 * would also work, but some plugins depend on the name being
11663 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011664 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011665 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011666 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011667 if (rettv->vval.v_string != NULL)
11668 {
11669 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011670 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011671 }
11672 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011673 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011674 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011676 }
11677}
11678
11679/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011680 * "garbagecollect()" function
11681 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011683f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011684{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011685 /* This is postponed until we are back at the toplevel, because we may be
11686 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11687 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011688
11689 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11690 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011691}
11692
11693/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011694 * "get()" function
11695 */
11696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011697f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011698{
Bram Moolenaar33570922005-01-25 22:26:29 +000011699 listitem_T *li;
11700 list_T *l;
11701 dictitem_T *di;
11702 dict_T *d;
11703 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011704
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011706 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011707 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 int error = FALSE;
11710
11711 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11712 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011713 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011714 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011715 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011716 else if (argvars[0].v_type == VAR_DICT)
11717 {
11718 if ((d = argvars[0].vval.v_dict) != NULL)
11719 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011720 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011721 if (di != NULL)
11722 tv = &di->di_tv;
11723 }
11724 }
11725 else
11726 EMSG2(_(e_listdictarg), "get()");
11727
11728 if (tv == NULL)
11729 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011731 copy_tv(&argvars[2], rettv);
11732 }
11733 else
11734 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011735}
11736
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011737static 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 +000011738
11739/*
11740 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011741 * Return a range (from start to end) of lines in rettv from the specified
11742 * buffer.
11743 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011744 */
11745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011746get_buffer_lines(
11747 buf_T *buf,
11748 linenr_T start,
11749 linenr_T end,
11750 int retlist,
11751 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011752{
11753 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011754
Bram Moolenaar959a1432013-12-14 12:17:38 +010011755 rettv->v_type = VAR_STRING;
11756 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011757 if (retlist && rettv_list_alloc(rettv) == FAIL)
11758 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011759
11760 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11761 return;
11762
11763 if (!retlist)
11764 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011765 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11766 p = ml_get_buf(buf, start, FALSE);
11767 else
11768 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011769 rettv->vval.v_string = vim_strsave(p);
11770 }
11771 else
11772 {
11773 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011774 return;
11775
11776 if (start < 1)
11777 start = 1;
11778 if (end > buf->b_ml.ml_line_count)
11779 end = buf->b_ml.ml_line_count;
11780 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011781 if (list_append_string(rettv->vval.v_list,
11782 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011783 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011784 }
11785}
11786
11787/*
11788 * "getbufline()" function
11789 */
11790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011791f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011792{
11793 linenr_T lnum;
11794 linenr_T end;
11795 buf_T *buf;
11796
11797 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11798 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011799 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011800 --emsg_off;
11801
Bram Moolenaar661b1822005-07-28 22:36:45 +000011802 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011803 if (argvars[2].v_type == VAR_UNKNOWN)
11804 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011805 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011806 end = get_tv_lnum_buf(&argvars[2], buf);
11807
Bram Moolenaar342337a2005-07-21 21:11:17 +000011808 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011809}
11810
Bram Moolenaar0d660222005-01-07 21:51:51 +000011811/*
11812 * "getbufvar()" function
11813 */
11814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011815f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011816{
11817 buf_T *buf;
11818 buf_T *save_curbuf;
11819 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011820 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011821 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11824 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011825 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011826 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011827
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011828 rettv->v_type = VAR_STRING;
11829 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011830
11831 if (buf != NULL && varname != NULL)
11832 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011833 /* set curbuf to be our buf, temporarily */
11834 save_curbuf = curbuf;
11835 curbuf = buf;
11836
Bram Moolenaar0d660222005-01-07 21:51:51 +000011837 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011838 {
11839 if (get_option_tv(&varname, rettv, TRUE) == OK)
11840 done = TRUE;
11841 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011842 else if (STRCMP(varname, "changedtick") == 0)
11843 {
11844 rettv->v_type = VAR_NUMBER;
11845 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011846 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011847 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011848 else
11849 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011850 /* Look up the variable. */
11851 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11852 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11853 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011854 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011855 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011856 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011857 done = TRUE;
11858 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011859 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011860
11861 /* restore previous notion of curbuf */
11862 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011863 }
11864
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011865 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11866 /* use the default value */
11867 copy_tv(&argvars[2], rettv);
11868
Bram Moolenaar0d660222005-01-07 21:51:51 +000011869 --emsg_off;
11870}
11871
11872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 * "getchar()" function
11874 */
11875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011876f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877{
11878 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011879 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011881 /* Position the cursor. Needed after a message that ends in a space. */
11882 windgoto(msg_row, msg_col);
11883
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 ++no_mapping;
11885 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011886 for (;;)
11887 {
11888 if (argvars[0].v_type == VAR_UNKNOWN)
11889 /* getchar(): blocking wait. */
11890 n = safe_vgetc();
11891 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11892 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011893 n = vpeekc_any();
11894 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011895 /* illegal argument or getchar(0) and no char avail: return zero */
11896 n = 0;
11897 else
11898 /* getchar(0) and char avail: return char */
11899 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011900
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011901 if (n == K_IGNORE)
11902 continue;
11903 break;
11904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 --no_mapping;
11906 --allow_keys;
11907
Bram Moolenaar219b8702006-11-01 14:32:36 +000011908 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11909 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11910 vimvars[VV_MOUSE_COL].vv_nr = 0;
11911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 if (IS_SPECIAL(n) || mod_mask != 0)
11914 {
11915 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11916 int i = 0;
11917
11918 /* Turn a special key into three bytes, plus modifier. */
11919 if (mod_mask != 0)
11920 {
11921 temp[i++] = K_SPECIAL;
11922 temp[i++] = KS_MODIFIER;
11923 temp[i++] = mod_mask;
11924 }
11925 if (IS_SPECIAL(n))
11926 {
11927 temp[i++] = K_SPECIAL;
11928 temp[i++] = K_SECOND(n);
11929 temp[i++] = K_THIRD(n);
11930 }
11931#ifdef FEAT_MBYTE
11932 else if (has_mbyte)
11933 i += (*mb_char2bytes)(n, temp + i);
11934#endif
11935 else
11936 temp[i++] = n;
11937 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011938 rettv->v_type = VAR_STRING;
11939 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011940
11941#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011942 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011943 {
11944 int row = mouse_row;
11945 int col = mouse_col;
11946 win_T *win;
11947 linenr_T lnum;
11948# ifdef FEAT_WINDOWS
11949 win_T *wp;
11950# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011951 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011952
11953 if (row >= 0 && col >= 0)
11954 {
11955 /* Find the window at the mouse coordinates and compute the
11956 * text position. */
11957 win = mouse_find_win(&row, &col);
11958 (void)mouse_comp_pos(win, &row, &col, &lnum);
11959# ifdef FEAT_WINDOWS
11960 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011961 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011962# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011963 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011964 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11965 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11966 }
11967 }
11968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 }
11970}
11971
11972/*
11973 * "getcharmod()" function
11974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011976f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979}
11980
11981/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011982 * "getcharsearch()" function
11983 */
11984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011985f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011986{
11987 if (rettv_dict_alloc(rettv) != FAIL)
11988 {
11989 dict_T *dict = rettv->vval.v_dict;
11990
11991 dict_add_nr_str(dict, "char", 0L, last_csearch());
11992 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11993 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11994 }
11995}
11996
11997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 * "getcmdline()" function
11999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012001f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003 rettv->v_type = VAR_STRING;
12004 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005}
12006
12007/*
12008 * "getcmdpos()" function
12009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012011f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014}
12015
12016/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012017 * "getcmdtype()" function
12018 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012020f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012021{
12022 rettv->v_type = VAR_STRING;
12023 rettv->vval.v_string = alloc(2);
12024 if (rettv->vval.v_string != NULL)
12025 {
12026 rettv->vval.v_string[0] = get_cmdline_type();
12027 rettv->vval.v_string[1] = NUL;
12028 }
12029}
12030
12031/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012032 * "getcmdwintype()" function
12033 */
12034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012035f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012036{
12037 rettv->v_type = VAR_STRING;
12038 rettv->vval.v_string = NULL;
12039#ifdef FEAT_CMDWIN
12040 rettv->vval.v_string = alloc(2);
12041 if (rettv->vval.v_string != NULL)
12042 {
12043 rettv->vval.v_string[0] = cmdwin_type;
12044 rettv->vval.v_string[1] = NUL;
12045 }
12046#endif
12047}
12048
12049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 * "getcwd()" function
12051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012053f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012055 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012056 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012059 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012060
12061 wp = find_tabwin(&argvars[0], &argvars[1]);
12062 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012064 if (wp->w_localdir != NULL)
12065 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12066 else if(globaldir != NULL)
12067 rettv->vval.v_string = vim_strsave(globaldir);
12068 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012069 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012070 cwd = alloc(MAXPATHL);
12071 if (cwd != NULL)
12072 {
12073 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12074 rettv->vval.v_string = vim_strsave(cwd);
12075 vim_free(cwd);
12076 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012077 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012078#ifdef BACKSLASH_IN_FILENAME
12079 if (rettv->vval.v_string != NULL)
12080 slash_adjust(rettv->vval.v_string);
12081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 }
12083}
12084
12085/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012086 * "getfontname()" function
12087 */
12088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012089f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012090{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091 rettv->v_type = VAR_STRING;
12092 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012093#ifdef FEAT_GUI
12094 if (gui.in_use)
12095 {
12096 GuiFont font;
12097 char_u *name = NULL;
12098
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012099 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012100 {
12101 /* Get the "Normal" font. Either the name saved by
12102 * hl_set_font_name() or from the font ID. */
12103 font = gui.norm_font;
12104 name = hl_get_font_name();
12105 }
12106 else
12107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012109 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12110 return;
12111 font = gui_mch_get_font(name, FALSE);
12112 if (font == NOFONT)
12113 return; /* Invalid font name, return empty string. */
12114 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012115 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012116 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012117 gui_mch_free_font(font);
12118 }
12119#endif
12120}
12121
12122/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012123 * "getfperm({fname})" function
12124 */
12125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012126f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012127{
12128 char_u *fname;
12129 struct stat st;
12130 char_u *perm = NULL;
12131 char_u flags[] = "rwx";
12132 int i;
12133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012137 if (mch_stat((char *)fname, &st) >= 0)
12138 {
12139 perm = vim_strsave((char_u *)"---------");
12140 if (perm != NULL)
12141 {
12142 for (i = 0; i < 9; i++)
12143 {
12144 if (st.st_mode & (1 << (8 - i)))
12145 perm[i] = flags[i % 3];
12146 }
12147 }
12148 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012150}
12151
12152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 * "getfsize({fname})" function
12154 */
12155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012156f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157{
12158 char_u *fname;
12159 struct stat st;
12160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012161 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012163 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164
12165 if (mch_stat((char *)fname, &st) >= 0)
12166 {
12167 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012171 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012172
12173 /* non-perfect check for overflow */
12174 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12175 rettv->vval.v_number = -2;
12176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 }
12178 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180}
12181
12182/*
12183 * "getftime({fname})" function
12184 */
12185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012186f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187{
12188 char_u *fname;
12189 struct stat st;
12190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192
12193 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197}
12198
12199/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012200 * "getftype({fname})" function
12201 */
12202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012203f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012204{
12205 char_u *fname;
12206 struct stat st;
12207 char_u *type = NULL;
12208 char *t;
12209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012210 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012212 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012213 if (mch_lstat((char *)fname, &st) >= 0)
12214 {
12215#ifdef S_ISREG
12216 if (S_ISREG(st.st_mode))
12217 t = "file";
12218 else if (S_ISDIR(st.st_mode))
12219 t = "dir";
12220# ifdef S_ISLNK
12221 else if (S_ISLNK(st.st_mode))
12222 t = "link";
12223# endif
12224# ifdef S_ISBLK
12225 else if (S_ISBLK(st.st_mode))
12226 t = "bdev";
12227# endif
12228# ifdef S_ISCHR
12229 else if (S_ISCHR(st.st_mode))
12230 t = "cdev";
12231# endif
12232# ifdef S_ISFIFO
12233 else if (S_ISFIFO(st.st_mode))
12234 t = "fifo";
12235# endif
12236# ifdef S_ISSOCK
12237 else if (S_ISSOCK(st.st_mode))
12238 t = "fifo";
12239# endif
12240 else
12241 t = "other";
12242#else
12243# ifdef S_IFMT
12244 switch (st.st_mode & S_IFMT)
12245 {
12246 case S_IFREG: t = "file"; break;
12247 case S_IFDIR: t = "dir"; break;
12248# ifdef S_IFLNK
12249 case S_IFLNK: t = "link"; break;
12250# endif
12251# ifdef S_IFBLK
12252 case S_IFBLK: t = "bdev"; break;
12253# endif
12254# ifdef S_IFCHR
12255 case S_IFCHR: t = "cdev"; break;
12256# endif
12257# ifdef S_IFIFO
12258 case S_IFIFO: t = "fifo"; break;
12259# endif
12260# ifdef S_IFSOCK
12261 case S_IFSOCK: t = "socket"; break;
12262# endif
12263 default: t = "other";
12264 }
12265# else
12266 if (mch_isdir(fname))
12267 t = "dir";
12268 else
12269 t = "file";
12270# endif
12271#endif
12272 type = vim_strsave((char_u *)t);
12273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012274 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012275}
12276
12277/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012278 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012279 */
12280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012281f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012282{
12283 linenr_T lnum;
12284 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012285 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012286
12287 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012288 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012289 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012290 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012291 retlist = FALSE;
12292 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012293 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012294 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012295 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012296 retlist = TRUE;
12297 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012298
Bram Moolenaar342337a2005-07-21 21:11:17 +000012299 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300}
12301
12302/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012303 * "getmatches()" function
12304 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012306f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012307{
12308#ifdef FEAT_SEARCH_EXTRA
12309 dict_T *dict;
12310 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012311 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012312
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012313 if (rettv_list_alloc(rettv) == OK)
12314 {
12315 while (cur != NULL)
12316 {
12317 dict = dict_alloc();
12318 if (dict == NULL)
12319 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012320 if (cur->match.regprog == NULL)
12321 {
12322 /* match added with matchaddpos() */
12323 for (i = 0; i < MAXPOSMATCH; ++i)
12324 {
12325 llpos_T *llpos;
12326 char buf[6];
12327 list_T *l;
12328
12329 llpos = &cur->pos.pos[i];
12330 if (llpos->lnum == 0)
12331 break;
12332 l = list_alloc();
12333 if (l == NULL)
12334 break;
12335 list_append_number(l, (varnumber_T)llpos->lnum);
12336 if (llpos->col > 0)
12337 {
12338 list_append_number(l, (varnumber_T)llpos->col);
12339 list_append_number(l, (varnumber_T)llpos->len);
12340 }
12341 sprintf(buf, "pos%d", i + 1);
12342 dict_add_list(dict, buf, l);
12343 }
12344 }
12345 else
12346 {
12347 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12348 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012349 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012350 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12351 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012352# ifdef FEAT_CONCEAL
12353 if (cur->conceal_char)
12354 {
12355 char_u buf[MB_MAXBYTES + 1];
12356
12357 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12358 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12359 }
12360# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012361 list_append_dict(rettv->vval.v_list, dict);
12362 cur = cur->next;
12363 }
12364 }
12365#endif
12366}
12367
12368/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012369 * "getpid()" function
12370 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012372f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012373{
12374 rettv->vval.v_number = mch_get_pid();
12375}
12376
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012377static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012378
12379/*
12380 * "getcurpos()" function
12381 */
12382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012383f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012384{
12385 getpos_both(argvars, rettv, TRUE);
12386}
12387
Bram Moolenaar18081e32008-02-20 19:11:07 +000012388/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012389 * "getpos(string)" function
12390 */
12391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012392f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012393{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012394 getpos_both(argvars, rettv, FALSE);
12395}
12396
12397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012398getpos_both(
12399 typval_T *argvars,
12400 typval_T *rettv,
12401 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012402{
Bram Moolenaara5525202006-03-02 22:52:09 +000012403 pos_T *fp;
12404 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012405 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012406
12407 if (rettv_list_alloc(rettv) == OK)
12408 {
12409 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012410 if (getcurpos)
12411 fp = &curwin->w_cursor;
12412 else
12413 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012414 if (fnum != -1)
12415 list_append_number(l, (varnumber_T)fnum);
12416 else
12417 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012418 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12419 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012420 list_append_number(l, (fp != NULL)
12421 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012422 : (varnumber_T)0);
12423 list_append_number(l,
12424#ifdef FEAT_VIRTUALEDIT
12425 (fp != NULL) ? (varnumber_T)fp->coladd :
12426#endif
12427 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012428 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012429 list_append_number(l, curwin->w_curswant == MAXCOL ?
12430 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012431 }
12432 else
12433 rettv->vval.v_number = FALSE;
12434}
12435
12436/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012437 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012438 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012440f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012441{
12442#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012443 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012444#endif
12445
Bram Moolenaar2641f772005-03-25 21:58:17 +000012446#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012447 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012448 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012449 wp = NULL;
12450 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12451 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012452 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012453 if (wp == NULL)
12454 return;
12455 }
12456
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012457 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012458 }
12459#endif
12460}
12461
12462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463 * "getreg()" function
12464 */
12465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012466f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467{
12468 char_u *strregname;
12469 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012470 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012471 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012472 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012474 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012476 strregname = get_tv_string_chk(&argvars[0]);
12477 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012478 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012480 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012481 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12482 return_list = get_tv_number_chk(&argvars[2], &error);
12483 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012486 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012487
12488 if (error)
12489 return;
12490
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 regname = (strregname == NULL ? '"' : *strregname);
12492 if (regname == 0)
12493 regname = '"';
12494
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012495 if (return_list)
12496 {
12497 rettv->v_type = VAR_LIST;
12498 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12499 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012500 if (rettv->vval.v_list != NULL)
12501 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012502 }
12503 else
12504 {
12505 rettv->v_type = VAR_STRING;
12506 rettv->vval.v_string = get_reg_contents(regname,
12507 arg2 ? GREG_EXPR_SRC : 0);
12508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509}
12510
12511/*
12512 * "getregtype()" function
12513 */
12514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012515f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516{
12517 char_u *strregname;
12518 int regname;
12519 char_u buf[NUMBUFLEN + 2];
12520 long reglen = 0;
12521
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012522 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012523 {
12524 strregname = get_tv_string_chk(&argvars[0]);
12525 if (strregname == NULL) /* type error; errmsg already given */
12526 {
12527 rettv->v_type = VAR_STRING;
12528 rettv->vval.v_string = NULL;
12529 return;
12530 }
12531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 else
12533 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012534 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535
12536 regname = (strregname == NULL ? '"' : *strregname);
12537 if (regname == 0)
12538 regname = '"';
12539
12540 buf[0] = NUL;
12541 buf[1] = NUL;
12542 switch (get_reg_type(regname, &reglen))
12543 {
12544 case MLINE: buf[0] = 'V'; break;
12545 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546 case MBLOCK:
12547 buf[0] = Ctrl_V;
12548 sprintf((char *)buf + 1, "%ld", reglen + 1);
12549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012551 rettv->v_type = VAR_STRING;
12552 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553}
12554
12555/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012556 * "gettabvar()" function
12557 */
12558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012559f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012560{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012561 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012562 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012563 dictitem_T *v;
12564 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012565 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012566
12567 rettv->v_type = VAR_STRING;
12568 rettv->vval.v_string = NULL;
12569
12570 varname = get_tv_string_chk(&argvars[1]);
12571 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12572 if (tp != NULL && varname != NULL)
12573 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012574 /* Set tp to be our tabpage, temporarily. Also set the window to the
12575 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012576 if (switch_win(&oldcurwin, &oldtabpage,
12577 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012578 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012579 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012580 /* look up the variable */
12581 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12582 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12583 if (v != NULL)
12584 {
12585 copy_tv(&v->di_tv, rettv);
12586 done = TRUE;
12587 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012588 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012589
12590 /* restore previous notion of curwin */
12591 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012592 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012593
12594 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012595 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012596}
12597
12598/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012599 * "gettabwinvar()" function
12600 */
12601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012602f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012603{
12604 getwinvar(argvars, rettv, 1);
12605}
12606
12607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 * "getwinposx()" function
12609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012611f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614#ifdef FEAT_GUI
12615 if (gui.in_use)
12616 {
12617 int x, y;
12618
12619 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012620 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 }
12622#endif
12623}
12624
12625/*
12626 * "getwinposy()" function
12627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012629f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012631 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632#ifdef FEAT_GUI
12633 if (gui.in_use)
12634 {
12635 int x, y;
12636
12637 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639 }
12640#endif
12641}
12642
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012643/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012644 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012645 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012646 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012647find_win_by_nr(
12648 typval_T *vp,
12649 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012650{
12651#ifdef FEAT_WINDOWS
12652 win_T *wp;
12653#endif
12654 int nr;
12655
12656 nr = get_tv_number_chk(vp, NULL);
12657
12658#ifdef FEAT_WINDOWS
12659 if (nr < 0)
12660 return NULL;
12661 if (nr == 0)
12662 return curwin;
12663
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012664 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12665 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012666 if (--nr <= 0)
12667 break;
12668 return wp;
12669#else
12670 if (nr == 0 || nr == 1)
12671 return curwin;
12672 return NULL;
12673#endif
12674}
12675
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012677 * Find window specified by "wvp" in tabpage "tvp".
12678 */
12679 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012680find_tabwin(
12681 typval_T *wvp, /* VAR_UNKNOWN for current window */
12682 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012683{
12684 win_T *wp = NULL;
12685 tabpage_T *tp = NULL;
12686 long n;
12687
12688 if (wvp->v_type != VAR_UNKNOWN)
12689 {
12690 if (tvp->v_type != VAR_UNKNOWN)
12691 {
12692 n = get_tv_number(tvp);
12693 if (n >= 0)
12694 tp = find_tabpage(n);
12695 }
12696 else
12697 tp = curtab;
12698
12699 if (tp != NULL)
12700 wp = find_win_by_nr(wvp, tp);
12701 }
12702 else
12703 wp = curwin;
12704
12705 return wp;
12706}
12707
12708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 * "getwinvar()" function
12710 */
12711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012712f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012714 getwinvar(argvars, rettv, 0);
12715}
12716
12717/*
12718 * getwinvar() and gettabwinvar()
12719 */
12720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012721getwinvar(
12722 typval_T *argvars,
12723 typval_T *rettv,
12724 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012725{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012726 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012728 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012729 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012730 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012731#ifdef FEAT_WINDOWS
12732 win_T *oldcurwin;
12733 tabpage_T *oldtabpage;
12734 int need_switch_win;
12735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012737#ifdef FEAT_WINDOWS
12738 if (off == 1)
12739 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12740 else
12741 tp = curtab;
12742#endif
12743 win = find_win_by_nr(&argvars[off], tp);
12744 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012747 rettv->v_type = VAR_STRING;
12748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749
12750 if (win != NULL && varname != NULL)
12751 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012752#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012753 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012754 * otherwise the window is not valid. Only do this when needed,
12755 * autocommands get blocked. */
12756 need_switch_win = !(tp == curtab && win == curwin);
12757 if (!need_switch_win
12758 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12759#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012760 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012761 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012762 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012763 if (get_option_tv(&varname, rettv, 1) == OK)
12764 done = TRUE;
12765 }
12766 else
12767 {
12768 /* Look up the variable. */
12769 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12770 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12771 varname, FALSE);
12772 if (v != NULL)
12773 {
12774 copy_tv(&v->di_tv, rettv);
12775 done = TRUE;
12776 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012779
Bram Moolenaarba117c22015-09-29 16:53:22 +020012780#ifdef FEAT_WINDOWS
12781 if (need_switch_win)
12782 /* restore previous notion of curwin */
12783 restore_win(oldcurwin, oldtabpage, TRUE);
12784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 }
12786
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012787 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12788 /* use the default return value */
12789 copy_tv(&argvars[off + 2], rettv);
12790
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 --emsg_off;
12792}
12793
12794/*
12795 * "glob()" function
12796 */
12797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012798f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012800 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012802 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012804 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012805 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012807 if (argvars[1].v_type != VAR_UNKNOWN)
12808 {
12809 if (get_tv_number_chk(&argvars[1], &error))
12810 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012811 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012812 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012813 if (get_tv_number_chk(&argvars[2], &error))
12814 {
12815 rettv->v_type = VAR_LIST;
12816 rettv->vval.v_list = NULL;
12817 }
12818 if (argvars[3].v_type != VAR_UNKNOWN
12819 && get_tv_number_chk(&argvars[3], &error))
12820 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012821 }
12822 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012823 if (!error)
12824 {
12825 ExpandInit(&xpc);
12826 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012827 if (p_wic)
12828 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012829 if (rettv->v_type == VAR_STRING)
12830 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012831 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012832 else if (rettv_list_alloc(rettv) != FAIL)
12833 {
12834 int i;
12835
12836 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12837 NULL, options, WILD_ALL_KEEP);
12838 for (i = 0; i < xpc.xp_numfiles; i++)
12839 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12840
12841 ExpandCleanup(&xpc);
12842 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012843 }
12844 else
12845 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846}
12847
12848/*
12849 * "globpath()" function
12850 */
12851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012852f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012854 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012856 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012857 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012858 garray_T ga;
12859 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012861 /* When the optional second argument is non-zero, don't remove matches
12862 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012864 if (argvars[2].v_type != VAR_UNKNOWN)
12865 {
12866 if (get_tv_number_chk(&argvars[2], &error))
12867 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012868 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012869 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012870 if (get_tv_number_chk(&argvars[3], &error))
12871 {
12872 rettv->v_type = VAR_LIST;
12873 rettv->vval.v_list = NULL;
12874 }
12875 if (argvars[4].v_type != VAR_UNKNOWN
12876 && get_tv_number_chk(&argvars[4], &error))
12877 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012878 }
12879 }
12880 if (file != NULL && !error)
12881 {
12882 ga_init2(&ga, (int)sizeof(char_u *), 10);
12883 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12884 if (rettv->v_type == VAR_STRING)
12885 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12886 else if (rettv_list_alloc(rettv) != FAIL)
12887 for (i = 0; i < ga.ga_len; ++i)
12888 list_append_string(rettv->vval.v_list,
12889 ((char_u **)(ga.ga_data))[i], -1);
12890 ga_clear_strings(&ga);
12891 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012892 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012893 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894}
12895
12896/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012897 * "glob2regpat()" function
12898 */
12899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012900f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012901{
12902 char_u *pat = get_tv_string_chk(&argvars[0]);
12903
12904 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012905 rettv->vval.v_string = (pat == NULL)
12906 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012907}
12908
12909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910 * "has()" function
12911 */
12912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012913f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914{
12915 int i;
12916 char_u *name;
12917 int n = FALSE;
12918 static char *(has_list[]) =
12919 {
12920#ifdef AMIGA
12921 "amiga",
12922# ifdef FEAT_ARP
12923 "arp",
12924# endif
12925#endif
12926#ifdef __BEOS__
12927 "beos",
12928#endif
12929#ifdef MSDOS
12930# ifdef DJGPP
12931 "dos32",
12932# else
12933 "dos16",
12934# endif
12935#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012936#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 "mac",
12938#endif
12939#if defined(MACOS_X_UNIX)
12940 "macunix",
12941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942#ifdef __QNX__
12943 "qnx",
12944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945#ifdef UNIX
12946 "unix",
12947#endif
12948#ifdef VMS
12949 "vms",
12950#endif
12951#ifdef WIN16
12952 "win16",
12953#endif
12954#ifdef WIN32
12955 "win32",
12956#endif
12957#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12958 "win32unix",
12959#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012960#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 "win64",
12962#endif
12963#ifdef EBCDIC
12964 "ebcdic",
12965#endif
12966#ifndef CASE_INSENSITIVE_FILENAME
12967 "fname_case",
12968#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012969#ifdef HAVE_ACL
12970 "acl",
12971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972#ifdef FEAT_ARABIC
12973 "arabic",
12974#endif
12975#ifdef FEAT_AUTOCMD
12976 "autocmd",
12977#endif
12978#ifdef FEAT_BEVAL
12979 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012980# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12981 "balloon_multiline",
12982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983#endif
12984#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12985 "builtin_terms",
12986# ifdef ALL_BUILTIN_TCAPS
12987 "all_builtin_terms",
12988# endif
12989#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012990#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12991 || defined(FEAT_GUI_W32) \
12992 || defined(FEAT_GUI_MOTIF))
12993 "browsefilter",
12994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995#ifdef FEAT_BYTEOFF
12996 "byte_offset",
12997#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012998#ifdef FEAT_CHANNEL
12999 "channel",
13000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001#ifdef FEAT_CINDENT
13002 "cindent",
13003#endif
13004#ifdef FEAT_CLIENTSERVER
13005 "clientserver",
13006#endif
13007#ifdef FEAT_CLIPBOARD
13008 "clipboard",
13009#endif
13010#ifdef FEAT_CMDL_COMPL
13011 "cmdline_compl",
13012#endif
13013#ifdef FEAT_CMDHIST
13014 "cmdline_hist",
13015#endif
13016#ifdef FEAT_COMMENTS
13017 "comments",
13018#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013019#ifdef FEAT_CONCEAL
13020 "conceal",
13021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022#ifdef FEAT_CRYPT
13023 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013024 "crypt-blowfish",
13025 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026#endif
13027#ifdef FEAT_CSCOPE
13028 "cscope",
13029#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013030#ifdef FEAT_CURSORBIND
13031 "cursorbind",
13032#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013033#ifdef CURSOR_SHAPE
13034 "cursorshape",
13035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036#ifdef DEBUG
13037 "debug",
13038#endif
13039#ifdef FEAT_CON_DIALOG
13040 "dialog_con",
13041#endif
13042#ifdef FEAT_GUI_DIALOG
13043 "dialog_gui",
13044#endif
13045#ifdef FEAT_DIFF
13046 "diff",
13047#endif
13048#ifdef FEAT_DIGRAPHS
13049 "digraphs",
13050#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013051#ifdef FEAT_DIRECTX
13052 "directx",
13053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054#ifdef FEAT_DND
13055 "dnd",
13056#endif
13057#ifdef FEAT_EMACS_TAGS
13058 "emacs_tags",
13059#endif
13060 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013061 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062#ifdef FEAT_SEARCH_EXTRA
13063 "extra_search",
13064#endif
13065#ifdef FEAT_FKMAP
13066 "farsi",
13067#endif
13068#ifdef FEAT_SEARCHPATH
13069 "file_in_path",
13070#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013071#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013072 "filterpipe",
13073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074#ifdef FEAT_FIND_ID
13075 "find_in_path",
13076#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013077#ifdef FEAT_FLOAT
13078 "float",
13079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080#ifdef FEAT_FOLDING
13081 "folding",
13082#endif
13083#ifdef FEAT_FOOTER
13084 "footer",
13085#endif
13086#if !defined(USE_SYSTEM) && defined(UNIX)
13087 "fork",
13088#endif
13089#ifdef FEAT_GETTEXT
13090 "gettext",
13091#endif
13092#ifdef FEAT_GUI
13093 "gui",
13094#endif
13095#ifdef FEAT_GUI_ATHENA
13096# ifdef FEAT_GUI_NEXTAW
13097 "gui_neXtaw",
13098# else
13099 "gui_athena",
13100# endif
13101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102#ifdef FEAT_GUI_GTK
13103 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013106#ifdef FEAT_GUI_GNOME
13107 "gui_gnome",
13108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109#ifdef FEAT_GUI_MAC
13110 "gui_mac",
13111#endif
13112#ifdef FEAT_GUI_MOTIF
13113 "gui_motif",
13114#endif
13115#ifdef FEAT_GUI_PHOTON
13116 "gui_photon",
13117#endif
13118#ifdef FEAT_GUI_W16
13119 "gui_win16",
13120#endif
13121#ifdef FEAT_GUI_W32
13122 "gui_win32",
13123#endif
13124#ifdef FEAT_HANGULIN
13125 "hangul_input",
13126#endif
13127#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13128 "iconv",
13129#endif
13130#ifdef FEAT_INS_EXPAND
13131 "insert_expand",
13132#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013133#ifdef FEAT_JOB
13134 "job",
13135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136#ifdef FEAT_JUMPLIST
13137 "jumplist",
13138#endif
13139#ifdef FEAT_KEYMAP
13140 "keymap",
13141#endif
13142#ifdef FEAT_LANGMAP
13143 "langmap",
13144#endif
13145#ifdef FEAT_LIBCALL
13146 "libcall",
13147#endif
13148#ifdef FEAT_LINEBREAK
13149 "linebreak",
13150#endif
13151#ifdef FEAT_LISP
13152 "lispindent",
13153#endif
13154#ifdef FEAT_LISTCMDS
13155 "listcmds",
13156#endif
13157#ifdef FEAT_LOCALMAP
13158 "localmap",
13159#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013160#ifdef FEAT_LUA
13161# ifndef DYNAMIC_LUA
13162 "lua",
13163# endif
13164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165#ifdef FEAT_MENU
13166 "menu",
13167#endif
13168#ifdef FEAT_SESSION
13169 "mksession",
13170#endif
13171#ifdef FEAT_MODIFY_FNAME
13172 "modify_fname",
13173#endif
13174#ifdef FEAT_MOUSE
13175 "mouse",
13176#endif
13177#ifdef FEAT_MOUSESHAPE
13178 "mouseshape",
13179#endif
13180#if defined(UNIX) || defined(VMS)
13181# ifdef FEAT_MOUSE_DEC
13182 "mouse_dec",
13183# endif
13184# ifdef FEAT_MOUSE_GPM
13185 "mouse_gpm",
13186# endif
13187# ifdef FEAT_MOUSE_JSB
13188 "mouse_jsbterm",
13189# endif
13190# ifdef FEAT_MOUSE_NET
13191 "mouse_netterm",
13192# endif
13193# ifdef FEAT_MOUSE_PTERM
13194 "mouse_pterm",
13195# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013196# ifdef FEAT_MOUSE_SGR
13197 "mouse_sgr",
13198# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013199# ifdef FEAT_SYSMOUSE
13200 "mouse_sysmouse",
13201# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013202# ifdef FEAT_MOUSE_URXVT
13203 "mouse_urxvt",
13204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205# ifdef FEAT_MOUSE_XTERM
13206 "mouse_xterm",
13207# endif
13208#endif
13209#ifdef FEAT_MBYTE
13210 "multi_byte",
13211#endif
13212#ifdef FEAT_MBYTE_IME
13213 "multi_byte_ime",
13214#endif
13215#ifdef FEAT_MULTI_LANG
13216 "multi_lang",
13217#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013218#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013219#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013220 "mzscheme",
13221#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223#ifdef FEAT_OLE
13224 "ole",
13225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226#ifdef FEAT_PATH_EXTRA
13227 "path_extra",
13228#endif
13229#ifdef FEAT_PERL
13230#ifndef DYNAMIC_PERL
13231 "perl",
13232#endif
13233#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013234#ifdef FEAT_PERSISTENT_UNDO
13235 "persistent_undo",
13236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237#ifdef FEAT_PYTHON
13238#ifndef DYNAMIC_PYTHON
13239 "python",
13240#endif
13241#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013242#ifdef FEAT_PYTHON3
13243#ifndef DYNAMIC_PYTHON3
13244 "python3",
13245#endif
13246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247#ifdef FEAT_POSTSCRIPT
13248 "postscript",
13249#endif
13250#ifdef FEAT_PRINTER
13251 "printer",
13252#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013253#ifdef FEAT_PROFILE
13254 "profile",
13255#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013256#ifdef FEAT_RELTIME
13257 "reltime",
13258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259#ifdef FEAT_QUICKFIX
13260 "quickfix",
13261#endif
13262#ifdef FEAT_RIGHTLEFT
13263 "rightleft",
13264#endif
13265#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13266 "ruby",
13267#endif
13268#ifdef FEAT_SCROLLBIND
13269 "scrollbind",
13270#endif
13271#ifdef FEAT_CMDL_INFO
13272 "showcmd",
13273 "cmdline_info",
13274#endif
13275#ifdef FEAT_SIGNS
13276 "signs",
13277#endif
13278#ifdef FEAT_SMARTINDENT
13279 "smartindent",
13280#endif
13281#ifdef FEAT_SNIFF
13282 "sniff",
13283#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013284#ifdef STARTUPTIME
13285 "startuptime",
13286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287#ifdef FEAT_STL_OPT
13288 "statusline",
13289#endif
13290#ifdef FEAT_SUN_WORKSHOP
13291 "sun_workshop",
13292#endif
13293#ifdef FEAT_NETBEANS_INTG
13294 "netbeans_intg",
13295#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013296#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013297 "spell",
13298#endif
13299#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 "syntax",
13301#endif
13302#if defined(USE_SYSTEM) || !defined(UNIX)
13303 "system",
13304#endif
13305#ifdef FEAT_TAG_BINS
13306 "tag_binary",
13307#endif
13308#ifdef FEAT_TAG_OLDSTATIC
13309 "tag_old_static",
13310#endif
13311#ifdef FEAT_TAG_ANYWHITE
13312 "tag_any_white",
13313#endif
13314#ifdef FEAT_TCL
13315# ifndef DYNAMIC_TCL
13316 "tcl",
13317# endif
13318#endif
13319#ifdef TERMINFO
13320 "terminfo",
13321#endif
13322#ifdef FEAT_TERMRESPONSE
13323 "termresponse",
13324#endif
13325#ifdef FEAT_TEXTOBJ
13326 "textobjects",
13327#endif
13328#ifdef HAVE_TGETENT
13329 "tgetent",
13330#endif
13331#ifdef FEAT_TITLE
13332 "title",
13333#endif
13334#ifdef FEAT_TOOLBAR
13335 "toolbar",
13336#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013337#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13338 "unnamedplus",
13339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340#ifdef FEAT_USR_CMDS
13341 "user-commands", /* was accidentally included in 5.4 */
13342 "user_commands",
13343#endif
13344#ifdef FEAT_VIMINFO
13345 "viminfo",
13346#endif
13347#ifdef FEAT_VERTSPLIT
13348 "vertsplit",
13349#endif
13350#ifdef FEAT_VIRTUALEDIT
13351 "virtualedit",
13352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354#ifdef FEAT_VISUALEXTRA
13355 "visualextra",
13356#endif
13357#ifdef FEAT_VREPLACE
13358 "vreplace",
13359#endif
13360#ifdef FEAT_WILDIGN
13361 "wildignore",
13362#endif
13363#ifdef FEAT_WILDMENU
13364 "wildmenu",
13365#endif
13366#ifdef FEAT_WINDOWS
13367 "windows",
13368#endif
13369#ifdef FEAT_WAK
13370 "winaltkeys",
13371#endif
13372#ifdef FEAT_WRITEBACKUP
13373 "writebackup",
13374#endif
13375#ifdef FEAT_XIM
13376 "xim",
13377#endif
13378#ifdef FEAT_XFONTSET
13379 "xfontset",
13380#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013381#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013382 "xpm",
13383 "xpm_w32", /* for backward compatibility */
13384#else
13385# if defined(HAVE_XPM)
13386 "xpm",
13387# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389#ifdef USE_XSMP
13390 "xsmp",
13391#endif
13392#ifdef USE_XSMP_INTERACT
13393 "xsmp_interact",
13394#endif
13395#ifdef FEAT_XCLIPBOARD
13396 "xterm_clipboard",
13397#endif
13398#ifdef FEAT_XTERM_SAVE
13399 "xterm_save",
13400#endif
13401#if defined(UNIX) && defined(FEAT_X11)
13402 "X11",
13403#endif
13404 NULL
13405 };
13406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 for (i = 0; has_list[i] != NULL; ++i)
13409 if (STRICMP(name, has_list[i]) == 0)
13410 {
13411 n = TRUE;
13412 break;
13413 }
13414
13415 if (n == FALSE)
13416 {
13417 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013418 {
13419 if (name[5] == '-'
13420 && STRLEN(name) > 11
13421 && vim_isdigit(name[6])
13422 && vim_isdigit(name[8])
13423 && vim_isdigit(name[10]))
13424 {
13425 int major = atoi((char *)name + 6);
13426 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013427
13428 /* Expect "patch-9.9.01234". */
13429 n = (major < VIM_VERSION_MAJOR
13430 || (major == VIM_VERSION_MAJOR
13431 && (minor < VIM_VERSION_MINOR
13432 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013433 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013434 }
13435 else
13436 n = has_patch(atoi((char *)name + 5));
13437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 else if (STRICMP(name, "vim_starting") == 0)
13439 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013440#ifdef FEAT_MBYTE
13441 else if (STRICMP(name, "multi_byte_encoding") == 0)
13442 n = has_mbyte;
13443#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013444#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13445 else if (STRICMP(name, "balloon_multiline") == 0)
13446 n = multiline_balloon_available();
13447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448#ifdef DYNAMIC_TCL
13449 else if (STRICMP(name, "tcl") == 0)
13450 n = tcl_enabled(FALSE);
13451#endif
13452#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13453 else if (STRICMP(name, "iconv") == 0)
13454 n = iconv_enabled(FALSE);
13455#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013456#ifdef DYNAMIC_LUA
13457 else if (STRICMP(name, "lua") == 0)
13458 n = lua_enabled(FALSE);
13459#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013460#ifdef DYNAMIC_MZSCHEME
13461 else if (STRICMP(name, "mzscheme") == 0)
13462 n = mzscheme_enabled(FALSE);
13463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464#ifdef DYNAMIC_RUBY
13465 else if (STRICMP(name, "ruby") == 0)
13466 n = ruby_enabled(FALSE);
13467#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013468#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469#ifdef DYNAMIC_PYTHON
13470 else if (STRICMP(name, "python") == 0)
13471 n = python_enabled(FALSE);
13472#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013473#endif
13474#ifdef FEAT_PYTHON3
13475#ifdef DYNAMIC_PYTHON3
13476 else if (STRICMP(name, "python3") == 0)
13477 n = python3_enabled(FALSE);
13478#endif
13479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480#ifdef DYNAMIC_PERL
13481 else if (STRICMP(name, "perl") == 0)
13482 n = perl_enabled(FALSE);
13483#endif
13484#ifdef FEAT_GUI
13485 else if (STRICMP(name, "gui_running") == 0)
13486 n = (gui.in_use || gui.starting);
13487# ifdef FEAT_GUI_W32
13488 else if (STRICMP(name, "gui_win32s") == 0)
13489 n = gui_is_win32s();
13490# endif
13491# ifdef FEAT_BROWSE
13492 else if (STRICMP(name, "browse") == 0)
13493 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13494# endif
13495#endif
13496#ifdef FEAT_SYN_HL
13497 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013498 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499#endif
13500#if defined(WIN3264)
13501 else if (STRICMP(name, "win95") == 0)
13502 n = mch_windows95();
13503#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013504#ifdef FEAT_NETBEANS_INTG
13505 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013506 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 }
13509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013510 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511}
13512
13513/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013514 * "has_key()" function
13515 */
13516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013517f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013518{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013519 if (argvars[0].v_type != VAR_DICT)
13520 {
13521 EMSG(_(e_dictreq));
13522 return;
13523 }
13524 if (argvars[0].vval.v_dict == NULL)
13525 return;
13526
13527 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013528 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013529}
13530
13531/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013532 * "haslocaldir()" function
13533 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013535f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013536{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013537 win_T *wp = NULL;
13538
13539 wp = find_tabwin(&argvars[0], &argvars[1]);
13540 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013541}
13542
13543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 * "hasmapto()" function
13545 */
13546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013547f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548{
13549 char_u *name;
13550 char_u *mode;
13551 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013552 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013554 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013555 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556 mode = (char_u *)"nvo";
13557 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013560 if (argvars[2].v_type != VAR_UNKNOWN)
13561 abbr = get_tv_number(&argvars[2]);
13562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563
Bram Moolenaar2c932302006-03-18 21:42:09 +000013564 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568}
13569
13570/*
13571 * "histadd()" function
13572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013574f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575{
13576#ifdef FEAT_CMDHIST
13577 int histype;
13578 char_u *str;
13579 char_u buf[NUMBUFLEN];
13580#endif
13581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 if (check_restricted() || check_secure())
13584 return;
13585#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13587 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 if (histype >= 0)
13589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 if (*str != NUL)
13592 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013593 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 return;
13597 }
13598 }
13599#endif
13600}
13601
13602/*
13603 * "histdel()" function
13604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013606f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607{
13608#ifdef FEAT_CMDHIST
13609 int n;
13610 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013611 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13614 if (str == NULL)
13615 n = 0;
13616 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013619 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 else
13624 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 get_tv_string_buf(&argvars[1], buf));
13627 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628#endif
13629}
13630
13631/*
13632 * "histget()" function
13633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013635f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636{
13637#ifdef FEAT_CMDHIST
13638 int type;
13639 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013640 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013642 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13643 if (str == NULL)
13644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013646 {
13647 type = get_histtype(str);
13648 if (argvars[1].v_type == VAR_UNKNOWN)
13649 idx = get_history_idx(type);
13650 else
13651 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13652 /* -1 on type error */
13653 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659}
13660
13661/*
13662 * "histnr()" function
13663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013665f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666{
13667 int i;
13668
13669#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013670 char_u *history = get_tv_string_chk(&argvars[0]);
13671
13672 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 if (i >= HIST_CMD && i < HIST_COUNT)
13674 i = get_history_idx(i);
13675 else
13676#endif
13677 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679}
13680
13681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 * "highlightID(name)" function
13683 */
13684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013685f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013687 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688}
13689
13690/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013691 * "highlight_exists()" function
13692 */
13693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013694f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013695{
13696 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13697}
13698
13699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 * "hostname()" function
13701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013703f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704{
13705 char_u hostname[256];
13706
13707 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708 rettv->v_type = VAR_STRING;
13709 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710}
13711
13712/*
13713 * iconv() function
13714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013716f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717{
13718#ifdef FEAT_MBYTE
13719 char_u buf1[NUMBUFLEN];
13720 char_u buf2[NUMBUFLEN];
13721 char_u *from, *to, *str;
13722 vimconv_T vimconv;
13723#endif
13724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013725 rettv->v_type = VAR_STRING;
13726 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727
13728#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 str = get_tv_string(&argvars[0]);
13730 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13731 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 vimconv.vc_type = CONV_NONE;
13733 convert_setup(&vimconv, from, to);
13734
13735 /* If the encodings are equal, no conversion needed. */
13736 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013737 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740
13741 convert_setup(&vimconv, NULL, NULL);
13742 vim_free(from);
13743 vim_free(to);
13744#endif
13745}
13746
13747/*
13748 * "indent()" function
13749 */
13750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013751f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752{
13753 linenr_T lnum;
13754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013757 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760}
13761
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013762/*
13763 * "index()" function
13764 */
13765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013766f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013767{
Bram Moolenaar33570922005-01-25 22:26:29 +000013768 list_T *l;
13769 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013770 long idx = 0;
13771 int ic = FALSE;
13772
13773 rettv->vval.v_number = -1;
13774 if (argvars[0].v_type != VAR_LIST)
13775 {
13776 EMSG(_(e_listreq));
13777 return;
13778 }
13779 l = argvars[0].vval.v_list;
13780 if (l != NULL)
13781 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013782 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013783 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013785 int error = FALSE;
13786
Bram Moolenaar758711c2005-02-02 23:11:38 +000013787 /* Start at specified item. Use the cached index that list_find()
13788 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013790 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013791 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013792 ic = get_tv_number_chk(&argvars[3], &error);
13793 if (error)
13794 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013795 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013796
Bram Moolenaar758711c2005-02-02 23:11:38 +000013797 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013798 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013799 {
13800 rettv->vval.v_number = idx;
13801 break;
13802 }
13803 }
13804}
13805
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806static int inputsecret_flag = 0;
13807
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013808static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013809
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013811 * This function is used by f_input() and f_inputdialog() functions. The third
13812 * argument to f_input() specifies the type of completion to use at the
13813 * prompt. The third argument to f_inputdialog() specifies the value to return
13814 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 */
13816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013817get_user_input(
13818 typval_T *argvars,
13819 typval_T *rettv,
13820 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 char_u *p = NULL;
13824 int c;
13825 char_u buf[NUMBUFLEN];
13826 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013828 int xp_type = EXPAND_NOTHING;
13829 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013831 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013832 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
13834#ifdef NO_CONSOLE_INPUT
13835 /* While starting up, there is no place to enter text. */
13836 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838#endif
13839
13840 cmd_silent = FALSE; /* Want to see the prompt. */
13841 if (prompt != NULL)
13842 {
13843 /* Only the part of the message after the last NL is considered as
13844 * prompt for the command line */
13845 p = vim_strrchr(prompt, '\n');
13846 if (p == NULL)
13847 p = prompt;
13848 else
13849 {
13850 ++p;
13851 c = *p;
13852 *p = NUL;
13853 msg_start();
13854 msg_clr_eos();
13855 msg_puts_attr(prompt, echo_attr);
13856 msg_didout = FALSE;
13857 msg_starthere();
13858 *p = c;
13859 }
13860 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 if (argvars[1].v_type != VAR_UNKNOWN)
13863 {
13864 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13865 if (defstr != NULL)
13866 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013868 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013869 {
13870 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013871 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013872 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013873
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013874 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013875 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013876
Bram Moolenaar4463f292005-09-25 22:20:24 +000013877 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13878 if (xp_name == NULL)
13879 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013880
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013881 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013882
Bram Moolenaar4463f292005-09-25 22:20:24 +000013883 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13884 &xp_arg) == FAIL)
13885 return;
13886 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013887 }
13888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013890 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013891 int save_ex_normal_busy = ex_normal_busy;
13892 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013893 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013894 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13895 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013896 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013897 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013898 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013899 && argvars[1].v_type != VAR_UNKNOWN
13900 && argvars[2].v_type != VAR_UNKNOWN)
13901 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13902 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013903
13904 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013906 /* since the user typed this, no need to wait for return */
13907 need_wait_return = FALSE;
13908 msg_didout = FALSE;
13909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 cmd_silent = cmd_silent_save;
13911}
13912
13913/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013914 * "input()" function
13915 * Also handles inputsecret() when inputsecret is set.
13916 */
13917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013918f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013919{
13920 get_user_input(argvars, rettv, FALSE);
13921}
13922
13923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924 * "inputdialog()" function
13925 */
13926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013927f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928{
13929#if defined(FEAT_GUI_TEXTDIALOG)
13930 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13931 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13932 {
13933 char_u *message;
13934 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013935 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013937 message = get_tv_string_chk(&argvars[0]);
13938 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013939 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013940 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 else
13942 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013943 if (message != NULL && defstr != NULL
13944 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013945 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013946 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 else
13948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 if (message != NULL && defstr != NULL
13950 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013951 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013952 rettv->vval.v_string = vim_strsave(
13953 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 }
13959 else
13960#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013961 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962}
13963
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013964/*
13965 * "inputlist()" function
13966 */
13967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013968f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013969{
13970 listitem_T *li;
13971 int selected;
13972 int mouse_used;
13973
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013974#ifdef NO_CONSOLE_INPUT
13975 /* While starting up, there is no place to enter text. */
13976 if (no_console_input())
13977 return;
13978#endif
13979 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13980 {
13981 EMSG2(_(e_listarg), "inputlist()");
13982 return;
13983 }
13984
13985 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013986 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013987 lines_left = Rows; /* avoid more prompt */
13988 msg_scroll = TRUE;
13989 msg_clr_eos();
13990
13991 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13992 {
13993 msg_puts(get_tv_string(&li->li_tv));
13994 msg_putchar('\n');
13995 }
13996
13997 /* Ask for choice. */
13998 selected = prompt_for_number(&mouse_used);
13999 if (mouse_used)
14000 selected -= lines_left;
14001
14002 rettv->vval.v_number = selected;
14003}
14004
14005
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14007
14008/*
14009 * "inputrestore()" function
14010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014012f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013{
14014 if (ga_userinput.ga_len > 0)
14015 {
14016 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14018 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014019 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 }
14021 else if (p_verbose > 1)
14022 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014023 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014024 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 }
14026}
14027
14028/*
14029 * "inputsave()" function
14030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014032f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014034 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 if (ga_grow(&ga_userinput, 1) == OK)
14036 {
14037 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14038 + ga_userinput.ga_len);
14039 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014040 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 }
14042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044}
14045
14046/*
14047 * "inputsecret()" function
14048 */
14049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014050f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051{
14052 ++cmdline_star;
14053 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014054 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 --cmdline_star;
14056 --inputsecret_flag;
14057}
14058
14059/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014060 * "insert()" function
14061 */
14062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014063f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014064{
14065 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014066 listitem_T *item;
14067 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014068 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014069
14070 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014072 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014073 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014074 {
14075 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 before = get_tv_number_chk(&argvars[2], &error);
14077 if (error)
14078 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014079
Bram Moolenaar758711c2005-02-02 23:11:38 +000014080 if (before == l->lv_len)
14081 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014082 else
14083 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014084 item = list_find(l, before);
14085 if (item == NULL)
14086 {
14087 EMSGN(_(e_listidx), before);
14088 l = NULL;
14089 }
14090 }
14091 if (l != NULL)
14092 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014093 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014094 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014095 }
14096 }
14097}
14098
14099/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014100 * "invert(expr)" function
14101 */
14102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014103f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014104{
14105 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14106}
14107
14108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 * "isdirectory()" function
14110 */
14111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014112f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014114 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115}
14116
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014117/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014118 * "islocked()" function
14119 */
14120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014121f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014122{
14123 lval_T lv;
14124 char_u *end;
14125 dictitem_T *di;
14126
14127 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014128 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14129 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014130 if (end != NULL && lv.ll_name != NULL)
14131 {
14132 if (*end != NUL)
14133 EMSG(_(e_trailing));
14134 else
14135 {
14136 if (lv.ll_tv == NULL)
14137 {
14138 if (check_changedtick(lv.ll_name))
14139 rettv->vval.v_number = 1; /* always locked */
14140 else
14141 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014142 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014143 if (di != NULL)
14144 {
14145 /* Consider a variable locked when:
14146 * 1. the variable itself is locked
14147 * 2. the value of the variable is locked.
14148 * 3. the List or Dict value is locked.
14149 */
14150 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14151 || tv_islocked(&di->di_tv));
14152 }
14153 }
14154 }
14155 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014156 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014157 else if (lv.ll_newkey != NULL)
14158 EMSG2(_(e_dictkey), lv.ll_newkey);
14159 else if (lv.ll_list != NULL)
14160 /* List item. */
14161 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14162 else
14163 /* Dictionary item. */
14164 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14165 }
14166 }
14167
14168 clear_lval(&lv);
14169}
14170
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014171static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014172
14173/*
14174 * Turn a dict into a list:
14175 * "what" == 0: list of keys
14176 * "what" == 1: list of values
14177 * "what" == 2: list of items
14178 */
14179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014180dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014181{
Bram Moolenaar33570922005-01-25 22:26:29 +000014182 list_T *l2;
14183 dictitem_T *di;
14184 hashitem_T *hi;
14185 listitem_T *li;
14186 listitem_T *li2;
14187 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014188 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014189
Bram Moolenaar8c711452005-01-14 21:53:12 +000014190 if (argvars[0].v_type != VAR_DICT)
14191 {
14192 EMSG(_(e_dictreq));
14193 return;
14194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014195 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014196 return;
14197
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014198 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014199 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014200
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014201 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014202 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014203 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014204 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014205 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014206 --todo;
14207 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014208
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014209 li = listitem_alloc();
14210 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014211 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014212 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014213
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014214 if (what == 0)
14215 {
14216 /* keys() */
14217 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014218 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014219 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14220 }
14221 else if (what == 1)
14222 {
14223 /* values() */
14224 copy_tv(&di->di_tv, &li->li_tv);
14225 }
14226 else
14227 {
14228 /* items() */
14229 l2 = list_alloc();
14230 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014231 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014232 li->li_tv.vval.v_list = l2;
14233 if (l2 == NULL)
14234 break;
14235 ++l2->lv_refcount;
14236
14237 li2 = listitem_alloc();
14238 if (li2 == NULL)
14239 break;
14240 list_append(l2, li2);
14241 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014242 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014243 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14244
14245 li2 = listitem_alloc();
14246 if (li2 == NULL)
14247 break;
14248 list_append(l2, li2);
14249 copy_tv(&di->di_tv, &li2->li_tv);
14250 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014251 }
14252 }
14253}
14254
14255/*
14256 * "items(dict)" function
14257 */
14258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014259f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014260{
14261 dict_list(argvars, rettv, 2);
14262}
14263
Bram Moolenaar835dc632016-02-07 14:27:38 +010014264#ifdef FEAT_JOB
14265/*
14266 * "job_start()" function
14267 */
14268 static void
14269f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14270{
14271 job_T *job;
14272 char_u *cmd = NULL;
14273#if defined(UNIX)
14274# define USE_ARGV
14275 char **argv = NULL;
14276 int argc = 0;
14277#else
14278 garray_T ga;
14279#endif
14280
14281 rettv->v_type = VAR_JOB;
14282 job = job_alloc();
14283 rettv->vval.v_job = job;
14284 if (job == NULL)
14285 return;
14286
14287 rettv->vval.v_job->jv_status = JOB_FAILED;
14288#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014289 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014290#endif
14291
14292 if (argvars[0].v_type == VAR_STRING)
14293 {
14294 /* Command is a string. */
14295 cmd = argvars[0].vval.v_string;
14296#ifdef USE_ARGV
14297 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14298 return;
14299 argv[argc] = NULL;
14300#endif
14301 }
14302 else if (argvars[0].v_type != VAR_LIST
14303 || argvars[0].vval.v_list == NULL
14304 || argvars[0].vval.v_list->lv_len < 1)
14305 {
14306 EMSG(_(e_invarg));
14307 return;
14308 }
14309 else
14310 {
14311 list_T *l = argvars[0].vval.v_list;
14312 listitem_T *li;
14313 char_u *s;
14314
14315#ifdef USE_ARGV
14316 /* Pass argv[] to mch_call_shell(). */
14317 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14318 if (argv == NULL)
14319 return;
14320#endif
14321 for (li = l->lv_first; li != NULL; li = li->li_next)
14322 {
14323 s = get_tv_string_chk(&li->li_tv);
14324 if (s == NULL)
14325 goto theend;
14326#ifdef USE_ARGV
14327 argv[argc++] = (char *)s;
14328#else
14329 if (li != l->lv_first)
14330 {
14331 s = vim_strsave_shellescape(s, FALSE, TRUE);
14332 if (s == NULL)
14333 goto theend;
14334 }
14335 ga_concat(&ga, s);
14336 vim_free(s);
14337 if (li->li_next != NULL)
14338 ga_append(&ga, ' ');
14339#endif
14340 }
14341#ifdef USE_ARGV
14342 argv[argc] = NULL;
14343#else
14344 cmd = ga.ga_data;
14345#endif
14346 }
14347#ifdef USE_ARGV
14348 mch_start_job(argv, job);
14349#else
14350 mch_start_job(cmd, job);
14351#endif
14352
14353theend:
14354#ifdef USE_ARGV
14355 if (argv != NULL)
14356 vim_free(argv);
14357#else
14358 vim_free(ga.ga_data);
14359#endif
14360}
14361
14362/*
14363 * "job_status()" function
14364 */
14365 static void
14366f_job_status(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14367{
14368 char *result;
14369
14370 if (argvars[0].v_type != VAR_JOB)
14371 EMSG(_(e_invarg));
14372 else
14373 {
14374 job_T *job = argvars[0].vval.v_job;
14375
14376 if (job->jv_status == JOB_ENDED)
14377 /* No need to check, dead is dead. */
14378 result = "dead";
14379 else if (job->jv_status == JOB_FAILED)
14380 result = "fail";
14381 else
14382 result = mch_job_status(job);
14383 rettv->v_type = VAR_STRING;
14384 rettv->vval.v_string = vim_strsave((char_u *)result);
14385 }
14386}
14387
14388/*
14389 * "job_stop()" function
14390 */
14391 static void
14392f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14393{
14394 if (argvars[0].v_type != VAR_JOB)
14395 EMSG(_(e_invarg));
14396 else
14397 {
14398 char_u *arg;
14399
14400 if (argvars[1].v_type == VAR_UNKNOWN)
14401 arg = (char_u *)"";
14402 else
14403 {
14404 arg = get_tv_string_chk(&argvars[1]);
14405 if (arg == NULL)
14406 {
14407 EMSG(_(e_invarg));
14408 return;
14409 }
14410 }
14411 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14412 rettv->vval.v_number = 0;
14413 else
14414 rettv->vval.v_number = 1;
14415 }
14416}
14417#endif
14418
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014420 * "join()" function
14421 */
14422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014423f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014424{
14425 garray_T ga;
14426 char_u *sep;
14427
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014428 if (argvars[0].v_type != VAR_LIST)
14429 {
14430 EMSG(_(e_listreq));
14431 return;
14432 }
14433 if (argvars[0].vval.v_list == NULL)
14434 return;
14435 if (argvars[1].v_type == VAR_UNKNOWN)
14436 sep = (char_u *)" ";
14437 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014438 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014439
14440 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441
14442 if (sep != NULL)
14443 {
14444 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014445 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 ga_append(&ga, NUL);
14447 rettv->vval.v_string = (char_u *)ga.ga_data;
14448 }
14449 else
14450 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014451}
14452
14453/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014454 * "jsdecode()" function
14455 */
14456 static void
14457f_jsdecode(typval_T *argvars, typval_T *rettv)
14458{
14459 js_read_T reader;
14460
14461 reader.js_buf = get_tv_string(&argvars[0]);
14462 reader.js_fill = NULL;
14463 reader.js_used = 0;
14464 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14465 EMSG(_(e_invarg));
14466}
14467
14468/*
14469 * "jsencode()" function
14470 */
14471 static void
14472f_jsencode(typval_T *argvars, typval_T *rettv)
14473{
14474 rettv->v_type = VAR_STRING;
14475 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14476}
14477
14478/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014479 * "jsondecode()" function
14480 */
14481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014482f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014483{
14484 js_read_T reader;
14485
14486 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014487 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014488 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014489 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014490 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014491}
14492
14493/*
14494 * "jsonencode()" function
14495 */
14496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014497f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014498{
14499 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014500 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014501}
14502
14503/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014504 * "keys()" function
14505 */
14506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014507f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014508{
14509 dict_list(argvars, rettv, 0);
14510}
14511
14512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 * "last_buffer_nr()" function.
14514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014516f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517{
14518 int n = 0;
14519 buf_T *buf;
14520
14521 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14522 if (n < buf->b_fnum)
14523 n = buf->b_fnum;
14524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014525 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014526}
14527
14528/*
14529 * "len()" function
14530 */
14531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014532f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014533{
14534 switch (argvars[0].v_type)
14535 {
14536 case VAR_STRING:
14537 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014538 rettv->vval.v_number = (varnumber_T)STRLEN(
14539 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014540 break;
14541 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014543 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014544 case VAR_DICT:
14545 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14546 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014547 case VAR_UNKNOWN:
14548 case VAR_SPECIAL:
14549 case VAR_FLOAT:
14550 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014551 case VAR_JOB:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014552 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014553 break;
14554 }
14555}
14556
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014557static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014558
14559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014560libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014561{
14562#ifdef FEAT_LIBCALL
14563 char_u *string_in;
14564 char_u **string_result;
14565 int nr_result;
14566#endif
14567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014568 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014569 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014570 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014571
14572 if (check_restricted() || check_secure())
14573 return;
14574
14575#ifdef FEAT_LIBCALL
14576 /* The first two args must be strings, otherwise its meaningless */
14577 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14578 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014579 string_in = NULL;
14580 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014581 string_in = argvars[2].vval.v_string;
14582 if (type == VAR_NUMBER)
14583 string_result = NULL;
14584 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014586 if (mch_libcall(argvars[0].vval.v_string,
14587 argvars[1].vval.v_string,
14588 string_in,
14589 argvars[2].vval.v_number,
14590 string_result,
14591 &nr_result) == OK
14592 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014593 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014594 }
14595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596}
14597
14598/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599 * "libcall()" function
14600 */
14601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014602f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603{
14604 libcall_common(argvars, rettv, VAR_STRING);
14605}
14606
14607/*
14608 * "libcallnr()" function
14609 */
14610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014611f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014612{
14613 libcall_common(argvars, rettv, VAR_NUMBER);
14614}
14615
14616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 * "line(string)" function
14618 */
14619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014620f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621{
14622 linenr_T lnum = 0;
14623 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014624 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014626 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 if (fp != NULL)
14628 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014629 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630}
14631
14632/*
14633 * "line2byte(lnum)" function
14634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014636f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637{
14638#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640#else
14641 linenr_T lnum;
14642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014645 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14648 if (rettv->vval.v_number >= 0)
14649 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650#endif
14651}
14652
14653/*
14654 * "lispindent(lnum)" function
14655 */
14656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014657f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658{
14659#ifdef FEAT_LISP
14660 pos_T pos;
14661 linenr_T lnum;
14662
14663 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014664 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14666 {
14667 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014668 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 curwin->w_cursor = pos;
14670 }
14671 else
14672#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674}
14675
14676/*
14677 * "localtime()" function
14678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014680f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683}
14684
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014685static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686
14687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014688get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689{
14690 char_u *keys;
14691 char_u *which;
14692 char_u buf[NUMBUFLEN];
14693 char_u *keys_buf = NULL;
14694 char_u *rhs;
14695 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014696 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014697 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014698 mapblock_T *mp;
14699 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700
14701 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014702 rettv->v_type = VAR_STRING;
14703 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014705 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 if (*keys == NUL)
14707 return;
14708
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014711 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014712 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014713 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014714 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014715 if (argvars[3].v_type != VAR_UNKNOWN)
14716 get_dict = get_tv_number(&argvars[3]);
14717 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 else
14720 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 if (which == NULL)
14722 return;
14723
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 mode = get_map_mode(&which, 0);
14725
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014726 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014727 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014729
14730 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014732 /* Return a string. */
14733 if (rhs != NULL)
14734 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735
Bram Moolenaarbd743252010-10-20 21:23:33 +020014736 }
14737 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14738 {
14739 /* Return a dictionary. */
14740 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14741 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14742 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743
Bram Moolenaarbd743252010-10-20 21:23:33 +020014744 dict_add_nr_str(dict, "lhs", 0L, lhs);
14745 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14746 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14747 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14748 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14749 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14750 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014751 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014752 dict_add_nr_str(dict, "mode", 0L, mapmode);
14753
14754 vim_free(lhs);
14755 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 }
14757}
14758
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014759#ifdef FEAT_FLOAT
14760/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014761 * "log()" function
14762 */
14763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014764f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014765{
14766 float_T f;
14767
14768 rettv->v_type = VAR_FLOAT;
14769 if (get_float_arg(argvars, &f) == OK)
14770 rettv->vval.v_float = log(f);
14771 else
14772 rettv->vval.v_float = 0.0;
14773}
14774
14775/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014776 * "log10()" function
14777 */
14778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014779f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014780{
14781 float_T f;
14782
14783 rettv->v_type = VAR_FLOAT;
14784 if (get_float_arg(argvars, &f) == OK)
14785 rettv->vval.v_float = log10(f);
14786 else
14787 rettv->vval.v_float = 0.0;
14788}
14789#endif
14790
Bram Moolenaar1dced572012-04-05 16:54:08 +020014791#ifdef FEAT_LUA
14792/*
14793 * "luaeval()" function
14794 */
14795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014796f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014797{
14798 char_u *str;
14799 char_u buf[NUMBUFLEN];
14800
14801 str = get_tv_string_buf(&argvars[0], buf);
14802 do_luaeval(str, argvars + 1, rettv);
14803}
14804#endif
14805
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014807 * "map()" function
14808 */
14809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014810f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014811{
14812 filter_map(argvars, rettv, TRUE);
14813}
14814
14815/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014816 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 */
14818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014819f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014821 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822}
14823
14824/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014825 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 */
14827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014828f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014830 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831}
14832
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014833static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834
14835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014836find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014838 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014839 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014840 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 char_u *pat;
14842 regmatch_T regmatch;
14843 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014844 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 char_u *save_cpo;
14846 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014847 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014848 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014849 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014850 list_T *l = NULL;
14851 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014852 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014853 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854
14855 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14856 save_cpo = p_cpo;
14857 p_cpo = (char_u *)"";
14858
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014859 rettv->vval.v_number = -1;
14860 if (type == 3)
14861 {
14862 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014863 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014864 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014865 }
14866 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868 rettv->v_type = VAR_STRING;
14869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014872 if (argvars[0].v_type == VAR_LIST)
14873 {
14874 if ((l = argvars[0].vval.v_list) == NULL)
14875 goto theend;
14876 li = l->lv_first;
14877 }
14878 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014879 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014880 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014881 len = (long)STRLEN(str);
14882 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014884 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14885 if (pat == NULL)
14886 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014887
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014888 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014890 int error = FALSE;
14891
14892 start = get_tv_number_chk(&argvars[2], &error);
14893 if (error)
14894 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014895 if (l != NULL)
14896 {
14897 li = list_find(l, start);
14898 if (li == NULL)
14899 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014900 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014901 }
14902 else
14903 {
14904 if (start < 0)
14905 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014906 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014907 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014908 /* When "count" argument is there ignore matches before "start",
14909 * otherwise skip part of the string. Differs when pattern is "^"
14910 * or "\<". */
14911 if (argvars[3].v_type != VAR_UNKNOWN)
14912 startcol = start;
14913 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014914 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014915 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014916 len -= start;
14917 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014918 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014919
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014920 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014921 nth = get_tv_number_chk(&argvars[3], &error);
14922 if (error)
14923 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924 }
14925
14926 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14927 if (regmatch.regprog != NULL)
14928 {
14929 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014930
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014931 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014932 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014933 if (l != NULL)
14934 {
14935 if (li == NULL)
14936 {
14937 match = FALSE;
14938 break;
14939 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014940 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014941 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014942 if (str == NULL)
14943 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014944 }
14945
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014946 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014947
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014948 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014949 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014950 if (l == NULL && !match)
14951 break;
14952
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014953 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014954 if (l != NULL)
14955 {
14956 li = li->li_next;
14957 ++idx;
14958 }
14959 else
14960 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014961#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014962 startcol = (colnr_T)(regmatch.startp[0]
14963 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014964#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014965 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014966#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014967 if (startcol > (colnr_T)len
14968 || str + startcol <= regmatch.startp[0])
14969 {
14970 match = FALSE;
14971 break;
14972 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014973 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014974 }
14975
14976 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014978 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014979 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014980 int i;
14981
14982 /* return list with matched string and submatches */
14983 for (i = 0; i < NSUBEXP; ++i)
14984 {
14985 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014986 {
14987 if (list_append_string(rettv->vval.v_list,
14988 (char_u *)"", 0) == FAIL)
14989 break;
14990 }
14991 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014992 regmatch.startp[i],
14993 (int)(regmatch.endp[i] - regmatch.startp[i]))
14994 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014995 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014996 }
14997 }
14998 else if (type == 2)
14999 {
15000 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015001 if (l != NULL)
15002 copy_tv(&li->li_tv, rettv);
15003 else
15004 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015006 }
15007 else if (l != NULL)
15008 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 else
15010 {
15011 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 (varnumber_T)(regmatch.startp[0] - str);
15014 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015017 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 }
15019 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015020 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 }
15022
15023theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015024 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 p_cpo = save_cpo;
15026}
15027
15028/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015029 * "match()" function
15030 */
15031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015032f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015033{
15034 find_some_match(argvars, rettv, 1);
15035}
15036
15037/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015038 * "matchadd()" function
15039 */
15040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015041f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015042{
15043#ifdef FEAT_SEARCH_EXTRA
15044 char_u buf[NUMBUFLEN];
15045 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15046 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15047 int prio = 10; /* default priority */
15048 int id = -1;
15049 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015050 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015051
15052 rettv->vval.v_number = -1;
15053
15054 if (grp == NULL || pat == NULL)
15055 return;
15056 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015057 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015058 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015059 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015060 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015061 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015062 if (argvars[4].v_type != VAR_UNKNOWN)
15063 {
15064 if (argvars[4].v_type != VAR_DICT)
15065 {
15066 EMSG(_(e_dictreq));
15067 return;
15068 }
15069 if (dict_find(argvars[4].vval.v_dict,
15070 (char_u *)"conceal", -1) != NULL)
15071 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15072 (char_u *)"conceal", FALSE);
15073 }
15074 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015075 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015076 if (error == TRUE)
15077 return;
15078 if (id >= 1 && id <= 3)
15079 {
15080 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15081 return;
15082 }
15083
Bram Moolenaar6561d522015-07-21 15:48:27 +020015084 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15085 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015086#endif
15087}
15088
15089/*
15090 * "matchaddpos()" function
15091 */
15092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015093f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015094{
15095#ifdef FEAT_SEARCH_EXTRA
15096 char_u buf[NUMBUFLEN];
15097 char_u *group;
15098 int prio = 10;
15099 int id = -1;
15100 int error = FALSE;
15101 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015102 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015103
15104 rettv->vval.v_number = -1;
15105
15106 group = get_tv_string_buf_chk(&argvars[0], buf);
15107 if (group == NULL)
15108 return;
15109
15110 if (argvars[1].v_type != VAR_LIST)
15111 {
15112 EMSG2(_(e_listarg), "matchaddpos()");
15113 return;
15114 }
15115 l = argvars[1].vval.v_list;
15116 if (l == NULL)
15117 return;
15118
15119 if (argvars[2].v_type != VAR_UNKNOWN)
15120 {
15121 prio = get_tv_number_chk(&argvars[2], &error);
15122 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015123 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015124 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015125 if (argvars[4].v_type != VAR_UNKNOWN)
15126 {
15127 if (argvars[4].v_type != VAR_DICT)
15128 {
15129 EMSG(_(e_dictreq));
15130 return;
15131 }
15132 if (dict_find(argvars[4].vval.v_dict,
15133 (char_u *)"conceal", -1) != NULL)
15134 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15135 (char_u *)"conceal", FALSE);
15136 }
15137 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015138 }
15139 if (error == TRUE)
15140 return;
15141
15142 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15143 if (id == 1 || id == 2)
15144 {
15145 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15146 return;
15147 }
15148
Bram Moolenaar6561d522015-07-21 15:48:27 +020015149 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15150 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015151#endif
15152}
15153
15154/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015155 * "matcharg()" function
15156 */
15157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015158f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015159{
15160 if (rettv_list_alloc(rettv) == OK)
15161 {
15162#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015163 int id = get_tv_number(&argvars[0]);
15164 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015165
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015166 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015167 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015168 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15169 {
15170 list_append_string(rettv->vval.v_list,
15171 syn_id2name(m->hlg_id), -1);
15172 list_append_string(rettv->vval.v_list, m->pattern, -1);
15173 }
15174 else
15175 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015176 list_append_string(rettv->vval.v_list, NULL, -1);
15177 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015178 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015179 }
15180#endif
15181 }
15182}
15183
15184/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015185 * "matchdelete()" function
15186 */
15187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015188f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015189{
15190#ifdef FEAT_SEARCH_EXTRA
15191 rettv->vval.v_number = match_delete(curwin,
15192 (int)get_tv_number(&argvars[0]), TRUE);
15193#endif
15194}
15195
15196/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015197 * "matchend()" function
15198 */
15199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015200f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201{
15202 find_some_match(argvars, rettv, 0);
15203}
15204
15205/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015206 * "matchlist()" function
15207 */
15208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015209f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015210{
15211 find_some_match(argvars, rettv, 3);
15212}
15213
15214/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015215 * "matchstr()" function
15216 */
15217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015218f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219{
15220 find_some_match(argvars, rettv, 2);
15221}
15222
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015223static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015224
15225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015226max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015227{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015228 long n = 0;
15229 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015231
15232 if (argvars[0].v_type == VAR_LIST)
15233 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 list_T *l;
15235 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015236
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015237 l = argvars[0].vval.v_list;
15238 if (l != NULL)
15239 {
15240 li = l->lv_first;
15241 if (li != NULL)
15242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015243 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015244 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015245 {
15246 li = li->li_next;
15247 if (li == NULL)
15248 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015250 if (domax ? i > n : i < n)
15251 n = i;
15252 }
15253 }
15254 }
15255 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015256 else if (argvars[0].v_type == VAR_DICT)
15257 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015258 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015259 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015261 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015262
15263 d = argvars[0].vval.v_dict;
15264 if (d != NULL)
15265 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015266 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015267 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015268 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015269 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015271 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015272 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015273 if (first)
15274 {
15275 n = i;
15276 first = FALSE;
15277 }
15278 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015279 n = i;
15280 }
15281 }
15282 }
15283 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015284 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015285 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015287}
15288
15289/*
15290 * "max()" function
15291 */
15292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015293f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015294{
15295 max_min(argvars, rettv, TRUE);
15296}
15297
15298/*
15299 * "min()" function
15300 */
15301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015302f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015303{
15304 max_min(argvars, rettv, FALSE);
15305}
15306
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015307static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015308
15309/*
15310 * Create the directory in which "dir" is located, and higher levels when
15311 * needed.
15312 */
15313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015314mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015315{
15316 char_u *p;
15317 char_u *updir;
15318 int r = FAIL;
15319
15320 /* Get end of directory name in "dir".
15321 * We're done when it's "/" or "c:/". */
15322 p = gettail_sep(dir);
15323 if (p <= get_past_head(dir))
15324 return OK;
15325
15326 /* If the directory exists we're done. Otherwise: create it.*/
15327 updir = vim_strnsave(dir, (int)(p - dir));
15328 if (updir == NULL)
15329 return FAIL;
15330 if (mch_isdir(updir))
15331 r = OK;
15332 else if (mkdir_recurse(updir, prot) == OK)
15333 r = vim_mkdir_emsg(updir, prot);
15334 vim_free(updir);
15335 return r;
15336}
15337
15338#ifdef vim_mkdir
15339/*
15340 * "mkdir()" function
15341 */
15342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015343f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015344{
15345 char_u *dir;
15346 char_u buf[NUMBUFLEN];
15347 int prot = 0755;
15348
15349 rettv->vval.v_number = FAIL;
15350 if (check_restricted() || check_secure())
15351 return;
15352
15353 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015354 if (*dir == NUL)
15355 rettv->vval.v_number = FAIL;
15356 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015357 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015358 if (*gettail(dir) == NUL)
15359 /* remove trailing slashes */
15360 *gettail_sep(dir) = NUL;
15361
15362 if (argvars[1].v_type != VAR_UNKNOWN)
15363 {
15364 if (argvars[2].v_type != VAR_UNKNOWN)
15365 prot = get_tv_number_chk(&argvars[2], NULL);
15366 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15367 mkdir_recurse(dir, prot);
15368 }
15369 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015370 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015371}
15372#endif
15373
Bram Moolenaar0d660222005-01-07 21:51:51 +000015374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 * "mode()" function
15376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015378f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015380 char_u buf[3];
15381
15382 buf[1] = NUL;
15383 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 if (VIsual_active)
15386 {
15387 if (VIsual_select)
15388 buf[0] = VIsual_mode + 's' - 'v';
15389 else
15390 buf[0] = VIsual_mode;
15391 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015392 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015393 || State == CONFIRM)
15394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015396 if (State == ASKMORE)
15397 buf[1] = 'm';
15398 else if (State == CONFIRM)
15399 buf[1] = '?';
15400 }
15401 else if (State == EXTERNCMD)
15402 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 else if (State & INSERT)
15404 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015405#ifdef FEAT_VREPLACE
15406 if (State & VREPLACE_FLAG)
15407 {
15408 buf[0] = 'R';
15409 buf[1] = 'v';
15410 }
15411 else
15412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 if (State & REPLACE_FLAG)
15414 buf[0] = 'R';
15415 else
15416 buf[0] = 'i';
15417 }
15418 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015421 if (exmode_active)
15422 buf[1] = 'v';
15423 }
15424 else if (exmode_active)
15425 {
15426 buf[0] = 'c';
15427 buf[1] = 'e';
15428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015432 if (finish_op)
15433 buf[1] = 'o';
15434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015436 /* Clear out the minor mode when the argument is not a non-zero number or
15437 * non-empty string. */
15438 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015439 buf[1] = NUL;
15440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 rettv->vval.v_string = vim_strsave(buf);
15442 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443}
15444
Bram Moolenaar429fa852013-04-15 12:27:36 +020015445#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015446/*
15447 * "mzeval()" function
15448 */
15449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015450f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015451{
15452 char_u *str;
15453 char_u buf[NUMBUFLEN];
15454
15455 str = get_tv_string_buf(&argvars[0], buf);
15456 do_mzeval(str, rettv);
15457}
Bram Moolenaar75676462013-01-30 14:55:42 +010015458
15459 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015460mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015461{
15462 typval_T argvars[3];
15463
15464 argvars[0].v_type = VAR_STRING;
15465 argvars[0].vval.v_string = name;
15466 copy_tv(args, &argvars[1]);
15467 argvars[2].v_type = VAR_UNKNOWN;
15468 f_call(argvars, rettv);
15469 clear_tv(&argvars[1]);
15470}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015471#endif
15472
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015474 * "nextnonblank()" function
15475 */
15476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015477f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015478{
15479 linenr_T lnum;
15480
15481 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015483 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015484 {
15485 lnum = 0;
15486 break;
15487 }
15488 if (*skipwhite(ml_get(lnum)) != NUL)
15489 break;
15490 }
15491 rettv->vval.v_number = lnum;
15492}
15493
15494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 * "nr2char()" function
15496 */
15497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015498f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499{
15500 char_u buf[NUMBUFLEN];
15501
15502#ifdef FEAT_MBYTE
15503 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015504 {
15505 int utf8 = 0;
15506
15507 if (argvars[1].v_type != VAR_UNKNOWN)
15508 utf8 = get_tv_number_chk(&argvars[1], NULL);
15509 if (utf8)
15510 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15511 else
15512 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 else
15515#endif
15516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015517 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518 buf[1] = NUL;
15519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 rettv->v_type = VAR_STRING;
15521 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015522}
15523
15524/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015525 * "or(expr, expr)" function
15526 */
15527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015528f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015529{
15530 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15531 | get_tv_number_chk(&argvars[1], NULL);
15532}
15533
15534/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015535 * "pathshorten()" function
15536 */
15537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015538f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015539{
15540 char_u *p;
15541
15542 rettv->v_type = VAR_STRING;
15543 p = get_tv_string_chk(&argvars[0]);
15544 if (p == NULL)
15545 rettv->vval.v_string = NULL;
15546 else
15547 {
15548 p = vim_strsave(p);
15549 rettv->vval.v_string = p;
15550 if (p != NULL)
15551 shorten_dir(p);
15552 }
15553}
15554
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015555#ifdef FEAT_PERL
15556/*
15557 * "perleval()" function
15558 */
15559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015560f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015561{
15562 char_u *str;
15563 char_u buf[NUMBUFLEN];
15564
15565 str = get_tv_string_buf(&argvars[0], buf);
15566 do_perleval(str, rettv);
15567}
15568#endif
15569
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015570#ifdef FEAT_FLOAT
15571/*
15572 * "pow()" function
15573 */
15574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015575f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015576{
15577 float_T fx, fy;
15578
15579 rettv->v_type = VAR_FLOAT;
15580 if (get_float_arg(argvars, &fx) == OK
15581 && get_float_arg(&argvars[1], &fy) == OK)
15582 rettv->vval.v_float = pow(fx, fy);
15583 else
15584 rettv->vval.v_float = 0.0;
15585}
15586#endif
15587
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015589 * "prevnonblank()" function
15590 */
15591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015592f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593{
15594 linenr_T lnum;
15595
15596 lnum = get_tv_lnum(argvars);
15597 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15598 lnum = 0;
15599 else
15600 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15601 --lnum;
15602 rettv->vval.v_number = lnum;
15603}
15604
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015605/* This dummy va_list is here because:
15606 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15607 * - locally in the function results in a "used before set" warning
15608 * - using va_start() to initialize it gives "function with fixed args" error */
15609static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015610
Bram Moolenaar8c711452005-01-14 21:53:12 +000015611/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015612 * "printf()" function
15613 */
15614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015615f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015616{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015617 char_u buf[NUMBUFLEN];
15618 int len;
15619 char_u *s;
15620 int saved_did_emsg = did_emsg;
15621 char *fmt;
15622
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015623 rettv->v_type = VAR_STRING;
15624 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015625
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015626 /* Get the required length, allocate the buffer and do it for real. */
15627 did_emsg = FALSE;
15628 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15629 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15630 if (!did_emsg)
15631 {
15632 s = alloc(len + 1);
15633 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015634 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015635 rettv->vval.v_string = s;
15636 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015637 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015638 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015639 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015640}
15641
15642/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015643 * "pumvisible()" function
15644 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015646f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015647{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015648#ifdef FEAT_INS_EXPAND
15649 if (pum_visible())
15650 rettv->vval.v_number = 1;
15651#endif
15652}
15653
Bram Moolenaardb913952012-06-29 12:54:53 +020015654#ifdef FEAT_PYTHON3
15655/*
15656 * "py3eval()" function
15657 */
15658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015659f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015660{
15661 char_u *str;
15662 char_u buf[NUMBUFLEN];
15663
15664 str = get_tv_string_buf(&argvars[0], buf);
15665 do_py3eval(str, rettv);
15666}
15667#endif
15668
15669#ifdef FEAT_PYTHON
15670/*
15671 * "pyeval()" function
15672 */
15673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015674f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015675{
15676 char_u *str;
15677 char_u buf[NUMBUFLEN];
15678
15679 str = get_tv_string_buf(&argvars[0], buf);
15680 do_pyeval(str, rettv);
15681}
15682#endif
15683
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015684/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015685 * "range()" function
15686 */
15687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015688f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015689{
15690 long start;
15691 long end;
15692 long stride = 1;
15693 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015694 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015697 if (argvars[1].v_type == VAR_UNKNOWN)
15698 {
15699 end = start - 1;
15700 start = 0;
15701 }
15702 else
15703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015704 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015706 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015707 }
15708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015709 if (error)
15710 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015711 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015712 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015713 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015714 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015715 else
15716 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015717 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015718 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015719 if (list_append_number(rettv->vval.v_list,
15720 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015721 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015722 }
15723}
15724
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015725/*
15726 * "readfile()" function
15727 */
15728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015729f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015730{
15731 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015732 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015733 char_u *fname;
15734 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015735 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15736 int io_size = sizeof(buf);
15737 int readlen; /* size of last fread() */
15738 char_u *prev = NULL; /* previously read bytes, if any */
15739 long prevlen = 0; /* length of data in prev */
15740 long prevsize = 0; /* size of prev buffer */
15741 long maxline = MAXLNUM;
15742 long cnt = 0;
15743 char_u *p; /* position in buf */
15744 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015745
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015746 if (argvars[1].v_type != VAR_UNKNOWN)
15747 {
15748 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15749 binary = TRUE;
15750 if (argvars[2].v_type != VAR_UNKNOWN)
15751 maxline = get_tv_number(&argvars[2]);
15752 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015753
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015754 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015755 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015756
15757 /* Always open the file in binary mode, library functions have a mind of
15758 * their own about CR-LF conversion. */
15759 fname = get_tv_string(&argvars[0]);
15760 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15761 {
15762 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15763 return;
15764 }
15765
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015766 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015767 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015768 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015769
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015770 /* This for loop processes what was read, but is also entered at end
15771 * of file so that either:
15772 * - an incomplete line gets written
15773 * - a "binary" file gets an empty line at the end if it ends in a
15774 * newline. */
15775 for (p = buf, start = buf;
15776 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15777 ++p)
15778 {
15779 if (*p == '\n' || readlen <= 0)
15780 {
15781 listitem_T *li;
15782 char_u *s = NULL;
15783 long_u len = p - start;
15784
15785 /* Finished a line. Remove CRs before NL. */
15786 if (readlen > 0 && !binary)
15787 {
15788 while (len > 0 && start[len - 1] == '\r')
15789 --len;
15790 /* removal may cross back to the "prev" string */
15791 if (len == 0)
15792 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15793 --prevlen;
15794 }
15795 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015796 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015797 else
15798 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015799 /* Change "prev" buffer to be the right size. This way
15800 * the bytes are only copied once, and very long lines are
15801 * allocated only once. */
15802 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015803 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015804 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015805 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015806 prev = NULL; /* the list will own the string */
15807 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015808 }
15809 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015810 if (s == NULL)
15811 {
15812 do_outofmem_msg((long_u) prevlen + len + 1);
15813 failed = TRUE;
15814 break;
15815 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015816
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015817 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015818 {
15819 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015820 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015821 break;
15822 }
15823 li->li_tv.v_type = VAR_STRING;
15824 li->li_tv.v_lock = 0;
15825 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015826 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015827
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015828 start = p + 1; /* step over newline */
15829 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015830 break;
15831 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015832 else if (*p == NUL)
15833 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015834#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015835 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15836 * when finding the BF and check the previous two bytes. */
15837 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015838 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015839 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15840 * + 1, these may be in the "prev" string. */
15841 char_u back1 = p >= buf + 1 ? p[-1]
15842 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15843 char_u back2 = p >= buf + 2 ? p[-2]
15844 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15845 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015846
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015847 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015848 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015849 char_u *dest = p - 2;
15850
15851 /* Usually a BOM is at the beginning of a file, and so at
15852 * the beginning of a line; then we can just step over it.
15853 */
15854 if (start == dest)
15855 start = p + 1;
15856 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015857 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015858 /* have to shuffle buf to close gap */
15859 int adjust_prevlen = 0;
15860
15861 if (dest < buf)
15862 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015863 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015864 dest = buf;
15865 }
15866 if (readlen > p - buf + 1)
15867 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15868 readlen -= 3 - adjust_prevlen;
15869 prevlen -= adjust_prevlen;
15870 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015871 }
15872 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015873 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015874#endif
15875 } /* for */
15876
15877 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15878 break;
15879 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015880 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015881 /* There's part of a line in buf, store it in "prev". */
15882 if (p - start + prevlen >= prevsize)
15883 {
15884 /* need bigger "prev" buffer */
15885 char_u *newprev;
15886
15887 /* A common use case is ordinary text files and "prev" gets a
15888 * fragment of a line, so the first allocation is made
15889 * small, to avoid repeatedly 'allocing' large and
15890 * 'reallocing' small. */
15891 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015892 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015893 else
15894 {
15895 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015896 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015897 prevsize = grow50pc > growmin ? grow50pc : growmin;
15898 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015899 newprev = prev == NULL ? alloc(prevsize)
15900 : vim_realloc(prev, prevsize);
15901 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015902 {
15903 do_outofmem_msg((long_u)prevsize);
15904 failed = TRUE;
15905 break;
15906 }
15907 prev = newprev;
15908 }
15909 /* Add the line part to end of "prev". */
15910 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015911 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015912 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015913 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015914
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015915 /*
15916 * For a negative line count use only the lines at the end of the file,
15917 * free the rest.
15918 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015919 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015920 while (cnt > -maxline)
15921 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015922 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015923 --cnt;
15924 }
15925
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015926 if (failed)
15927 {
15928 list_free(rettv->vval.v_list, TRUE);
15929 /* readfile doc says an empty list is returned on error */
15930 rettv->vval.v_list = list_alloc();
15931 }
15932
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015933 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015934 fclose(fd);
15935}
15936
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015937#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015938static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015939
15940/*
15941 * Convert a List to proftime_T.
15942 * Return FAIL when there is something wrong.
15943 */
15944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015945list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015946{
15947 long n1, n2;
15948 int error = FALSE;
15949
15950 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15951 || arg->vval.v_list->lv_len != 2)
15952 return FAIL;
15953 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15954 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15955# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015956 tm->HighPart = n1;
15957 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015958# else
15959 tm->tv_sec = n1;
15960 tm->tv_usec = n2;
15961# endif
15962 return error ? FAIL : OK;
15963}
15964#endif /* FEAT_RELTIME */
15965
15966/*
15967 * "reltime()" function
15968 */
15969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015970f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015971{
15972#ifdef FEAT_RELTIME
15973 proftime_T res;
15974 proftime_T start;
15975
15976 if (argvars[0].v_type == VAR_UNKNOWN)
15977 {
15978 /* No arguments: get current time. */
15979 profile_start(&res);
15980 }
15981 else if (argvars[1].v_type == VAR_UNKNOWN)
15982 {
15983 if (list2proftime(&argvars[0], &res) == FAIL)
15984 return;
15985 profile_end(&res);
15986 }
15987 else
15988 {
15989 /* Two arguments: compute the difference. */
15990 if (list2proftime(&argvars[0], &start) == FAIL
15991 || list2proftime(&argvars[1], &res) == FAIL)
15992 return;
15993 profile_sub(&res, &start);
15994 }
15995
15996 if (rettv_list_alloc(rettv) == OK)
15997 {
15998 long n1, n2;
15999
16000# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016001 n1 = res.HighPart;
16002 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016003# else
16004 n1 = res.tv_sec;
16005 n2 = res.tv_usec;
16006# endif
16007 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16008 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16009 }
16010#endif
16011}
16012
16013/*
16014 * "reltimestr()" function
16015 */
16016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016017f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016018{
16019#ifdef FEAT_RELTIME
16020 proftime_T tm;
16021#endif
16022
16023 rettv->v_type = VAR_STRING;
16024 rettv->vval.v_string = NULL;
16025#ifdef FEAT_RELTIME
16026 if (list2proftime(&argvars[0], &tm) == OK)
16027 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16028#endif
16029}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016030
Bram Moolenaar0d660222005-01-07 21:51:51 +000016031#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016032static void make_connection(void);
16033static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016034
16035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016036make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016037{
16038 if (X_DISPLAY == NULL
16039# ifdef FEAT_GUI
16040 && !gui.in_use
16041# endif
16042 )
16043 {
16044 x_force_connect = TRUE;
16045 setup_term_clip();
16046 x_force_connect = FALSE;
16047 }
16048}
16049
16050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016051check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016052{
16053 make_connection();
16054 if (X_DISPLAY == NULL)
16055 {
16056 EMSG(_("E240: No connection to Vim server"));
16057 return FAIL;
16058 }
16059 return OK;
16060}
16061#endif
16062
16063#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016064static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016065
16066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016067remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016068{
16069 char_u *server_name;
16070 char_u *keys;
16071 char_u *r = NULL;
16072 char_u buf[NUMBUFLEN];
16073# ifdef WIN32
16074 HWND w;
16075# else
16076 Window w;
16077# endif
16078
16079 if (check_restricted() || check_secure())
16080 return;
16081
16082# ifdef FEAT_X11
16083 if (check_connection() == FAIL)
16084 return;
16085# endif
16086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016087 server_name = get_tv_string_chk(&argvars[0]);
16088 if (server_name == NULL)
16089 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090 keys = get_tv_string_buf(&argvars[1], buf);
16091# ifdef WIN32
16092 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16093# else
16094 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16095 < 0)
16096# endif
16097 {
16098 if (r != NULL)
16099 EMSG(r); /* sending worked but evaluation failed */
16100 else
16101 EMSG2(_("E241: Unable to send to %s"), server_name);
16102 return;
16103 }
16104
16105 rettv->vval.v_string = r;
16106
16107 if (argvars[2].v_type != VAR_UNKNOWN)
16108 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016109 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016110 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016112
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016113 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016114 v.di_tv.v_type = VAR_STRING;
16115 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016116 idvar = get_tv_string_chk(&argvars[2]);
16117 if (idvar != NULL)
16118 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016119 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016120 }
16121}
16122#endif
16123
16124/*
16125 * "remote_expr()" function
16126 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016128f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129{
16130 rettv->v_type = VAR_STRING;
16131 rettv->vval.v_string = NULL;
16132#ifdef FEAT_CLIENTSERVER
16133 remote_common(argvars, rettv, TRUE);
16134#endif
16135}
16136
16137/*
16138 * "remote_foreground()" function
16139 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016141f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016142{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016143#ifdef FEAT_CLIENTSERVER
16144# ifdef WIN32
16145 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016146 {
16147 char_u *server_name = get_tv_string_chk(&argvars[0]);
16148
16149 if (server_name != NULL)
16150 serverForeground(server_name);
16151 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016152# else
16153 /* Send a foreground() expression to the server. */
16154 argvars[1].v_type = VAR_STRING;
16155 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16156 argvars[2].v_type = VAR_UNKNOWN;
16157 remote_common(argvars, rettv, TRUE);
16158 vim_free(argvars[1].vval.v_string);
16159# endif
16160#endif
16161}
16162
Bram Moolenaar0d660222005-01-07 21:51:51 +000016163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016164f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165{
16166#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016167 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168 char_u *s = NULL;
16169# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016170 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016172 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016173
16174 if (check_restricted() || check_secure())
16175 {
16176 rettv->vval.v_number = -1;
16177 return;
16178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016179 serverid = get_tv_string_chk(&argvars[0]);
16180 if (serverid == NULL)
16181 {
16182 rettv->vval.v_number = -1;
16183 return; /* type error; errmsg already given */
16184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016186 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187 if (n == 0)
16188 rettv->vval.v_number = -1;
16189 else
16190 {
16191 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16192 rettv->vval.v_number = (s != NULL);
16193 }
16194# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195 if (check_connection() == FAIL)
16196 return;
16197
16198 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016199 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200# endif
16201
16202 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016204 char_u *retvar;
16205
Bram Moolenaar33570922005-01-25 22:26:29 +000016206 v.di_tv.v_type = VAR_STRING;
16207 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016208 retvar = get_tv_string_chk(&argvars[1]);
16209 if (retvar != NULL)
16210 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016211 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212 }
16213#else
16214 rettv->vval.v_number = -1;
16215#endif
16216}
16217
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016219f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016220{
16221 char_u *r = NULL;
16222
16223#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016224 char_u *serverid = get_tv_string_chk(&argvars[0]);
16225
16226 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227 {
16228# ifdef WIN32
16229 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016230 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016231
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016232 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016233 if (n != 0)
16234 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16235 if (r == NULL)
16236# else
16237 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016238 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016239# endif
16240 EMSG(_("E277: Unable to read a server reply"));
16241 }
16242#endif
16243 rettv->v_type = VAR_STRING;
16244 rettv->vval.v_string = r;
16245}
16246
16247/*
16248 * "remote_send()" function
16249 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252{
16253 rettv->v_type = VAR_STRING;
16254 rettv->vval.v_string = NULL;
16255#ifdef FEAT_CLIENTSERVER
16256 remote_common(argvars, rettv, FALSE);
16257#endif
16258}
16259
16260/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016261 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016262 */
16263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016264f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016265{
Bram Moolenaar33570922005-01-25 22:26:29 +000016266 list_T *l;
16267 listitem_T *item, *item2;
16268 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016269 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016270 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016271 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016272 dict_T *d;
16273 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016274 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016275
Bram Moolenaar8c711452005-01-14 21:53:12 +000016276 if (argvars[0].v_type == VAR_DICT)
16277 {
16278 if (argvars[2].v_type != VAR_UNKNOWN)
16279 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016280 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016281 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016283 key = get_tv_string_chk(&argvars[1]);
16284 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016286 di = dict_find(d, key, -1);
16287 if (di == NULL)
16288 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016289 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16290 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016291 {
16292 *rettv = di->di_tv;
16293 init_tv(&di->di_tv);
16294 dictitem_remove(d, di);
16295 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016296 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016297 }
16298 }
16299 else if (argvars[0].v_type != VAR_LIST)
16300 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016301 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016302 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016304 int error = FALSE;
16305
16306 idx = get_tv_number_chk(&argvars[1], &error);
16307 if (error)
16308 ; /* type error: do nothing, errmsg already given */
16309 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016310 EMSGN(_(e_listidx), idx);
16311 else
16312 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016313 if (argvars[2].v_type == VAR_UNKNOWN)
16314 {
16315 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016316 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016317 *rettv = item->li_tv;
16318 vim_free(item);
16319 }
16320 else
16321 {
16322 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016323 end = get_tv_number_chk(&argvars[2], &error);
16324 if (error)
16325 ; /* type error: do nothing */
16326 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016327 EMSGN(_(e_listidx), end);
16328 else
16329 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016330 int cnt = 0;
16331
16332 for (li = item; li != NULL; li = li->li_next)
16333 {
16334 ++cnt;
16335 if (li == item2)
16336 break;
16337 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016338 if (li == NULL) /* didn't find "item2" after "item" */
16339 EMSG(_(e_invrange));
16340 else
16341 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016342 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016343 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016344 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016345 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016346 l->lv_first = item;
16347 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016348 item->li_prev = NULL;
16349 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016350 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016351 }
16352 }
16353 }
16354 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016355 }
16356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357}
16358
16359/*
16360 * "rename({from}, {to})" function
16361 */
16362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016363f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364{
16365 char_u buf[NUMBUFLEN];
16366
16367 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016370 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16371 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372}
16373
16374/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016375 * "repeat()" function
16376 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016378f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016379{
16380 char_u *p;
16381 int n;
16382 int slen;
16383 int len;
16384 char_u *r;
16385 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016386
16387 n = get_tv_number(&argvars[1]);
16388 if (argvars[0].v_type == VAR_LIST)
16389 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016390 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016391 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016392 if (list_extend(rettv->vval.v_list,
16393 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016394 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016395 }
16396 else
16397 {
16398 p = get_tv_string(&argvars[0]);
16399 rettv->v_type = VAR_STRING;
16400 rettv->vval.v_string = NULL;
16401
16402 slen = (int)STRLEN(p);
16403 len = slen * n;
16404 if (len <= 0)
16405 return;
16406
16407 r = alloc(len + 1);
16408 if (r != NULL)
16409 {
16410 for (i = 0; i < n; i++)
16411 mch_memmove(r + i * slen, p, (size_t)slen);
16412 r[len] = NUL;
16413 }
16414
16415 rettv->vval.v_string = r;
16416 }
16417}
16418
16419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 * "resolve()" function
16421 */
16422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016423f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424{
16425 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016426#ifdef HAVE_READLINK
16427 char_u *buf = NULL;
16428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016430 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431#ifdef FEAT_SHORTCUT
16432 {
16433 char_u *v = NULL;
16434
16435 v = mch_resolve_shortcut(p);
16436 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016437 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 }
16441#else
16442# ifdef HAVE_READLINK
16443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 char_u *cpy;
16445 int len;
16446 char_u *remain = NULL;
16447 char_u *q;
16448 int is_relative_to_current = FALSE;
16449 int has_trailing_pathsep = FALSE;
16450 int limit = 100;
16451
16452 p = vim_strsave(p);
16453
16454 if (p[0] == '.' && (vim_ispathsep(p[1])
16455 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16456 is_relative_to_current = TRUE;
16457
16458 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016459 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016462 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464
16465 q = getnextcomp(p);
16466 if (*q != NUL)
16467 {
16468 /* Separate the first path component in "p", and keep the
16469 * remainder (beginning with the path separator). */
16470 remain = vim_strsave(q - 1);
16471 q[-1] = NUL;
16472 }
16473
Bram Moolenaard9462e32011-04-11 21:35:11 +020016474 buf = alloc(MAXPATHL + 1);
16475 if (buf == NULL)
16476 goto fail;
16477
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 for (;;)
16479 {
16480 for (;;)
16481 {
16482 len = readlink((char *)p, (char *)buf, MAXPATHL);
16483 if (len <= 0)
16484 break;
16485 buf[len] = NUL;
16486
16487 if (limit-- == 0)
16488 {
16489 vim_free(p);
16490 vim_free(remain);
16491 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016492 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 goto fail;
16494 }
16495
16496 /* Ensure that the result will have a trailing path separator
16497 * if the argument has one. */
16498 if (remain == NULL && has_trailing_pathsep)
16499 add_pathsep(buf);
16500
16501 /* Separate the first path component in the link value and
16502 * concatenate the remainders. */
16503 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16504 if (*q != NUL)
16505 {
16506 if (remain == NULL)
16507 remain = vim_strsave(q - 1);
16508 else
16509 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016510 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 if (cpy != NULL)
16512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 vim_free(remain);
16514 remain = cpy;
16515 }
16516 }
16517 q[-1] = NUL;
16518 }
16519
16520 q = gettail(p);
16521 if (q > p && *q == NUL)
16522 {
16523 /* Ignore trailing path separator. */
16524 q[-1] = NUL;
16525 q = gettail(p);
16526 }
16527 if (q > p && !mch_isFullName(buf))
16528 {
16529 /* symlink is relative to directory of argument */
16530 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16531 if (cpy != NULL)
16532 {
16533 STRCPY(cpy, p);
16534 STRCPY(gettail(cpy), buf);
16535 vim_free(p);
16536 p = cpy;
16537 }
16538 }
16539 else
16540 {
16541 vim_free(p);
16542 p = vim_strsave(buf);
16543 }
16544 }
16545
16546 if (remain == NULL)
16547 break;
16548
16549 /* Append the first path component of "remain" to "p". */
16550 q = getnextcomp(remain + 1);
16551 len = q - remain - (*q != NUL);
16552 cpy = vim_strnsave(p, STRLEN(p) + len);
16553 if (cpy != NULL)
16554 {
16555 STRNCAT(cpy, remain, len);
16556 vim_free(p);
16557 p = cpy;
16558 }
16559 /* Shorten "remain". */
16560 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016561 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 else
16563 {
16564 vim_free(remain);
16565 remain = NULL;
16566 }
16567 }
16568
16569 /* If the result is a relative path name, make it explicitly relative to
16570 * the current directory if and only if the argument had this form. */
16571 if (!vim_ispathsep(*p))
16572 {
16573 if (is_relative_to_current
16574 && *p != NUL
16575 && !(p[0] == '.'
16576 && (p[1] == NUL
16577 || vim_ispathsep(p[1])
16578 || (p[1] == '.'
16579 && (p[2] == NUL
16580 || vim_ispathsep(p[2]))))))
16581 {
16582 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016583 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 if (cpy != NULL)
16585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 vim_free(p);
16587 p = cpy;
16588 }
16589 }
16590 else if (!is_relative_to_current)
16591 {
16592 /* Strip leading "./". */
16593 q = p;
16594 while (q[0] == '.' && vim_ispathsep(q[1]))
16595 q += 2;
16596 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016597 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598 }
16599 }
16600
16601 /* Ensure that the result will have no trailing path separator
16602 * if the argument had none. But keep "/" or "//". */
16603 if (!has_trailing_pathsep)
16604 {
16605 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016606 if (after_pathsep(p, q))
16607 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 }
16609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016610 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 }
16612# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614# endif
16615#endif
16616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016617 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618
16619#ifdef HAVE_READLINK
16620fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016621 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016623 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624}
16625
16626/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016627 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 */
16629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016630f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631{
Bram Moolenaar33570922005-01-25 22:26:29 +000016632 list_T *l;
16633 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634
Bram Moolenaar0d660222005-01-07 21:51:51 +000016635 if (argvars[0].v_type != VAR_LIST)
16636 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016637 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016638 && !tv_check_lock(l->lv_lock,
16639 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016640 {
16641 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016642 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016643 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016644 while (li != NULL)
16645 {
16646 ni = li->li_prev;
16647 list_append(l, li);
16648 li = ni;
16649 }
16650 rettv->vval.v_list = l;
16651 rettv->v_type = VAR_LIST;
16652 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016653 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655}
16656
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016657#define SP_NOMOVE 0x01 /* don't move cursor */
16658#define SP_REPEAT 0x02 /* repeat to find outer pair */
16659#define SP_RETCOUNT 0x04 /* return matchcount */
16660#define SP_SETPCMARK 0x08 /* set previous context mark */
16661#define SP_START 0x10 /* accept match at start position */
16662#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16663#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016664#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016665
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016666static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016667
16668/*
16669 * Get flags for a search function.
16670 * Possibly sets "p_ws".
16671 * Returns BACKWARD, FORWARD or zero (for an error).
16672 */
16673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016674get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016675{
16676 int dir = FORWARD;
16677 char_u *flags;
16678 char_u nbuf[NUMBUFLEN];
16679 int mask;
16680
16681 if (varp->v_type != VAR_UNKNOWN)
16682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016683 flags = get_tv_string_buf_chk(varp, nbuf);
16684 if (flags == NULL)
16685 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016686 while (*flags != NUL)
16687 {
16688 switch (*flags)
16689 {
16690 case 'b': dir = BACKWARD; break;
16691 case 'w': p_ws = TRUE; break;
16692 case 'W': p_ws = FALSE; break;
16693 default: mask = 0;
16694 if (flagsp != NULL)
16695 switch (*flags)
16696 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016697 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016698 case 'e': mask = SP_END; break;
16699 case 'm': mask = SP_RETCOUNT; break;
16700 case 'n': mask = SP_NOMOVE; break;
16701 case 'p': mask = SP_SUBPAT; break;
16702 case 'r': mask = SP_REPEAT; break;
16703 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016704 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016705 }
16706 if (mask == 0)
16707 {
16708 EMSG2(_(e_invarg2), flags);
16709 dir = 0;
16710 }
16711 else
16712 *flagsp |= mask;
16713 }
16714 if (dir == 0)
16715 break;
16716 ++flags;
16717 }
16718 }
16719 return dir;
16720}
16721
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016723 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016726search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016728 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729 char_u *pat;
16730 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016731 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 int save_p_ws = p_ws;
16733 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016734 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016735 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016736 proftime_T tm;
16737#ifdef FEAT_RELTIME
16738 long time_limit = 0;
16739#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016740 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016741 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016744 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016745 if (dir == 0)
16746 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016747 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016748 if (flags & SP_START)
16749 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016750 if (flags & SP_END)
16751 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016752 if (flags & SP_COLUMN)
16753 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016754
Bram Moolenaar76929292008-01-06 19:07:36 +000016755 /* Optional arguments: line number to stop searching and timeout. */
16756 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016757 {
16758 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16759 if (lnum_stop < 0)
16760 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016761#ifdef FEAT_RELTIME
16762 if (argvars[3].v_type != VAR_UNKNOWN)
16763 {
16764 time_limit = get_tv_number_chk(&argvars[3], NULL);
16765 if (time_limit < 0)
16766 goto theend;
16767 }
16768#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016769 }
16770
Bram Moolenaar76929292008-01-06 19:07:36 +000016771#ifdef FEAT_RELTIME
16772 /* Set the time limit, if there is one. */
16773 profile_setlimit(time_limit, &tm);
16774#endif
16775
Bram Moolenaar231334e2005-07-25 20:46:57 +000016776 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016777 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016778 * Check to make sure only those flags are set.
16779 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16780 * flags cannot be set. Check for that condition also.
16781 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016782 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016783 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016785 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016786 goto theend;
16787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016789 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016790 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016791 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016792 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016794 if (flags & SP_SUBPAT)
16795 retval = subpatnum;
16796 else
16797 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016798 if (flags & SP_SETPCMARK)
16799 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016801 if (match_pos != NULL)
16802 {
16803 /* Store the match cursor position */
16804 match_pos->lnum = pos.lnum;
16805 match_pos->col = pos.col + 1;
16806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 /* "/$" will put the cursor after the end of the line, may need to
16808 * correct that here */
16809 check_cursor();
16810 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016811
16812 /* If 'n' flag is used: restore cursor position. */
16813 if (flags & SP_NOMOVE)
16814 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016815 else
16816 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016817theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016819
16820 return retval;
16821}
16822
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016823#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016824
16825/*
16826 * round() is not in C90, use ceil() or floor() instead.
16827 */
16828 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016829vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016830{
16831 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16832}
16833
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016834/*
16835 * "round({float})" function
16836 */
16837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016838f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016839{
16840 float_T f;
16841
16842 rettv->v_type = VAR_FLOAT;
16843 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016844 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016845 else
16846 rettv->vval.v_float = 0.0;
16847}
16848#endif
16849
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016850/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016851 * "screenattr()" function
16852 */
16853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016854f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016855{
16856 int row;
16857 int col;
16858 int c;
16859
16860 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16861 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16862 if (row < 0 || row >= screen_Rows
16863 || col < 0 || col >= screen_Columns)
16864 c = -1;
16865 else
16866 c = ScreenAttrs[LineOffset[row] + col];
16867 rettv->vval.v_number = c;
16868}
16869
16870/*
16871 * "screenchar()" function
16872 */
16873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016874f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016875{
16876 int row;
16877 int col;
16878 int off;
16879 int c;
16880
16881 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16882 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16883 if (row < 0 || row >= screen_Rows
16884 || col < 0 || col >= screen_Columns)
16885 c = -1;
16886 else
16887 {
16888 off = LineOffset[row] + col;
16889#ifdef FEAT_MBYTE
16890 if (enc_utf8 && ScreenLinesUC[off] != 0)
16891 c = ScreenLinesUC[off];
16892 else
16893#endif
16894 c = ScreenLines[off];
16895 }
16896 rettv->vval.v_number = c;
16897}
16898
16899/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016900 * "screencol()" function
16901 *
16902 * First column is 1 to be consistent with virtcol().
16903 */
16904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016905f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016906{
16907 rettv->vval.v_number = screen_screencol() + 1;
16908}
16909
16910/*
16911 * "screenrow()" function
16912 */
16913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016914f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016915{
16916 rettv->vval.v_number = screen_screenrow() + 1;
16917}
16918
16919/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016920 * "search()" function
16921 */
16922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016923f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016924{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016925 int flags = 0;
16926
16927 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928}
16929
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016931 * "searchdecl()" function
16932 */
16933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016934f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016935{
16936 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016937 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016938 int error = FALSE;
16939 char_u *name;
16940
16941 rettv->vval.v_number = 1; /* default: FAIL */
16942
16943 name = get_tv_string_chk(&argvars[0]);
16944 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016945 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016946 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016947 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16948 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16949 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016950 if (!error && name != NULL)
16951 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016952 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016953}
16954
16955/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016956 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016959searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960{
16961 char_u *spat, *mpat, *epat;
16962 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964 int dir;
16965 int flags = 0;
16966 char_u nbuf1[NUMBUFLEN];
16967 char_u nbuf2[NUMBUFLEN];
16968 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016969 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016970 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016971 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016974 spat = get_tv_string_chk(&argvars[0]);
16975 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16976 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16977 if (spat == NULL || mpat == NULL || epat == NULL)
16978 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 /* Handle the optional fourth argument: flags */
16981 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016982 if (dir == 0)
16983 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016984
16985 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016986 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16987 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016988 if ((flags & (SP_END | SP_SUBPAT)) != 0
16989 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016990 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016991 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016992 goto theend;
16993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016995 /* Using 'r' implies 'W', otherwise it doesn't work. */
16996 if (flags & SP_REPEAT)
16997 p_ws = FALSE;
16998
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016999 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017000 if (argvars[3].v_type == VAR_UNKNOWN
17001 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 skip = (char_u *)"";
17003 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017005 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017006 if (argvars[5].v_type != VAR_UNKNOWN)
17007 {
17008 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17009 if (lnum_stop < 0)
17010 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017011#ifdef FEAT_RELTIME
17012 if (argvars[6].v_type != VAR_UNKNOWN)
17013 {
17014 time_limit = get_tv_number_chk(&argvars[6], NULL);
17015 if (time_limit < 0)
17016 goto theend;
17017 }
17018#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017019 }
17020 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 if (skip == NULL)
17022 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017024 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017025 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017026
17027theend:
17028 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017029
17030 return retval;
17031}
17032
17033/*
17034 * "searchpair()" function
17035 */
17036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017037f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017038{
17039 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17040}
17041
17042/*
17043 * "searchpairpos()" function
17044 */
17045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017046f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017047{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017048 pos_T match_pos;
17049 int lnum = 0;
17050 int col = 0;
17051
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017052 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017053 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017054
17055 if (searchpair_cmn(argvars, &match_pos) > 0)
17056 {
17057 lnum = match_pos.lnum;
17058 col = match_pos.col;
17059 }
17060
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017061 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17062 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017063}
17064
17065/*
17066 * Search for a start/middle/end thing.
17067 * Used by searchpair(), see its documentation for the details.
17068 * Returns 0 or -1 for no match,
17069 */
17070 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017071do_searchpair(
17072 char_u *spat, /* start pattern */
17073 char_u *mpat, /* middle pattern */
17074 char_u *epat, /* end pattern */
17075 int dir, /* BACKWARD or FORWARD */
17076 char_u *skip, /* skip expression */
17077 int flags, /* SP_SETPCMARK and other SP_ values */
17078 pos_T *match_pos,
17079 linenr_T lnum_stop, /* stop at this line if not zero */
17080 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017081{
17082 char_u *save_cpo;
17083 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17084 long retval = 0;
17085 pos_T pos;
17086 pos_T firstpos;
17087 pos_T foundpos;
17088 pos_T save_cursor;
17089 pos_T save_pos;
17090 int n;
17091 int r;
17092 int nest = 1;
17093 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017094 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017095 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017096
17097 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17098 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017099 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017100
Bram Moolenaar76929292008-01-06 19:07:36 +000017101#ifdef FEAT_RELTIME
17102 /* Set the time limit, if there is one. */
17103 profile_setlimit(time_limit, &tm);
17104#endif
17105
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017106 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17107 * start/middle/end (pat3, for the top pair). */
17108 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17109 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17110 if (pat2 == NULL || pat3 == NULL)
17111 goto theend;
17112 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17113 if (*mpat == NUL)
17114 STRCPY(pat3, pat2);
17115 else
17116 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17117 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017118 if (flags & SP_START)
17119 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017120
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 save_cursor = curwin->w_cursor;
17122 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017123 clearpos(&firstpos);
17124 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 pat = pat3;
17126 for (;;)
17127 {
17128 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017129 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17131 /* didn't find it or found the first match again: FAIL */
17132 break;
17133
17134 if (firstpos.lnum == 0)
17135 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017136 if (equalpos(pos, foundpos))
17137 {
17138 /* Found the same position again. Can happen with a pattern that
17139 * has "\zs" at the end and searching backwards. Advance one
17140 * character and try again. */
17141 if (dir == BACKWARD)
17142 decl(&pos);
17143 else
17144 incl(&pos);
17145 }
17146 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017148 /* clear the start flag to avoid getting stuck here */
17149 options &= ~SEARCH_START;
17150
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 /* If the skip pattern matches, ignore this match. */
17152 if (*skip != NUL)
17153 {
17154 save_pos = curwin->w_cursor;
17155 curwin->w_cursor = pos;
17156 r = eval_to_bool(skip, &err, NULL, FALSE);
17157 curwin->w_cursor = save_pos;
17158 if (err)
17159 {
17160 /* Evaluating {skip} caused an error, break here. */
17161 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017162 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 break;
17164 }
17165 if (r)
17166 continue;
17167 }
17168
17169 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17170 {
17171 /* Found end when searching backwards or start when searching
17172 * forward: nested pair. */
17173 ++nest;
17174 pat = pat2; /* nested, don't search for middle */
17175 }
17176 else
17177 {
17178 /* Found end when searching forward or start when searching
17179 * backward: end of (nested) pair; or found middle in outer pair. */
17180 if (--nest == 1)
17181 pat = pat3; /* outer level, search for middle */
17182 }
17183
17184 if (nest == 0)
17185 {
17186 /* Found the match: return matchcount or line number. */
17187 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017188 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017190 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017191 if (flags & SP_SETPCMARK)
17192 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 curwin->w_cursor = pos;
17194 if (!(flags & SP_REPEAT))
17195 break;
17196 nest = 1; /* search for next unmatched */
17197 }
17198 }
17199
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017200 if (match_pos != NULL)
17201 {
17202 /* Store the match cursor position */
17203 match_pos->lnum = curwin->w_cursor.lnum;
17204 match_pos->col = curwin->w_cursor.col + 1;
17205 }
17206
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017208 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 curwin->w_cursor = save_cursor;
17210
17211theend:
17212 vim_free(pat2);
17213 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017214 if (p_cpo == empty_option)
17215 p_cpo = save_cpo;
17216 else
17217 /* Darn, evaluating the {skip} expression changed the value. */
17218 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017219
17220 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221}
17222
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017223/*
17224 * "searchpos()" function
17225 */
17226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017227f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017228{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017229 pos_T match_pos;
17230 int lnum = 0;
17231 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017232 int n;
17233 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017234
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017235 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017236 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017237
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017238 n = search_cmn(argvars, &match_pos, &flags);
17239 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017240 {
17241 lnum = match_pos.lnum;
17242 col = match_pos.col;
17243 }
17244
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017245 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17246 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017247 if (flags & SP_SUBPAT)
17248 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017249}
17250
Bram Moolenaar0d660222005-01-07 21:51:51 +000017251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017252f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017254#ifdef FEAT_CLIENTSERVER
17255 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017256 char_u *server = get_tv_string_chk(&argvars[0]);
17257 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258
Bram Moolenaar0d660222005-01-07 21:51:51 +000017259 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017260 if (server == NULL || reply == NULL)
17261 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017262 if (check_restricted() || check_secure())
17263 return;
17264# ifdef FEAT_X11
17265 if (check_connection() == FAIL)
17266 return;
17267# endif
17268
17269 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017271 EMSG(_("E258: Unable to send to client"));
17272 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017274 rettv->vval.v_number = 0;
17275#else
17276 rettv->vval.v_number = -1;
17277#endif
17278}
17279
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017281f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017282{
17283 char_u *r = NULL;
17284
17285#ifdef FEAT_CLIENTSERVER
17286# ifdef WIN32
17287 r = serverGetVimNames();
17288# else
17289 make_connection();
17290 if (X_DISPLAY != NULL)
17291 r = serverGetVimNames(X_DISPLAY);
17292# endif
17293#endif
17294 rettv->v_type = VAR_STRING;
17295 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296}
17297
17298/*
17299 * "setbufvar()" function
17300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017302f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303{
17304 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017307 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 char_u nbuf[NUMBUFLEN];
17309
17310 if (check_restricted() || check_secure())
17311 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017312 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17313 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017314 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 varp = &argvars[2];
17316
17317 if (buf != NULL && varname != NULL && varp != NULL)
17318 {
17319 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321
17322 if (*varname == '&')
17323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017324 long numval;
17325 char_u *strval;
17326 int error = FALSE;
17327
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017329 numval = get_tv_number_chk(varp, &error);
17330 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017331 if (!error && strval != NULL)
17332 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 }
17334 else
17335 {
17336 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17337 if (bufvarname != NULL)
17338 {
17339 STRCPY(bufvarname, "b:");
17340 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017341 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 vim_free(bufvarname);
17343 }
17344 }
17345
17346 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349}
17350
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017352f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017353{
17354 dict_T *d;
17355 dictitem_T *di;
17356 char_u *csearch;
17357
17358 if (argvars[0].v_type != VAR_DICT)
17359 {
17360 EMSG(_(e_dictreq));
17361 return;
17362 }
17363
17364 if ((d = argvars[0].vval.v_dict) != NULL)
17365 {
17366 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17367 if (csearch != NULL)
17368 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017369#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017370 if (enc_utf8)
17371 {
17372 int pcc[MAX_MCO];
17373 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017374
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017375 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17376 }
17377 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017378#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017379 set_last_csearch(PTR2CHAR(csearch),
17380 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017381 }
17382
17383 di = dict_find(d, (char_u *)"forward", -1);
17384 if (di != NULL)
17385 set_csearch_direction(get_tv_number(&di->di_tv)
17386 ? FORWARD : BACKWARD);
17387
17388 di = dict_find(d, (char_u *)"until", -1);
17389 if (di != NULL)
17390 set_csearch_until(!!get_tv_number(&di->di_tv));
17391 }
17392}
17393
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394/*
17395 * "setcmdpos()" function
17396 */
17397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017398f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017400 int pos = (int)get_tv_number(&argvars[0]) - 1;
17401
17402 if (pos >= 0)
17403 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404}
17405
17406/*
17407 * "setline()" function
17408 */
17409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017410f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411{
17412 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017413 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017414 list_T *l = NULL;
17415 listitem_T *li = NULL;
17416 long added = 0;
17417 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017419 lnum = get_tv_lnum(&argvars[0]);
17420 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017422 l = argvars[1].vval.v_list;
17423 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017425 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017426 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017427
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017428 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017429 for (;;)
17430 {
17431 if (l != NULL)
17432 {
17433 /* list argument, get next string */
17434 if (li == NULL)
17435 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017436 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017437 li = li->li_next;
17438 }
17439
17440 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017441 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017442 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017443
17444 /* When coming here from Insert mode, sync undo, so that this can be
17445 * undone separately from what was previously inserted. */
17446 if (u_sync_once == 2)
17447 {
17448 u_sync_once = 1; /* notify that u_sync() was called */
17449 u_sync(TRUE);
17450 }
17451
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017452 if (lnum <= curbuf->b_ml.ml_line_count)
17453 {
17454 /* existing line, replace it */
17455 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17456 {
17457 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017458 if (lnum == curwin->w_cursor.lnum)
17459 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017460 rettv->vval.v_number = 0; /* OK */
17461 }
17462 }
17463 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17464 {
17465 /* lnum is one past the last line, append the line */
17466 ++added;
17467 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17468 rettv->vval.v_number = 0; /* OK */
17469 }
17470
17471 if (l == NULL) /* only one string argument */
17472 break;
17473 ++lnum;
17474 }
17475
17476 if (added > 0)
17477 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478}
17479
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017480static 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 +000017481
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017483 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017484 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017486set_qf_ll_list(
17487 win_T *wp UNUSED,
17488 typval_T *list_arg UNUSED,
17489 typval_T *action_arg UNUSED,
17490 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017491{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017492#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017493 char_u *act;
17494 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017495#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017496
Bram Moolenaar2641f772005-03-25 21:58:17 +000017497 rettv->vval.v_number = -1;
17498
17499#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017500 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017501 EMSG(_(e_listreq));
17502 else
17503 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017504 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017505
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017506 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017507 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017508 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017509 if (act == NULL)
17510 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017511 if (*act == 'a' || *act == 'r')
17512 action = *act;
17513 }
17514
Bram Moolenaar81484f42012-12-05 15:16:47 +010017515 if (l != NULL && set_errorlist(wp, l, action,
17516 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017517 rettv->vval.v_number = 0;
17518 }
17519#endif
17520}
17521
17522/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017523 * "setloclist()" function
17524 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017526f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017527{
17528 win_T *win;
17529
17530 rettv->vval.v_number = -1;
17531
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017532 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017533 if (win != NULL)
17534 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17535}
17536
17537/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017538 * "setmatches()" function
17539 */
17540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017541f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017542{
17543#ifdef FEAT_SEARCH_EXTRA
17544 list_T *l;
17545 listitem_T *li;
17546 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017547 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017548
17549 rettv->vval.v_number = -1;
17550 if (argvars[0].v_type != VAR_LIST)
17551 {
17552 EMSG(_(e_listreq));
17553 return;
17554 }
17555 if ((l = argvars[0].vval.v_list) != NULL)
17556 {
17557
17558 /* To some extent make sure that we are dealing with a list from
17559 * "getmatches()". */
17560 li = l->lv_first;
17561 while (li != NULL)
17562 {
17563 if (li->li_tv.v_type != VAR_DICT
17564 || (d = li->li_tv.vval.v_dict) == NULL)
17565 {
17566 EMSG(_(e_invarg));
17567 return;
17568 }
17569 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017570 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17571 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017572 && dict_find(d, (char_u *)"priority", -1) != NULL
17573 && dict_find(d, (char_u *)"id", -1) != NULL))
17574 {
17575 EMSG(_(e_invarg));
17576 return;
17577 }
17578 li = li->li_next;
17579 }
17580
17581 clear_matches(curwin);
17582 li = l->lv_first;
17583 while (li != NULL)
17584 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017585 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017586 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017587 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017588 char_u *group;
17589 int priority;
17590 int id;
17591 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017592
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017593 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017594 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17595 {
17596 if (s == NULL)
17597 {
17598 s = list_alloc();
17599 if (s == NULL)
17600 return;
17601 }
17602
17603 /* match from matchaddpos() */
17604 for (i = 1; i < 9; i++)
17605 {
17606 sprintf((char *)buf, (char *)"pos%d", i);
17607 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17608 {
17609 if (di->di_tv.v_type != VAR_LIST)
17610 return;
17611
17612 list_append_tv(s, &di->di_tv);
17613 s->lv_refcount++;
17614 }
17615 else
17616 break;
17617 }
17618 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017619
17620 group = get_dict_string(d, (char_u *)"group", FALSE);
17621 priority = (int)get_dict_number(d, (char_u *)"priority");
17622 id = (int)get_dict_number(d, (char_u *)"id");
17623 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17624 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17625 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017626 if (i == 0)
17627 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017628 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017629 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017630 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017631 }
17632 else
17633 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017634 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017635 list_unref(s);
17636 s = NULL;
17637 }
17638
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017639 li = li->li_next;
17640 }
17641 rettv->vval.v_number = 0;
17642 }
17643#endif
17644}
17645
17646/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017647 * "setpos()" function
17648 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017650f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017651{
17652 pos_T pos;
17653 int fnum;
17654 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017655 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017656
Bram Moolenaar08250432008-02-13 11:42:46 +000017657 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017658 name = get_tv_string_chk(argvars);
17659 if (name != NULL)
17660 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017661 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017662 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017663 if (--pos.col < 0)
17664 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017665 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017666 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017667 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017668 if (fnum == curbuf->b_fnum)
17669 {
17670 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017671 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017672 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017673 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017674 curwin->w_set_curswant = FALSE;
17675 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017676 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017677 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017678 }
17679 else
17680 EMSG(_(e_invarg));
17681 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017682 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17683 {
17684 /* set mark */
17685 if (setmark_pos(name[1], &pos, fnum) == OK)
17686 rettv->vval.v_number = 0;
17687 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017688 else
17689 EMSG(_(e_invarg));
17690 }
17691 }
17692}
17693
17694/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017695 * "setqflist()" function
17696 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017698f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017699{
17700 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17701}
17702
17703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 * "setreg()" function
17705 */
17706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017707f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708{
17709 int regname;
17710 char_u *strregname;
17711 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017712 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 int append;
17714 char_u yank_type;
17715 long block_len;
17716
17717 block_len = -1;
17718 yank_type = MAUTO;
17719 append = FALSE;
17720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017721 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017722 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017724 if (strregname == NULL)
17725 return; /* type error; errmsg already given */
17726 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727 if (regname == 0 || regname == '@')
17728 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017732 stropt = get_tv_string_chk(&argvars[2]);
17733 if (stropt == NULL)
17734 return; /* type error */
17735 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 switch (*stropt)
17737 {
17738 case 'a': case 'A': /* append */
17739 append = TRUE;
17740 break;
17741 case 'v': case 'c': /* character-wise selection */
17742 yank_type = MCHAR;
17743 break;
17744 case 'V': case 'l': /* line-wise selection */
17745 yank_type = MLINE;
17746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 case 'b': case Ctrl_V: /* block-wise selection */
17748 yank_type = MBLOCK;
17749 if (VIM_ISDIGIT(stropt[1]))
17750 {
17751 ++stropt;
17752 block_len = getdigits(&stropt) - 1;
17753 --stropt;
17754 }
17755 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 }
17757 }
17758
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017759 if (argvars[1].v_type == VAR_LIST)
17760 {
17761 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017762 char_u **allocval;
17763 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017764 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017765 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017766 int len = argvars[1].vval.v_list->lv_len;
17767 listitem_T *li;
17768
Bram Moolenaar7d647822014-04-05 21:28:56 +020017769 /* First half: use for pointers to result lines; second half: use for
17770 * pointers to allocated copies. */
17771 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017772 if (lstval == NULL)
17773 return;
17774 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017775 allocval = lstval + len + 2;
17776 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017777
17778 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17779 li = li->li_next)
17780 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017781 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017782 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017783 goto free_lstval;
17784 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017785 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017786 /* Need to make a copy, next get_tv_string_buf_chk() will
17787 * overwrite the string. */
17788 strval = vim_strsave(buf);
17789 if (strval == NULL)
17790 goto free_lstval;
17791 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017792 }
17793 *curval++ = strval;
17794 }
17795 *curval++ = NULL;
17796
17797 write_reg_contents_lst(regname, lstval, -1,
17798 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017799free_lstval:
17800 while (curallocval > allocval)
17801 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017802 vim_free(lstval);
17803 }
17804 else
17805 {
17806 strval = get_tv_string_chk(&argvars[1]);
17807 if (strval == NULL)
17808 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017809 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017812 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813}
17814
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017815/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017816 * "settabvar()" function
17817 */
17818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017819f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017820{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017821#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017822 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017823 tabpage_T *tp;
17824#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017825 char_u *varname, *tabvarname;
17826 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017827
17828 rettv->vval.v_number = 0;
17829
17830 if (check_restricted() || check_secure())
17831 return;
17832
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017833#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017834 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017835#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017836 varname = get_tv_string_chk(&argvars[1]);
17837 varp = &argvars[2];
17838
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017839 if (varname != NULL && varp != NULL
17840#ifdef FEAT_WINDOWS
17841 && tp != NULL
17842#endif
17843 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017844 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017845#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017846 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017847 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017848#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017849
17850 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17851 if (tabvarname != NULL)
17852 {
17853 STRCPY(tabvarname, "t:");
17854 STRCPY(tabvarname + 2, varname);
17855 set_var(tabvarname, varp, TRUE);
17856 vim_free(tabvarname);
17857 }
17858
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017859#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017860 /* Restore current tabpage */
17861 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017862 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017863#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017864 }
17865}
17866
17867/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017868 * "settabwinvar()" function
17869 */
17870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017871f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017872{
17873 setwinvar(argvars, rettv, 1);
17874}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875
17876/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017877 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017880f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017882 setwinvar(argvars, rettv, 0);
17883}
17884
17885/*
17886 * "setwinvar()" and "settabwinvar()" functions
17887 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017888
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017890setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017891{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 win_T *win;
17893#ifdef FEAT_WINDOWS
17894 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017895 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017896 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897#endif
17898 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017899 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017901 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902
17903 if (check_restricted() || check_secure())
17904 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017905
17906#ifdef FEAT_WINDOWS
17907 if (off == 1)
17908 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17909 else
17910 tp = curtab;
17911#endif
17912 win = find_win_by_nr(&argvars[off], tp);
17913 varname = get_tv_string_chk(&argvars[off + 1]);
17914 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915
17916 if (win != NULL && varname != NULL && varp != NULL)
17917 {
17918#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017919 need_switch_win = !(tp == curtab && win == curwin);
17920 if (!need_switch_win
17921 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017924 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017926 long numval;
17927 char_u *strval;
17928 int error = FALSE;
17929
17930 ++varname;
17931 numval = get_tv_number_chk(varp, &error);
17932 strval = get_tv_string_buf_chk(varp, nbuf);
17933 if (!error && strval != NULL)
17934 set_option_value(varname, numval, strval, OPT_LOCAL);
17935 }
17936 else
17937 {
17938 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17939 if (winvarname != NULL)
17940 {
17941 STRCPY(winvarname, "w:");
17942 STRCPY(winvarname + 2, varname);
17943 set_var(winvarname, varp, TRUE);
17944 vim_free(winvarname);
17945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946 }
17947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017949 if (need_switch_win)
17950 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951#endif
17952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953}
17954
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017955#ifdef FEAT_CRYPT
17956/*
17957 * "sha256({string})" function
17958 */
17959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017960f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017961{
17962 char_u *p;
17963
17964 p = get_tv_string(&argvars[0]);
17965 rettv->vval.v_string = vim_strsave(
17966 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17967 rettv->v_type = VAR_STRING;
17968}
17969#endif /* FEAT_CRYPT */
17970
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017972 * "shellescape({string})" function
17973 */
17974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017975f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017976{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017977 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017978 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017979 rettv->v_type = VAR_STRING;
17980}
17981
17982/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017983 * shiftwidth() function
17984 */
17985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017986f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017987{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017988 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017989}
17990
17991/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017992 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 */
17994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017995f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017997 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998
Bram Moolenaar0d660222005-01-07 21:51:51 +000017999 p = get_tv_string(&argvars[0]);
18000 rettv->vval.v_string = vim_strsave(p);
18001 simplify_filename(rettv->vval.v_string); /* simplify in place */
18002 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003}
18004
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018005#ifdef FEAT_FLOAT
18006/*
18007 * "sin()" function
18008 */
18009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018010f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018011{
18012 float_T f;
18013
18014 rettv->v_type = VAR_FLOAT;
18015 if (get_float_arg(argvars, &f) == OK)
18016 rettv->vval.v_float = sin(f);
18017 else
18018 rettv->vval.v_float = 0.0;
18019}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018020
18021/*
18022 * "sinh()" function
18023 */
18024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018025f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018026{
18027 float_T f;
18028
18029 rettv->v_type = VAR_FLOAT;
18030 if (get_float_arg(argvars, &f) == OK)
18031 rettv->vval.v_float = sinh(f);
18032 else
18033 rettv->vval.v_float = 0.0;
18034}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018035#endif
18036
Bram Moolenaar0d660222005-01-07 21:51:51 +000018037static int
18038#ifdef __BORLANDC__
18039 _RTLENTRYF
18040#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018041 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042static int
18043#ifdef __BORLANDC__
18044 _RTLENTRYF
18045#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018046 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018047
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018048/* struct used in the array that's given to qsort() */
18049typedef struct
18050{
18051 listitem_T *item;
18052 int idx;
18053} sortItem_T;
18054
Bram Moolenaar0d660222005-01-07 21:51:51 +000018055static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018056static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018057static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018058#ifdef FEAT_FLOAT
18059static int item_compare_float;
18060#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018061static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018062static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018063static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018064static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018065static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018066#define ITEM_COMPARE_FAIL 999
18067
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018069 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018071 static int
18072#ifdef __BORLANDC__
18073_RTLENTRYF
18074#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018075item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018077 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018078 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018079 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018080 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018081 int res;
18082 char_u numbuf1[NUMBUFLEN];
18083 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018085 si1 = (sortItem_T *)s1;
18086 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018087 tv1 = &si1->item->li_tv;
18088 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018089
18090 if (item_compare_numbers)
18091 {
18092 long v1 = get_tv_number(tv1);
18093 long v2 = get_tv_number(tv2);
18094
18095 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18096 }
18097
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018098#ifdef FEAT_FLOAT
18099 if (item_compare_float)
18100 {
18101 float_T v1 = get_tv_float(tv1);
18102 float_T v2 = get_tv_float(tv2);
18103
18104 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18105 }
18106#endif
18107
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018108 /* tv2string() puts quotes around a string and allocates memory. Don't do
18109 * that for string variables. Use a single quote when comparing with a
18110 * non-string to do what the docs promise. */
18111 if (tv1->v_type == VAR_STRING)
18112 {
18113 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18114 p1 = (char_u *)"'";
18115 else
18116 p1 = tv1->vval.v_string;
18117 }
18118 else
18119 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18120 if (tv2->v_type == VAR_STRING)
18121 {
18122 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18123 p2 = (char_u *)"'";
18124 else
18125 p2 = tv2->vval.v_string;
18126 }
18127 else
18128 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018129 if (p1 == NULL)
18130 p1 = (char_u *)"";
18131 if (p2 == NULL)
18132 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018133 if (!item_compare_numeric)
18134 {
18135 if (item_compare_ic)
18136 res = STRICMP(p1, p2);
18137 else
18138 res = STRCMP(p1, p2);
18139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018141 {
18142 double n1, n2;
18143 n1 = strtod((char *)p1, (char **)&p1);
18144 n2 = strtod((char *)p2, (char **)&p2);
18145 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18146 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018147
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018148 /* When the result would be zero, compare the item indexes. Makes the
18149 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018150 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018151 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018152
Bram Moolenaar0d660222005-01-07 21:51:51 +000018153 vim_free(tofree1);
18154 vim_free(tofree2);
18155 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156}
18157
18158 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018159#ifdef __BORLANDC__
18160_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018162item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018163{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018164 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018165 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018166 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018167 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018168 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018170 /* shortcut after failure in previous call; compare all items equal */
18171 if (item_compare_func_err)
18172 return 0;
18173
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018174 si1 = (sortItem_T *)s1;
18175 si2 = (sortItem_T *)s2;
18176
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018177 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018178 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018179 copy_tv(&si1->item->li_tv, &argv[0]);
18180 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018181
18182 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018183 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018184 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18185 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018186 clear_tv(&argv[0]);
18187 clear_tv(&argv[1]);
18188
18189 if (res == FAIL)
18190 res = ITEM_COMPARE_FAIL;
18191 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018192 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18193 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018194 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018195 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018196
18197 /* When the result would be zero, compare the pointers themselves. Makes
18198 * the sort stable. */
18199 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018200 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018201
Bram Moolenaar0d660222005-01-07 21:51:51 +000018202 return res;
18203}
18204
18205/*
18206 * "sort({list})" function
18207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018209do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210{
Bram Moolenaar33570922005-01-25 22:26:29 +000018211 list_T *l;
18212 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018213 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018214 long len;
18215 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216
Bram Moolenaar0d660222005-01-07 21:51:51 +000018217 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018218 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 else
18220 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018222 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018223 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18224 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018225 return;
18226 rettv->vval.v_list = l;
18227 rettv->v_type = VAR_LIST;
18228 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229
Bram Moolenaar0d660222005-01-07 21:51:51 +000018230 len = list_len(l);
18231 if (len <= 1)
18232 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233
Bram Moolenaar0d660222005-01-07 21:51:51 +000018234 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018235 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018236 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018237#ifdef FEAT_FLOAT
18238 item_compare_float = FALSE;
18239#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018240 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018241 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018242 if (argvars[1].v_type != VAR_UNKNOWN)
18243 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018244 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018246 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018247 else
18248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018249 int error = FALSE;
18250
18251 i = get_tv_number_chk(&argvars[1], &error);
18252 if (error)
18253 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018254 if (i == 1)
18255 item_compare_ic = TRUE;
18256 else
18257 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018258 if (item_compare_func != NULL)
18259 {
18260 if (STRCMP(item_compare_func, "n") == 0)
18261 {
18262 item_compare_func = NULL;
18263 item_compare_numeric = TRUE;
18264 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018265 else if (STRCMP(item_compare_func, "N") == 0)
18266 {
18267 item_compare_func = NULL;
18268 item_compare_numbers = TRUE;
18269 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018270#ifdef FEAT_FLOAT
18271 else if (STRCMP(item_compare_func, "f") == 0)
18272 {
18273 item_compare_func = NULL;
18274 item_compare_float = TRUE;
18275 }
18276#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018277 else if (STRCMP(item_compare_func, "i") == 0)
18278 {
18279 item_compare_func = NULL;
18280 item_compare_ic = TRUE;
18281 }
18282 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018283 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018284
18285 if (argvars[2].v_type != VAR_UNKNOWN)
18286 {
18287 /* optional third argument: {dict} */
18288 if (argvars[2].v_type != VAR_DICT)
18289 {
18290 EMSG(_(e_dictreq));
18291 return;
18292 }
18293 item_compare_selfdict = argvars[2].vval.v_dict;
18294 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296
Bram Moolenaar0d660222005-01-07 21:51:51 +000018297 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018298 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018299 if (ptrs == NULL)
18300 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301
Bram Moolenaar327aa022014-03-25 18:24:23 +010018302 i = 0;
18303 if (sort)
18304 {
18305 /* sort(): ptrs will be the list to sort */
18306 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018307 {
18308 ptrs[i].item = li;
18309 ptrs[i].idx = i;
18310 ++i;
18311 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018312
18313 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018314 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018315 /* test the compare function */
18316 if (item_compare_func != NULL
18317 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018318 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018319 EMSG(_("E702: Sort compare function failed"));
18320 else
18321 {
18322 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018323 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018324 item_compare_func == NULL ? item_compare : item_compare2);
18325
18326 if (!item_compare_func_err)
18327 {
18328 /* Clear the List and append the items in sorted order. */
18329 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18330 l->lv_len = 0;
18331 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018332 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018333 }
18334 }
18335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018337 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018338 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018339
18340 /* f_uniq(): ptrs will be a stack of items to remove */
18341 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018342 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018343 item_compare_func_ptr = item_compare_func
18344 ? item_compare2 : item_compare;
18345
18346 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18347 li = li->li_next)
18348 {
18349 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18350 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018351 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018352 if (item_compare_func_err)
18353 {
18354 EMSG(_("E882: Uniq compare function failed"));
18355 break;
18356 }
18357 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018359 if (!item_compare_func_err)
18360 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018361 while (--i >= 0)
18362 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018363 li = ptrs[i].item->li_next;
18364 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018365 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018366 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018367 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018368 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018369 list_fix_watch(l, li);
18370 listitem_free(li);
18371 l->lv_len--;
18372 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018373 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018374 }
18375
18376 vim_free(ptrs);
18377 }
18378}
18379
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018380/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018381 * "sort({list})" function
18382 */
18383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018384f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018385{
18386 do_sort_uniq(argvars, rettv, TRUE);
18387}
18388
18389/*
18390 * "uniq({list})" function
18391 */
18392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018393f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018394{
18395 do_sort_uniq(argvars, rettv, FALSE);
18396}
18397
18398/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018399 * "soundfold({word})" function
18400 */
18401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018402f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018403{
18404 char_u *s;
18405
18406 rettv->v_type = VAR_STRING;
18407 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018408#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018409 rettv->vval.v_string = eval_soundfold(s);
18410#else
18411 rettv->vval.v_string = vim_strsave(s);
18412#endif
18413}
18414
18415/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018416 * "spellbadword()" function
18417 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018419f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018420{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018421 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018422 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018423 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018424
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018425 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018426 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018427
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018428#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018429 if (argvars[0].v_type == VAR_UNKNOWN)
18430 {
18431 /* Find the start and length of the badly spelled word. */
18432 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18433 if (len != 0)
18434 word = ml_get_cursor();
18435 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018436 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018437 {
18438 char_u *str = get_tv_string_chk(&argvars[0]);
18439 int capcol = -1;
18440
18441 if (str != NULL)
18442 {
18443 /* Check the argument for spelling. */
18444 while (*str != NUL)
18445 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018446 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018447 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018448 {
18449 word = str;
18450 break;
18451 }
18452 str += len;
18453 }
18454 }
18455 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018456#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018457
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018458 list_append_string(rettv->vval.v_list, word, len);
18459 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018460 attr == HLF_SPB ? "bad" :
18461 attr == HLF_SPR ? "rare" :
18462 attr == HLF_SPL ? "local" :
18463 attr == HLF_SPC ? "caps" :
18464 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018465}
18466
18467/*
18468 * "spellsuggest()" function
18469 */
18470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018471f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018472{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018473#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018474 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018475 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018476 int maxcount;
18477 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018478 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018479 listitem_T *li;
18480 int need_capital = FALSE;
18481#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018482
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018483 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018484 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018485
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018486#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018487 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018488 {
18489 str = get_tv_string(&argvars[0]);
18490 if (argvars[1].v_type != VAR_UNKNOWN)
18491 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018492 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018493 if (maxcount <= 0)
18494 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018495 if (argvars[2].v_type != VAR_UNKNOWN)
18496 {
18497 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18498 if (typeerr)
18499 return;
18500 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018501 }
18502 else
18503 maxcount = 25;
18504
Bram Moolenaar4770d092006-01-12 23:22:24 +000018505 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018506
18507 for (i = 0; i < ga.ga_len; ++i)
18508 {
18509 str = ((char_u **)ga.ga_data)[i];
18510
18511 li = listitem_alloc();
18512 if (li == NULL)
18513 vim_free(str);
18514 else
18515 {
18516 li->li_tv.v_type = VAR_STRING;
18517 li->li_tv.v_lock = 0;
18518 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018519 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018520 }
18521 }
18522 ga_clear(&ga);
18523 }
18524#endif
18525}
18526
Bram Moolenaar0d660222005-01-07 21:51:51 +000018527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018528f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018529{
18530 char_u *str;
18531 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018532 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018533 regmatch_T regmatch;
18534 char_u patbuf[NUMBUFLEN];
18535 char_u *save_cpo;
18536 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018537 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018538 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018539 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018540
18541 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18542 save_cpo = p_cpo;
18543 p_cpo = (char_u *)"";
18544
18545 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018546 if (argvars[1].v_type != VAR_UNKNOWN)
18547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018548 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18549 if (pat == NULL)
18550 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018552 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018553 }
18554 if (pat == NULL || *pat == NUL)
18555 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018556
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018557 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018559 if (typeerr)
18560 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561
Bram Moolenaar0d660222005-01-07 21:51:51 +000018562 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18563 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018565 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018566 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018567 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018568 if (*str == NUL)
18569 match = FALSE; /* empty item at the end */
18570 else
18571 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018572 if (match)
18573 end = regmatch.startp[0];
18574 else
18575 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018576 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18577 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018578 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018579 if (list_append_string(rettv->vval.v_list, str,
18580 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018581 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018582 }
18583 if (!match)
18584 break;
18585 /* Advance to just after the match. */
18586 if (regmatch.endp[0] > str)
18587 col = 0;
18588 else
18589 {
18590 /* Don't get stuck at the same match. */
18591#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018592 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018593#else
18594 col = 1;
18595#endif
18596 }
18597 str = regmatch.endp[0];
18598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599
Bram Moolenaar473de612013-06-08 18:19:48 +020018600 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602
Bram Moolenaar0d660222005-01-07 21:51:51 +000018603 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604}
18605
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018606#ifdef FEAT_FLOAT
18607/*
18608 * "sqrt()" function
18609 */
18610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018611f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018612{
18613 float_T f;
18614
18615 rettv->v_type = VAR_FLOAT;
18616 if (get_float_arg(argvars, &f) == OK)
18617 rettv->vval.v_float = sqrt(f);
18618 else
18619 rettv->vval.v_float = 0.0;
18620}
18621
18622/*
18623 * "str2float()" function
18624 */
18625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018626f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018627{
18628 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18629
18630 if (*p == '+')
18631 p = skipwhite(p + 1);
18632 (void)string2float(p, &rettv->vval.v_float);
18633 rettv->v_type = VAR_FLOAT;
18634}
18635#endif
18636
Bram Moolenaar2c932302006-03-18 21:42:09 +000018637/*
18638 * "str2nr()" function
18639 */
18640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018641f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018642{
18643 int base = 10;
18644 char_u *p;
18645 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018646 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018647
18648 if (argvars[1].v_type != VAR_UNKNOWN)
18649 {
18650 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018651 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018652 {
18653 EMSG(_(e_invarg));
18654 return;
18655 }
18656 }
18657
18658 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018659 if (*p == '+')
18660 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018661 switch (base)
18662 {
18663 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18664 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18665 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18666 default: what = 0;
18667 }
18668 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018669 rettv->vval.v_number = n;
18670}
18671
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672#ifdef HAVE_STRFTIME
18673/*
18674 * "strftime({format}[, {time}])" function
18675 */
18676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018677f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678{
18679 char_u result_buf[256];
18680 struct tm *curtime;
18681 time_t seconds;
18682 char_u *p;
18683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018686 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018687 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 seconds = time(NULL);
18689 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 curtime = localtime(&seconds);
18692 /* MSVC returns NULL for an invalid value of seconds. */
18693 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 else
18696 {
18697# ifdef FEAT_MBYTE
18698 vimconv_T conv;
18699 char_u *enc;
18700
18701 conv.vc_type = CONV_NONE;
18702 enc = enc_locale();
18703 convert_setup(&conv, p_enc, enc);
18704 if (conv.vc_type != CONV_NONE)
18705 p = string_convert(&conv, p, NULL);
18706# endif
18707 if (p != NULL)
18708 (void)strftime((char *)result_buf, sizeof(result_buf),
18709 (char *)p, curtime);
18710 else
18711 result_buf[0] = NUL;
18712
18713# ifdef FEAT_MBYTE
18714 if (conv.vc_type != CONV_NONE)
18715 vim_free(p);
18716 convert_setup(&conv, enc, p_enc);
18717 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018718 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 else
18720# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018721 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722
18723# ifdef FEAT_MBYTE
18724 /* Release conversion descriptors */
18725 convert_setup(&conv, NULL, NULL);
18726 vim_free(enc);
18727# endif
18728 }
18729}
18730#endif
18731
18732/*
18733 * "stridx()" function
18734 */
18735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018736f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737{
18738 char_u buf[NUMBUFLEN];
18739 char_u *needle;
18740 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018741 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018743 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018745 needle = get_tv_string_chk(&argvars[1]);
18746 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018747 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018748 if (needle == NULL || haystack == NULL)
18749 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 if (argvars[2].v_type != VAR_UNKNOWN)
18752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018753 int error = FALSE;
18754
18755 start_idx = get_tv_number_chk(&argvars[2], &error);
18756 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018757 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018758 if (start_idx >= 0)
18759 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 }
18761
18762 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18763 if (pos != NULL)
18764 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765}
18766
18767/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018768 * "string()" function
18769 */
18770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018771f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018772{
18773 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018774 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018776 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018777 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018778 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018779 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018780 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781}
18782
18783/*
18784 * "strlen()" function
18785 */
18786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018787f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018789 rettv->vval.v_number = (varnumber_T)(STRLEN(
18790 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791}
18792
18793/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018794 * "strchars()" function
18795 */
18796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018797f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018798{
18799 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018800 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018801#ifdef FEAT_MBYTE
18802 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018803 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018804#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018805
18806 if (argvars[1].v_type != VAR_UNKNOWN)
18807 skipcc = get_tv_number_chk(&argvars[1], NULL);
18808 if (skipcc < 0 || skipcc > 1)
18809 EMSG(_(e_invarg));
18810 else
18811 {
18812#ifdef FEAT_MBYTE
18813 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18814 while (*s != NUL)
18815 {
18816 func_mb_ptr2char_adv(&s);
18817 ++len;
18818 }
18819 rettv->vval.v_number = len;
18820#else
18821 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18822#endif
18823 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018824}
18825
18826/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018827 * "strdisplaywidth()" function
18828 */
18829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018830f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018831{
18832 char_u *s = get_tv_string(&argvars[0]);
18833 int col = 0;
18834
18835 if (argvars[1].v_type != VAR_UNKNOWN)
18836 col = get_tv_number(&argvars[1]);
18837
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018838 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018839}
18840
18841/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018842 * "strwidth()" function
18843 */
18844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018845f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018846{
18847 char_u *s = get_tv_string(&argvars[0]);
18848
18849 rettv->vval.v_number = (varnumber_T)(
18850#ifdef FEAT_MBYTE
18851 mb_string2cells(s, -1)
18852#else
18853 STRLEN(s)
18854#endif
18855 );
18856}
18857
18858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 * "strpart()" function
18860 */
18861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018862f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863{
18864 char_u *p;
18865 int n;
18866 int len;
18867 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018868 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018870 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 slen = (int)STRLEN(p);
18872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018873 n = get_tv_number_chk(&argvars[1], &error);
18874 if (error)
18875 len = 0;
18876 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018877 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 else
18879 len = slen - n; /* default len: all bytes that are available. */
18880
18881 /*
18882 * Only return the overlap between the specified part and the actual
18883 * string.
18884 */
18885 if (n < 0)
18886 {
18887 len += n;
18888 n = 0;
18889 }
18890 else if (n > slen)
18891 n = slen;
18892 if (len < 0)
18893 len = 0;
18894 else if (n + len > slen)
18895 len = slen - n;
18896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018897 rettv->v_type = VAR_STRING;
18898 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899}
18900
18901/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018902 * "strridx()" function
18903 */
18904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018905f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018906{
18907 char_u buf[NUMBUFLEN];
18908 char_u *needle;
18909 char_u *haystack;
18910 char_u *rest;
18911 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018912 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018914 needle = get_tv_string_chk(&argvars[1]);
18915 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018916
18917 rettv->vval.v_number = -1;
18918 if (needle == NULL || haystack == NULL)
18919 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018920
18921 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018922 if (argvars[2].v_type != VAR_UNKNOWN)
18923 {
18924 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018925 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018926 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018927 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018928 }
18929 else
18930 end_idx = haystack_len;
18931
Bram Moolenaar0d660222005-01-07 21:51:51 +000018932 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018933 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018934 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018935 lastmatch = haystack + end_idx;
18936 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018937 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018938 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939 for (rest = haystack; *rest != '\0'; ++rest)
18940 {
18941 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018942 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018943 break;
18944 lastmatch = rest;
18945 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018946 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018947
18948 if (lastmatch == NULL)
18949 rettv->vval.v_number = -1;
18950 else
18951 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18952}
18953
18954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 * "strtrans()" function
18956 */
18957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018958f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018960 rettv->v_type = VAR_STRING;
18961 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962}
18963
18964/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018965 * "submatch()" function
18966 */
18967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018968f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969{
Bram Moolenaar41571762014-04-02 19:00:58 +020018970 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018971 int no;
18972 int retList = 0;
18973
18974 no = (int)get_tv_number_chk(&argvars[0], &error);
18975 if (error)
18976 return;
18977 error = FALSE;
18978 if (argvars[1].v_type != VAR_UNKNOWN)
18979 retList = get_tv_number_chk(&argvars[1], &error);
18980 if (error)
18981 return;
18982
18983 if (retList == 0)
18984 {
18985 rettv->v_type = VAR_STRING;
18986 rettv->vval.v_string = reg_submatch(no);
18987 }
18988 else
18989 {
18990 rettv->v_type = VAR_LIST;
18991 rettv->vval.v_list = reg_submatch_list(no);
18992 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018993}
18994
18995/*
18996 * "substitute()" function
18997 */
18998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018999f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019000{
19001 char_u patbuf[NUMBUFLEN];
19002 char_u subbuf[NUMBUFLEN];
19003 char_u flagsbuf[NUMBUFLEN];
19004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019005 char_u *str = get_tv_string_chk(&argvars[0]);
19006 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19007 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19008 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19009
Bram Moolenaar0d660222005-01-07 21:51:51 +000019010 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019011 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19012 rettv->vval.v_string = NULL;
19013 else
19014 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019015}
19016
19017/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019018 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019021f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022{
19023 int id = 0;
19024#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019025 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 long col;
19027 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019028 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019030 lnum = get_tv_lnum(argvars); /* -1 on type error */
19031 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19032 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019034 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019035 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019036 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037#endif
19038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019039 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040}
19041
19042/*
19043 * "synIDattr(id, what [, mode])" function
19044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019046f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047{
19048 char_u *p = NULL;
19049#ifdef FEAT_SYN_HL
19050 int id;
19051 char_u *what;
19052 char_u *mode;
19053 char_u modebuf[NUMBUFLEN];
19054 int modec;
19055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019056 id = get_tv_number(&argvars[0]);
19057 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019058 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019060 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019062 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 modec = 0; /* replace invalid with current */
19064 }
19065 else
19066 {
19067#ifdef FEAT_GUI
19068 if (gui.in_use)
19069 modec = 'g';
19070 else
19071#endif
19072 if (t_colors > 1)
19073 modec = 'c';
19074 else
19075 modec = 't';
19076 }
19077
19078
19079 switch (TOLOWER_ASC(what[0]))
19080 {
19081 case 'b':
19082 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19083 p = highlight_color(id, what, modec);
19084 else /* bold */
19085 p = highlight_has_attr(id, HL_BOLD, modec);
19086 break;
19087
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019088 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 p = highlight_color(id, what, modec);
19090 break;
19091
19092 case 'i':
19093 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19094 p = highlight_has_attr(id, HL_INVERSE, modec);
19095 else /* italic */
19096 p = highlight_has_attr(id, HL_ITALIC, modec);
19097 break;
19098
19099 case 'n': /* name */
19100 p = get_highlight_name(NULL, id - 1);
19101 break;
19102
19103 case 'r': /* reverse */
19104 p = highlight_has_attr(id, HL_INVERSE, modec);
19105 break;
19106
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019107 case 's':
19108 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19109 p = highlight_color(id, what, modec);
19110 else /* standout */
19111 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 break;
19113
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019114 case 'u':
19115 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19116 /* underline */
19117 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19118 else
19119 /* undercurl */
19120 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 break;
19122 }
19123
19124 if (p != NULL)
19125 p = vim_strsave(p);
19126#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019127 rettv->v_type = VAR_STRING;
19128 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129}
19130
19131/*
19132 * "synIDtrans(id)" function
19133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019135f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136{
19137 int id;
19138
19139#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019140 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141
19142 if (id > 0)
19143 id = syn_get_final_id(id);
19144 else
19145#endif
19146 id = 0;
19147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019148 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149}
19150
19151/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019152 * "synconcealed(lnum, col)" function
19153 */
19154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019155f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019156{
19157#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19158 long lnum;
19159 long col;
19160 int syntax_flags = 0;
19161 int cchar;
19162 int matchid = 0;
19163 char_u str[NUMBUFLEN];
19164#endif
19165
19166 rettv->v_type = VAR_LIST;
19167 rettv->vval.v_list = NULL;
19168
19169#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19170 lnum = get_tv_lnum(argvars); /* -1 on type error */
19171 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19172
19173 vim_memset(str, NUL, sizeof(str));
19174
19175 if (rettv_list_alloc(rettv) != FAIL)
19176 {
19177 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19178 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19179 && curwin->w_p_cole > 0)
19180 {
19181 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19182 syntax_flags = get_syntax_info(&matchid);
19183
19184 /* get the conceal character */
19185 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19186 {
19187 cchar = syn_get_sub_char();
19188 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19189 cchar = lcs_conceal;
19190 if (cchar != NUL)
19191 {
19192# ifdef FEAT_MBYTE
19193 if (has_mbyte)
19194 (*mb_char2bytes)(cchar, str);
19195 else
19196# endif
19197 str[0] = cchar;
19198 }
19199 }
19200 }
19201
19202 list_append_number(rettv->vval.v_list,
19203 (syntax_flags & HL_CONCEAL) != 0);
19204 /* -1 to auto-determine strlen */
19205 list_append_string(rettv->vval.v_list, str, -1);
19206 list_append_number(rettv->vval.v_list, matchid);
19207 }
19208#endif
19209}
19210
19211/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019212 * "synstack(lnum, col)" function
19213 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019215f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019216{
19217#ifdef FEAT_SYN_HL
19218 long lnum;
19219 long col;
19220 int i;
19221 int id;
19222#endif
19223
19224 rettv->v_type = VAR_LIST;
19225 rettv->vval.v_list = NULL;
19226
19227#ifdef FEAT_SYN_HL
19228 lnum = get_tv_lnum(argvars); /* -1 on type error */
19229 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19230
19231 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019232 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019233 && rettv_list_alloc(rettv) != FAIL)
19234 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019235 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019236 for (i = 0; ; ++i)
19237 {
19238 id = syn_get_stack_item(i);
19239 if (id < 0)
19240 break;
19241 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19242 break;
19243 }
19244 }
19245#endif
19246}
19247
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019249get_cmd_output_as_rettv(
19250 typval_T *argvars,
19251 typval_T *rettv,
19252 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019254 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019256 char_u *infile = NULL;
19257 char_u buf[NUMBUFLEN];
19258 int err = FALSE;
19259 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019260 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019261 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019263 rettv->v_type = VAR_STRING;
19264 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019265 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019266 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019267
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019268 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019269 {
19270 /*
19271 * Write the string to a temp file, to be used for input of the shell
19272 * command.
19273 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019274 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019275 {
19276 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019277 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019278 }
19279
19280 fd = mch_fopen((char *)infile, WRITEBIN);
19281 if (fd == NULL)
19282 {
19283 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019284 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019285 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019286 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019287 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019288 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19289 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019290 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019291 else
19292 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019293 size_t len;
19294
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019295 p = get_tv_string_buf_chk(&argvars[1], buf);
19296 if (p == NULL)
19297 {
19298 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019299 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019300 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019301 len = STRLEN(p);
19302 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019303 err = TRUE;
19304 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019305 if (fclose(fd) != 0)
19306 err = TRUE;
19307 if (err)
19308 {
19309 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019310 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019311 }
19312 }
19313
Bram Moolenaar52a72462014-08-29 15:53:52 +020019314 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19315 * echoes typeahead, that messes up the display. */
19316 if (!msg_silent)
19317 flags += SHELL_COOKED;
19318
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019319 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019321 int len;
19322 listitem_T *li;
19323 char_u *s = NULL;
19324 char_u *start;
19325 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019326 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327
Bram Moolenaar52a72462014-08-29 15:53:52 +020019328 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019329 if (res == NULL)
19330 goto errret;
19331
19332 list = list_alloc();
19333 if (list == NULL)
19334 goto errret;
19335
19336 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019338 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019339 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019340 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019341 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019342
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019343 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019344 if (s == NULL)
19345 goto errret;
19346
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019347 for (p = s; start < end; ++p, ++start)
19348 *p = *start == NUL ? NL : *start;
19349 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019350
19351 li = listitem_alloc();
19352 if (li == NULL)
19353 {
19354 vim_free(s);
19355 goto errret;
19356 }
19357 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019358 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019359 li->li_tv.vval.v_string = s;
19360 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019362
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019363 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019364 rettv->v_type = VAR_LIST;
19365 rettv->vval.v_list = list;
19366 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019368 else
19369 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019370 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019371#ifdef USE_CR
19372 /* translate <CR> into <NL> */
19373 if (res != NULL)
19374 {
19375 char_u *s;
19376
19377 for (s = res; *s; ++s)
19378 {
19379 if (*s == CAR)
19380 *s = NL;
19381 }
19382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383#else
19384# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019385 /* translate <CR><NL> into <NL> */
19386 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019388 char_u *s, *d;
19389
19390 d = res;
19391 for (s = res; *s; ++s)
19392 {
19393 if (s[0] == CAR && s[1] == NL)
19394 ++s;
19395 *d++ = *s;
19396 }
19397 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399# endif
19400#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019401 rettv->vval.v_string = res;
19402 res = NULL;
19403 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019404
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019405errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019406 if (infile != NULL)
19407 {
19408 mch_remove(infile);
19409 vim_free(infile);
19410 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019411 if (res != NULL)
19412 vim_free(res);
19413 if (list != NULL)
19414 list_free(list, TRUE);
19415}
19416
19417/*
19418 * "system()" function
19419 */
19420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019421f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019422{
19423 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19424}
19425
19426/*
19427 * "systemlist()" function
19428 */
19429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019430f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019431{
19432 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433}
19434
19435/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019436 * "tabpagebuflist()" function
19437 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019439f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019440{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019441#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019442 tabpage_T *tp;
19443 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019444
19445 if (argvars[0].v_type == VAR_UNKNOWN)
19446 wp = firstwin;
19447 else
19448 {
19449 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19450 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019451 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019452 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019453 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019454 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019455 for (; wp != NULL; wp = wp->w_next)
19456 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019457 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019458 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019459 }
19460#endif
19461}
19462
19463
19464/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019465 * "tabpagenr()" function
19466 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019468f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019469{
19470 int nr = 1;
19471#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019472 char_u *arg;
19473
19474 if (argvars[0].v_type != VAR_UNKNOWN)
19475 {
19476 arg = get_tv_string_chk(&argvars[0]);
19477 nr = 0;
19478 if (arg != NULL)
19479 {
19480 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019481 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019482 else
19483 EMSG2(_(e_invexpr2), arg);
19484 }
19485 }
19486 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019487 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019488#endif
19489 rettv->vval.v_number = nr;
19490}
19491
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019492
19493#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019494static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019495
19496/*
19497 * Common code for tabpagewinnr() and winnr().
19498 */
19499 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019500get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019501{
19502 win_T *twin;
19503 int nr = 1;
19504 win_T *wp;
19505 char_u *arg;
19506
19507 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19508 if (argvar->v_type != VAR_UNKNOWN)
19509 {
19510 arg = get_tv_string_chk(argvar);
19511 if (arg == NULL)
19512 nr = 0; /* type error; errmsg already given */
19513 else if (STRCMP(arg, "$") == 0)
19514 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19515 else if (STRCMP(arg, "#") == 0)
19516 {
19517 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19518 if (twin == NULL)
19519 nr = 0;
19520 }
19521 else
19522 {
19523 EMSG2(_(e_invexpr2), arg);
19524 nr = 0;
19525 }
19526 }
19527
19528 if (nr > 0)
19529 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19530 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019531 {
19532 if (wp == NULL)
19533 {
19534 /* didn't find it in this tabpage */
19535 nr = 0;
19536 break;
19537 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019538 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019539 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019540 return nr;
19541}
19542#endif
19543
19544/*
19545 * "tabpagewinnr()" function
19546 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019548f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019549{
19550 int nr = 1;
19551#ifdef FEAT_WINDOWS
19552 tabpage_T *tp;
19553
19554 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19555 if (tp == NULL)
19556 nr = 0;
19557 else
19558 nr = get_winnr(tp, &argvars[1]);
19559#endif
19560 rettv->vval.v_number = nr;
19561}
19562
19563
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019564/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019565 * "tagfiles()" function
19566 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019568f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019569{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019570 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019571 tagname_T tn;
19572 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019573
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019574 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019575 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019576 fname = alloc(MAXPATHL);
19577 if (fname == NULL)
19578 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019579
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019580 for (first = TRUE; ; first = FALSE)
19581 if (get_tagfname(&tn, first, fname) == FAIL
19582 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019583 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019584 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019585 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019586}
19587
19588/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019589 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019590 */
19591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019592f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019593{
19594 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019595
19596 tag_pattern = get_tv_string(&argvars[0]);
19597
19598 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019599 if (*tag_pattern == NUL)
19600 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019601
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019602 if (rettv_list_alloc(rettv) == OK)
19603 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019604}
19605
19606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 * "tempname()" function
19608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019610f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611{
19612 static int x = 'A';
19613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019614 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019615 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616
19617 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19618 * names. Skip 'I' and 'O', they are used for shell redirection. */
19619 do
19620 {
19621 if (x == 'Z')
19622 x = '0';
19623 else if (x == '9')
19624 x = 'A';
19625 else
19626 {
19627#ifdef EBCDIC
19628 if (x == 'I')
19629 x = 'J';
19630 else if (x == 'R')
19631 x = 'S';
19632 else
19633#endif
19634 ++x;
19635 }
19636 } while (x == 'I' || x == 'O');
19637}
19638
19639/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019640 * "test(list)" function: Just checking the walls...
19641 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019643f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019644{
19645 /* Used for unit testing. Change the code below to your liking. */
19646#if 0
19647 listitem_T *li;
19648 list_T *l;
19649 char_u *bad, *good;
19650
19651 if (argvars[0].v_type != VAR_LIST)
19652 return;
19653 l = argvars[0].vval.v_list;
19654 if (l == NULL)
19655 return;
19656 li = l->lv_first;
19657 if (li == NULL)
19658 return;
19659 bad = get_tv_string(&li->li_tv);
19660 li = li->li_next;
19661 if (li == NULL)
19662 return;
19663 good = get_tv_string(&li->li_tv);
19664 rettv->vval.v_number = test_edit_score(bad, good);
19665#endif
19666}
19667
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019668#ifdef FEAT_FLOAT
19669/*
19670 * "tan()" function
19671 */
19672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019673f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019674{
19675 float_T f;
19676
19677 rettv->v_type = VAR_FLOAT;
19678 if (get_float_arg(argvars, &f) == OK)
19679 rettv->vval.v_float = tan(f);
19680 else
19681 rettv->vval.v_float = 0.0;
19682}
19683
19684/*
19685 * "tanh()" function
19686 */
19687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019688f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019689{
19690 float_T f;
19691
19692 rettv->v_type = VAR_FLOAT;
19693 if (get_float_arg(argvars, &f) == OK)
19694 rettv->vval.v_float = tanh(f);
19695 else
19696 rettv->vval.v_float = 0.0;
19697}
19698#endif
19699
Bram Moolenaard52d9742005-08-21 22:20:28 +000019700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701 * "tolower(string)" function
19702 */
19703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019704f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705{
19706 char_u *p;
19707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019708 p = vim_strsave(get_tv_string(&argvars[0]));
19709 rettv->v_type = VAR_STRING;
19710 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711
19712 if (p != NULL)
19713 while (*p != NUL)
19714 {
19715#ifdef FEAT_MBYTE
19716 int l;
19717
19718 if (enc_utf8)
19719 {
19720 int c, lc;
19721
19722 c = utf_ptr2char(p);
19723 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019724 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 /* TODO: reallocate string when byte count changes. */
19726 if (utf_char2len(lc) == l)
19727 utf_char2bytes(lc, p);
19728 p += l;
19729 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019730 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 p += l; /* skip multi-byte character */
19732 else
19733#endif
19734 {
19735 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19736 ++p;
19737 }
19738 }
19739}
19740
19741/*
19742 * "toupper(string)" function
19743 */
19744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019745f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019747 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019748 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749}
19750
19751/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019752 * "tr(string, fromstr, tostr)" function
19753 */
19754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019755f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019756{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019757 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019758 char_u *fromstr;
19759 char_u *tostr;
19760 char_u *p;
19761#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019762 int inlen;
19763 int fromlen;
19764 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019765 int idx;
19766 char_u *cpstr;
19767 int cplen;
19768 int first = TRUE;
19769#endif
19770 char_u buf[NUMBUFLEN];
19771 char_u buf2[NUMBUFLEN];
19772 garray_T ga;
19773
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019774 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019775 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19776 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019777
19778 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019779 rettv->v_type = VAR_STRING;
19780 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019781 if (fromstr == NULL || tostr == NULL)
19782 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019783 ga_init2(&ga, (int)sizeof(char), 80);
19784
19785#ifdef FEAT_MBYTE
19786 if (!has_mbyte)
19787#endif
19788 /* not multi-byte: fromstr and tostr must be the same length */
19789 if (STRLEN(fromstr) != STRLEN(tostr))
19790 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019791#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019792error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019793#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019794 EMSG2(_(e_invarg2), fromstr);
19795 ga_clear(&ga);
19796 return;
19797 }
19798
19799 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019800 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019801 {
19802#ifdef FEAT_MBYTE
19803 if (has_mbyte)
19804 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019805 inlen = (*mb_ptr2len)(in_str);
19806 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019807 cplen = inlen;
19808 idx = 0;
19809 for (p = fromstr; *p != NUL; p += fromlen)
19810 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019811 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019812 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019813 {
19814 for (p = tostr; *p != NUL; p += tolen)
19815 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019816 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019817 if (idx-- == 0)
19818 {
19819 cplen = tolen;
19820 cpstr = p;
19821 break;
19822 }
19823 }
19824 if (*p == NUL) /* tostr is shorter than fromstr */
19825 goto error;
19826 break;
19827 }
19828 ++idx;
19829 }
19830
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019831 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019832 {
19833 /* Check that fromstr and tostr have the same number of
19834 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019835 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019836 first = FALSE;
19837 for (p = tostr; *p != NUL; p += tolen)
19838 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019839 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019840 --idx;
19841 }
19842 if (idx != 0)
19843 goto error;
19844 }
19845
Bram Moolenaarcde88542015-08-11 19:14:00 +020019846 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019847 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019848 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019849
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019850 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019851 }
19852 else
19853#endif
19854 {
19855 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019856 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019857 if (p != NULL)
19858 ga_append(&ga, tostr[p - fromstr]);
19859 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019860 ga_append(&ga, *in_str);
19861 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019862 }
19863 }
19864
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019865 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019866 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019867 ga_append(&ga, NUL);
19868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019869 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019870}
19871
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019872#ifdef FEAT_FLOAT
19873/*
19874 * "trunc({float})" function
19875 */
19876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019877f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019878{
19879 float_T f;
19880
19881 rettv->v_type = VAR_FLOAT;
19882 if (get_float_arg(argvars, &f) == OK)
19883 /* trunc() is not in C90, use floor() or ceil() instead. */
19884 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19885 else
19886 rettv->vval.v_float = 0.0;
19887}
19888#endif
19889
Bram Moolenaar8299df92004-07-10 09:47:34 +000019890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 * "type(expr)" function
19892 */
19893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019894f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019896 int n;
19897
19898 switch (argvars[0].v_type)
19899 {
19900 case VAR_NUMBER: n = 0; break;
19901 case VAR_STRING: n = 1; break;
19902 case VAR_FUNC: n = 2; break;
19903 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019904 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019905 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019906 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010019907 if (argvars[0].vval.v_number == VVAL_FALSE
19908 || argvars[0].vval.v_number == VVAL_TRUE)
19909 n = 6;
19910 else
19911 n = 7;
19912 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010019913 case VAR_JOB: n = 8; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010019914 case VAR_UNKNOWN:
19915 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
19916 n = -1;
19917 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019918 }
19919 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920}
19921
19922/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019923 * "undofile(name)" function
19924 */
19925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019926f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019927{
19928 rettv->v_type = VAR_STRING;
19929#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019930 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019931 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019932
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019933 if (*fname == NUL)
19934 {
19935 /* If there is no file name there will be no undo file. */
19936 rettv->vval.v_string = NULL;
19937 }
19938 else
19939 {
19940 char_u *ffname = FullName_save(fname, FALSE);
19941
19942 if (ffname != NULL)
19943 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19944 vim_free(ffname);
19945 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019946 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019947#else
19948 rettv->vval.v_string = NULL;
19949#endif
19950}
19951
19952/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019953 * "undotree()" function
19954 */
19955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019956f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019957{
19958 if (rettv_dict_alloc(rettv) == OK)
19959 {
19960 dict_T *dict = rettv->vval.v_dict;
19961 list_T *list;
19962
Bram Moolenaar730cde92010-06-27 05:18:54 +020019963 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019964 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019965 dict_add_nr_str(dict, "save_last",
19966 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019967 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19968 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019969 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019970
19971 list = list_alloc();
19972 if (list != NULL)
19973 {
19974 u_eval_tree(curbuf->b_u_oldhead, list);
19975 dict_add_list(dict, "entries", list);
19976 }
19977 }
19978}
19979
19980/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019981 * "values(dict)" function
19982 */
19983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019984f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019985{
19986 dict_list(argvars, rettv, 1);
19987}
19988
19989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 * "virtcol(string)" function
19991 */
19992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019993f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994{
19995 colnr_T vcol = 0;
19996 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019997 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019999 fp = var2fpos(&argvars[0], FALSE, &fnum);
20000 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20001 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 {
20003 getvvcol(curwin, fp, NULL, NULL, &vcol);
20004 ++vcol;
20005 }
20006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020007 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008}
20009
20010/*
20011 * "visualmode()" function
20012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020014f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 char_u str[2];
20017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 str[0] = curbuf->b_visual_mode_eval;
20020 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020021 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022
20023 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020024 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026}
20027
20028/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020029 * "wildmenumode()" function
20030 */
20031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020032f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020033{
20034#ifdef FEAT_WILDMENU
20035 if (wild_menu_showing)
20036 rettv->vval.v_number = 1;
20037#endif
20038}
20039
20040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 * "winbufnr(nr)" function
20042 */
20043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020044f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045{
20046 win_T *wp;
20047
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020048 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020050 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020052 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053}
20054
20055/*
20056 * "wincol()" function
20057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020059f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060{
20061 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020062 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063}
20064
20065/*
20066 * "winheight(nr)" function
20067 */
20068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020069f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070{
20071 win_T *wp;
20072
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020073 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020077 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078}
20079
20080/*
20081 * "winline()" function
20082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020084f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085{
20086 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020087 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088}
20089
20090/*
20091 * "winnr()" function
20092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020094f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095{
20096 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020097
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020099 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020101 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102}
20103
20104/*
20105 * "winrestcmd()" function
20106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020108f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109{
20110#ifdef FEAT_WINDOWS
20111 win_T *wp;
20112 int winnr = 1;
20113 garray_T ga;
20114 char_u buf[50];
20115
20116 ga_init2(&ga, (int)sizeof(char), 70);
20117 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20118 {
20119 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20120 ga_concat(&ga, buf);
20121# ifdef FEAT_VERTSPLIT
20122 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20123 ga_concat(&ga, buf);
20124# endif
20125 ++winnr;
20126 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020127 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020129 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020131 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020133 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134}
20135
20136/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020137 * "winrestview()" function
20138 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020140f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020141{
20142 dict_T *dict;
20143
20144 if (argvars[0].v_type != VAR_DICT
20145 || (dict = argvars[0].vval.v_dict) == NULL)
20146 EMSG(_(e_invarg));
20147 else
20148 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020149 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20150 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20151 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20152 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020153#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020154 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20155 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020156#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020157 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20158 {
20159 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20160 curwin->w_set_curswant = FALSE;
20161 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020162
Bram Moolenaar82c25852014-05-28 16:47:16 +020020163 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20164 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020165#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020166 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20167 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020168#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020169 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20170 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20171 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20172 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020173
20174 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020175 win_new_height(curwin, curwin->w_height);
20176# ifdef FEAT_VERTSPLIT
20177 win_new_width(curwin, W_WIDTH(curwin));
20178# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020179 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020180
Bram Moolenaarb851a962014-10-31 15:45:52 +010020181 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020182 curwin->w_topline = 1;
20183 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20184 curwin->w_topline = curbuf->b_ml.ml_line_count;
20185#ifdef FEAT_DIFF
20186 check_topfill(curwin, TRUE);
20187#endif
20188 }
20189}
20190
20191/*
20192 * "winsaveview()" function
20193 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020195f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020196{
20197 dict_T *dict;
20198
Bram Moolenaara800b422010-06-27 01:15:55 +020020199 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020200 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020201 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020202
20203 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20204 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20205#ifdef FEAT_VIRTUALEDIT
20206 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20207#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020208 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020209 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20210
20211 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20212#ifdef FEAT_DIFF
20213 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20214#endif
20215 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20216 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20217}
20218
20219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 * "winwidth(nr)" function
20221 */
20222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020223f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224{
20225 win_T *wp;
20226
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020227 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020229 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 else
20231#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020232 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235#endif
20236}
20237
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020239 * "wordcount()" function
20240 */
20241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020242f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020243{
20244 if (rettv_dict_alloc(rettv) == FAIL)
20245 return;
20246 cursor_pos_info(rettv->vval.v_dict);
20247}
20248
20249/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020250 * Write list of strings to file
20251 */
20252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020253write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020254{
20255 listitem_T *li;
20256 int c;
20257 int ret = OK;
20258 char_u *s;
20259
20260 for (li = list->lv_first; li != NULL; li = li->li_next)
20261 {
20262 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20263 {
20264 if (*s == '\n')
20265 c = putc(NUL, fd);
20266 else
20267 c = putc(*s, fd);
20268 if (c == EOF)
20269 {
20270 ret = FAIL;
20271 break;
20272 }
20273 }
20274 if (!binary || li->li_next != NULL)
20275 if (putc('\n', fd) == EOF)
20276 {
20277 ret = FAIL;
20278 break;
20279 }
20280 if (ret == FAIL)
20281 {
20282 EMSG(_(e_write));
20283 break;
20284 }
20285 }
20286 return ret;
20287}
20288
20289/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020290 * "writefile()" function
20291 */
20292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020293f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020294{
20295 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020296 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020297 char_u *fname;
20298 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020299 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020300
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020301 if (check_restricted() || check_secure())
20302 return;
20303
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020304 if (argvars[0].v_type != VAR_LIST)
20305 {
20306 EMSG2(_(e_listarg), "writefile()");
20307 return;
20308 }
20309 if (argvars[0].vval.v_list == NULL)
20310 return;
20311
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020312 if (argvars[2].v_type != VAR_UNKNOWN)
20313 {
20314 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20315 binary = TRUE;
20316 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20317 append = TRUE;
20318 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020319
20320 /* Always open the file in binary mode, library functions have a mind of
20321 * their own about CR-LF conversion. */
20322 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020323 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20324 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020325 {
20326 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20327 ret = -1;
20328 }
20329 else
20330 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020331 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20332 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020333 fclose(fd);
20334 }
20335
20336 rettv->vval.v_number = ret;
20337}
20338
20339/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020340 * "xor(expr, expr)" function
20341 */
20342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020343f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020344{
20345 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20346 ^ get_tv_number_chk(&argvars[1], NULL);
20347}
20348
20349
20350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020352 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 */
20354 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020355var2fpos(
20356 typval_T *varp,
20357 int dollar_lnum, /* TRUE when $ is last line */
20358 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020360 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020362 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363
Bram Moolenaara5525202006-03-02 22:52:09 +000020364 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020365 if (varp->v_type == VAR_LIST)
20366 {
20367 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020368 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020369 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020370 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020371
20372 l = varp->vval.v_list;
20373 if (l == NULL)
20374 return NULL;
20375
20376 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020377 pos.lnum = list_find_nr(l, 0L, &error);
20378 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020379 return NULL; /* invalid line number */
20380
20381 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020382 pos.col = list_find_nr(l, 1L, &error);
20383 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020384 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020385 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020386
20387 /* We accept "$" for the column number: last column. */
20388 li = list_find(l, 1L);
20389 if (li != NULL && li->li_tv.v_type == VAR_STRING
20390 && li->li_tv.vval.v_string != NULL
20391 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20392 pos.col = len + 1;
20393
Bram Moolenaara5525202006-03-02 22:52:09 +000020394 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020395 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020396 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020397 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020398
Bram Moolenaara5525202006-03-02 22:52:09 +000020399#ifdef FEAT_VIRTUALEDIT
20400 /* Get the virtual offset. Defaults to zero. */
20401 pos.coladd = list_find_nr(l, 2L, &error);
20402 if (error)
20403 pos.coladd = 0;
20404#endif
20405
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020406 return &pos;
20407 }
20408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020409 name = get_tv_string_chk(varp);
20410 if (name == NULL)
20411 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020412 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020414 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20415 {
20416 if (VIsual_active)
20417 return &VIsual;
20418 return &curwin->w_cursor;
20419 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020420 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020422 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20424 return NULL;
20425 return pp;
20426 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020427
20428#ifdef FEAT_VIRTUALEDIT
20429 pos.coladd = 0;
20430#endif
20431
Bram Moolenaar477933c2007-07-17 14:32:23 +000020432 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020433 {
20434 pos.col = 0;
20435 if (name[1] == '0') /* "w0": first visible line */
20436 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020437 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020438 pos.lnum = curwin->w_topline;
20439 return &pos;
20440 }
20441 else if (name[1] == '$') /* "w$": last visible line */
20442 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020443 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020444 pos.lnum = curwin->w_botline - 1;
20445 return &pos;
20446 }
20447 }
20448 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020450 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 {
20452 pos.lnum = curbuf->b_ml.ml_line_count;
20453 pos.col = 0;
20454 }
20455 else
20456 {
20457 pos.lnum = curwin->w_cursor.lnum;
20458 pos.col = (colnr_T)STRLEN(ml_get_curline());
20459 }
20460 return &pos;
20461 }
20462 return NULL;
20463}
20464
20465/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020466 * Convert list in "arg" into a position and optional file number.
20467 * When "fnump" is NULL there is no file number, only 3 items.
20468 * Note that the column is passed on as-is, the caller may want to decrement
20469 * it to use 1 for the first column.
20470 * Return FAIL when conversion is not possible, doesn't check the position for
20471 * validity.
20472 */
20473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020474list2fpos(
20475 typval_T *arg,
20476 pos_T *posp,
20477 int *fnump,
20478 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020479{
20480 list_T *l = arg->vval.v_list;
20481 long i = 0;
20482 long n;
20483
Bram Moolenaar493c1782014-05-28 14:34:46 +020020484 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20485 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020486 if (arg->v_type != VAR_LIST
20487 || l == NULL
20488 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020489 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020490 return FAIL;
20491
20492 if (fnump != NULL)
20493 {
20494 n = list_find_nr(l, i++, NULL); /* fnum */
20495 if (n < 0)
20496 return FAIL;
20497 if (n == 0)
20498 n = curbuf->b_fnum; /* current buffer */
20499 *fnump = n;
20500 }
20501
20502 n = list_find_nr(l, i++, NULL); /* lnum */
20503 if (n < 0)
20504 return FAIL;
20505 posp->lnum = n;
20506
20507 n = list_find_nr(l, i++, NULL); /* col */
20508 if (n < 0)
20509 return FAIL;
20510 posp->col = n;
20511
20512#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020513 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020514 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020515 posp->coladd = 0;
20516 else
20517 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020518#endif
20519
Bram Moolenaar493c1782014-05-28 14:34:46 +020020520 if (curswantp != NULL)
20521 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20522
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020523 return OK;
20524}
20525
20526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 * Get the length of an environment variable name.
20528 * Advance "arg" to the first character after the name.
20529 * Return 0 for error.
20530 */
20531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020532get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533{
20534 char_u *p;
20535 int len;
20536
20537 for (p = *arg; vim_isIDc(*p); ++p)
20538 ;
20539 if (p == *arg) /* no name found */
20540 return 0;
20541
20542 len = (int)(p - *arg);
20543 *arg = p;
20544 return len;
20545}
20546
20547/*
20548 * Get the length of the name of a function or internal variable.
20549 * "arg" is advanced to the first non-white character after the name.
20550 * Return 0 if something is wrong.
20551 */
20552 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020553get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554{
20555 char_u *p;
20556 int len;
20557
20558 /* Find the end of the name. */
20559 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020560 {
20561 if (*p == ':')
20562 {
20563 /* "s:" is start of "s:var", but "n:" is not and can be used in
20564 * slice "[n:]". Also "xx:" is not a namespace. */
20565 len = (int)(p - *arg);
20566 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20567 || len > 1)
20568 break;
20569 }
20570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 if (p == *arg) /* no name found */
20572 return 0;
20573
20574 len = (int)(p - *arg);
20575 *arg = skipwhite(p);
20576
20577 return len;
20578}
20579
20580/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020581 * Get the length of the name of a variable or function.
20582 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020584 * Return -1 if curly braces expansion failed.
20585 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 * If the name contains 'magic' {}'s, expand them and return the
20587 * expanded name in an allocated string via 'alias' - caller must free.
20588 */
20589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020590get_name_len(
20591 char_u **arg,
20592 char_u **alias,
20593 int evaluate,
20594 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595{
20596 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 char_u *p;
20598 char_u *expr_start;
20599 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600
20601 *alias = NULL; /* default to no alias */
20602
20603 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20604 && (*arg)[2] == (int)KE_SNR)
20605 {
20606 /* hard coded <SNR>, already translated */
20607 *arg += 3;
20608 return get_id_len(arg) + 3;
20609 }
20610 len = eval_fname_script(*arg);
20611 if (len > 0)
20612 {
20613 /* literal "<SID>", "s:" or "<SNR>" */
20614 *arg += len;
20615 }
20616
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020618 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020620 p = find_name_end(*arg, &expr_start, &expr_end,
20621 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622 if (expr_start != NULL)
20623 {
20624 char_u *temp_string;
20625
20626 if (!evaluate)
20627 {
20628 len += (int)(p - *arg);
20629 *arg = skipwhite(p);
20630 return len;
20631 }
20632
20633 /*
20634 * Include any <SID> etc in the expanded string:
20635 * Thus the -len here.
20636 */
20637 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20638 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020639 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 *alias = temp_string;
20641 *arg = skipwhite(p);
20642 return (int)STRLEN(temp_string);
20643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644
20645 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020646 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 EMSG2(_(e_invexpr2), *arg);
20648
20649 return len;
20650}
20651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020652/*
20653 * Find the end of a variable or function name, taking care of magic braces.
20654 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20655 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020656 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020657 * Return a pointer to just after the name. Equal to "arg" if there is no
20658 * valid name.
20659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020661find_name_end(
20662 char_u *arg,
20663 char_u **expr_start,
20664 char_u **expr_end,
20665 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020667 int mb_nest = 0;
20668 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020670 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020672 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020674 *expr_start = NULL;
20675 *expr_end = NULL;
20676 }
20677
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020678 /* Quick check for valid starting character. */
20679 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20680 return arg;
20681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020682 for (p = arg; *p != NUL
20683 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020684 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020685 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020686 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020687 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020688 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020689 if (*p == '\'')
20690 {
20691 /* skip over 'string' to avoid counting [ and ] inside it. */
20692 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20693 ;
20694 if (*p == NUL)
20695 break;
20696 }
20697 else if (*p == '"')
20698 {
20699 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20700 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20701 if (*p == '\\' && p[1] != NUL)
20702 ++p;
20703 if (*p == NUL)
20704 break;
20705 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020706 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20707 {
20708 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020709 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020710 len = (int)(p - arg);
20711 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020712 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020713 break;
20714 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020716 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020718 if (*p == '[')
20719 ++br_nest;
20720 else if (*p == ']')
20721 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020724 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020726 if (*p == '{')
20727 {
20728 mb_nest++;
20729 if (expr_start != NULL && *expr_start == NULL)
20730 *expr_start = p;
20731 }
20732 else if (*p == '}')
20733 {
20734 mb_nest--;
20735 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20736 *expr_end = p;
20737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 }
20740
20741 return p;
20742}
20743
20744/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020745 * Expands out the 'magic' {}'s in a variable/function name.
20746 * Note that this can call itself recursively, to deal with
20747 * constructs like foo{bar}{baz}{bam}
20748 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20749 * "in_start" ^
20750 * "expr_start" ^
20751 * "expr_end" ^
20752 * "in_end" ^
20753 *
20754 * Returns a new allocated string, which the caller must free.
20755 * Returns NULL for failure.
20756 */
20757 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020758make_expanded_name(
20759 char_u *in_start,
20760 char_u *expr_start,
20761 char_u *expr_end,
20762 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020763{
20764 char_u c1;
20765 char_u *retval = NULL;
20766 char_u *temp_result;
20767 char_u *nextcmd = NULL;
20768
20769 if (expr_end == NULL || in_end == NULL)
20770 return NULL;
20771 *expr_start = NUL;
20772 *expr_end = NUL;
20773 c1 = *in_end;
20774 *in_end = NUL;
20775
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020776 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020777 if (temp_result != NULL && nextcmd == NULL)
20778 {
20779 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20780 + (in_end - expr_end) + 1));
20781 if (retval != NULL)
20782 {
20783 STRCPY(retval, in_start);
20784 STRCAT(retval, temp_result);
20785 STRCAT(retval, expr_end + 1);
20786 }
20787 }
20788 vim_free(temp_result);
20789
20790 *in_end = c1; /* put char back for error messages */
20791 *expr_start = '{';
20792 *expr_end = '}';
20793
20794 if (retval != NULL)
20795 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020796 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020797 if (expr_start != NULL)
20798 {
20799 /* Further expansion! */
20800 temp_result = make_expanded_name(retval, expr_start,
20801 expr_end, temp_result);
20802 vim_free(retval);
20803 retval = temp_result;
20804 }
20805 }
20806
20807 return retval;
20808}
20809
20810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020812 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 */
20814 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020815eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020817 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20818}
20819
20820/*
20821 * Return TRUE if character "c" can be used as the first character in a
20822 * variable or function name (excluding '{' and '}').
20823 */
20824 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020825eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020826{
20827 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828}
20829
20830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 * Set number v: variable to "val".
20832 */
20833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020834set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020836 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837}
20838
20839/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020840 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 */
20842 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020843get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020845 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846}
20847
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020848/*
20849 * Get string v: variable value. Uses a static buffer, can only be used once.
20850 */
20851 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020852get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020853{
20854 return get_tv_string(&vimvars[idx].vv_tv);
20855}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020856
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020858 * Get List v: variable value. Caller must take care of reference count when
20859 * needed.
20860 */
20861 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020862get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020863{
20864 return vimvars[idx].vv_list;
20865}
20866
20867/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020868 * Set v:char to character "c".
20869 */
20870 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020871set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020872{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020873 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020874
20875#ifdef FEAT_MBYTE
20876 if (has_mbyte)
20877 buf[(*mb_char2bytes)(c, buf)] = NUL;
20878 else
20879#endif
20880 {
20881 buf[0] = c;
20882 buf[1] = NUL;
20883 }
20884 set_vim_var_string(VV_CHAR, buf, -1);
20885}
20886
20887/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020888 * Set v:count to "count" and v:count1 to "count1".
20889 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890 */
20891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020892set_vcount(
20893 long count,
20894 long count1,
20895 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020897 if (set_prevcount)
20898 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020899 vimvars[VV_COUNT].vv_nr = count;
20900 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901}
20902
20903/*
20904 * Set string v: variable to a copy of "val".
20905 */
20906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020907set_vim_var_string(
20908 int idx,
20909 char_u *val,
20910 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911{
Bram Moolenaara542c682016-01-31 16:28:04 +010020912 clear_tv(&vimvars[idx].vv_di.di_tv);
20913 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020915 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020917 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020919 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920}
20921
20922/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020923 * Set List v: variable to "val".
20924 */
20925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020926set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020927{
Bram Moolenaara542c682016-01-31 16:28:04 +010020928 clear_tv(&vimvars[idx].vv_di.di_tv);
20929 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020930 vimvars[idx].vv_list = val;
20931 if (val != NULL)
20932 ++val->lv_refcount;
20933}
20934
20935/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020936 * Set Dictionary v: variable to "val".
20937 */
20938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020939set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020940{
20941 int todo;
20942 hashitem_T *hi;
20943
Bram Moolenaara542c682016-01-31 16:28:04 +010020944 clear_tv(&vimvars[idx].vv_di.di_tv);
20945 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020946 vimvars[idx].vv_dict = val;
20947 if (val != NULL)
20948 {
20949 ++val->dv_refcount;
20950
20951 /* Set readonly */
20952 todo = (int)val->dv_hashtab.ht_used;
20953 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20954 {
20955 if (HASHITEM_EMPTY(hi))
20956 continue;
20957 --todo;
20958 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20959 }
20960 }
20961}
20962
20963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 * Set v:register if needed.
20965 */
20966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020967set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968{
20969 char_u regname;
20970
20971 if (c == 0 || c == ' ')
20972 regname = '"';
20973 else
20974 regname = c;
20975 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020976 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 set_vim_var_string(VV_REG, &regname, 1);
20978}
20979
20980/*
20981 * Get or set v:exception. If "oldval" == NULL, return the current value.
20982 * Otherwise, restore the value to "oldval" and return NULL.
20983 * Must always be called in pairs to save and restore v:exception! Does not
20984 * take care of memory allocations.
20985 */
20986 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020987v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988{
20989 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020990 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991
Bram Moolenaare9a41262005-01-15 22:18:47 +000020992 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 return NULL;
20994}
20995
20996/*
20997 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20998 * Otherwise, restore the value to "oldval" and return NULL.
20999 * Must always be called in pairs to save and restore v:throwpoint! Does not
21000 * take care of memory allocations.
21001 */
21002 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021003v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004{
21005 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021006 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007
Bram Moolenaare9a41262005-01-15 22:18:47 +000021008 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 return NULL;
21010}
21011
21012#if defined(FEAT_AUTOCMD) || defined(PROTO)
21013/*
21014 * Set v:cmdarg.
21015 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21016 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21017 * Must always be called in pairs!
21018 */
21019 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021020set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021{
21022 char_u *oldval;
21023 char_u *newval;
21024 unsigned len;
21025
Bram Moolenaare9a41262005-01-15 22:18:47 +000021026 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021027 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021029 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021030 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021031 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 }
21033
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021034 if (eap->force_bin == FORCE_BIN)
21035 len = 6;
21036 else if (eap->force_bin == FORCE_NOBIN)
21037 len = 8;
21038 else
21039 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021040
21041 if (eap->read_edit)
21042 len += 7;
21043
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021044 if (eap->force_ff != 0)
21045 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21046# ifdef FEAT_MBYTE
21047 if (eap->force_enc != 0)
21048 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021049 if (eap->bad_char != 0)
21050 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021051# endif
21052
21053 newval = alloc(len + 1);
21054 if (newval == NULL)
21055 return NULL;
21056
21057 if (eap->force_bin == FORCE_BIN)
21058 sprintf((char *)newval, " ++bin");
21059 else if (eap->force_bin == FORCE_NOBIN)
21060 sprintf((char *)newval, " ++nobin");
21061 else
21062 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021063
21064 if (eap->read_edit)
21065 STRCAT(newval, " ++edit");
21066
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021067 if (eap->force_ff != 0)
21068 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21069 eap->cmd + eap->force_ff);
21070# ifdef FEAT_MBYTE
21071 if (eap->force_enc != 0)
21072 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21073 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021074 if (eap->bad_char == BAD_KEEP)
21075 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21076 else if (eap->bad_char == BAD_DROP)
21077 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21078 else if (eap->bad_char != 0)
21079 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021080# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021081 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021082 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083}
21084#endif
21085
21086/*
21087 * Get the value of internal variable "name".
21088 * Return OK or FAIL.
21089 */
21090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021091get_var_tv(
21092 char_u *name,
21093 int len, /* length of "name" */
21094 typval_T *rettv, /* NULL when only checking existence */
21095 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21096 int verbose, /* may give error message */
21097 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098{
21099 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021100 typval_T *tv = NULL;
21101 typval_T atv;
21102 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104
21105 /* truncate the name, so that we can use strcmp() */
21106 cc = name[len];
21107 name[len] = NUL;
21108
21109 /*
21110 * Check for "b:changedtick".
21111 */
21112 if (STRCMP(name, "b:changedtick") == 0)
21113 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021114 atv.v_type = VAR_NUMBER;
21115 atv.vval.v_number = curbuf->b_changedtick;
21116 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 }
21118
21119 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 * Check for user-defined variables.
21121 */
21122 else
21123 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021124 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021126 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021127 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021128 if (dip != NULL)
21129 *dip = v;
21130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 }
21132
Bram Moolenaare9a41262005-01-15 22:18:47 +000021133 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021135 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021136 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 ret = FAIL;
21138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021139 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021140 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141
21142 name[len] = cc;
21143
21144 return ret;
21145}
21146
21147/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021148 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21149 * Also handle function call with Funcref variable: func(expr)
21150 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21151 */
21152 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021153handle_subscript(
21154 char_u **arg,
21155 typval_T *rettv,
21156 int evaluate, /* do more than finding the end */
21157 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021158{
21159 int ret = OK;
21160 dict_T *selfdict = NULL;
21161 char_u *s;
21162 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021163 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021164
21165 while (ret == OK
21166 && (**arg == '['
21167 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021168 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021169 && !vim_iswhite(*(*arg - 1)))
21170 {
21171 if (**arg == '(')
21172 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021173 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021174 if (evaluate)
21175 {
21176 functv = *rettv;
21177 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021178
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021179 /* Invoke the function. Recursive! */
21180 s = functv.vval.v_string;
21181 }
21182 else
21183 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021184 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021185 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21186 &len, evaluate, selfdict);
21187
21188 /* Clear the funcref afterwards, so that deleting it while
21189 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021190 if (evaluate)
21191 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021192
21193 /* Stop the expression evaluation when immediately aborting on
21194 * error, or when an interrupt occurred or an exception was thrown
21195 * but not caught. */
21196 if (aborting())
21197 {
21198 if (ret == OK)
21199 clear_tv(rettv);
21200 ret = FAIL;
21201 }
21202 dict_unref(selfdict);
21203 selfdict = NULL;
21204 }
21205 else /* **arg == '[' || **arg == '.' */
21206 {
21207 dict_unref(selfdict);
21208 if (rettv->v_type == VAR_DICT)
21209 {
21210 selfdict = rettv->vval.v_dict;
21211 if (selfdict != NULL)
21212 ++selfdict->dv_refcount;
21213 }
21214 else
21215 selfdict = NULL;
21216 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21217 {
21218 clear_tv(rettv);
21219 ret = FAIL;
21220 }
21221 }
21222 }
21223 dict_unref(selfdict);
21224 return ret;
21225}
21226
21227/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021228 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021229 * value).
21230 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021231 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021232alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021233{
Bram Moolenaar33570922005-01-25 22:26:29 +000021234 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021235}
21236
21237/*
21238 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 * The string "s" must have been allocated, it is consumed.
21240 * Return NULL for out of memory, the variable otherwise.
21241 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021242 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021243alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244{
Bram Moolenaar33570922005-01-25 22:26:29 +000021245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021247 rettv = alloc_tv();
21248 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021250 rettv->v_type = VAR_STRING;
21251 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 }
21253 else
21254 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021255 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256}
21257
21258/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021259 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021261 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021262free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263{
21264 if (varp != NULL)
21265 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021266 switch (varp->v_type)
21267 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021268 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021269 func_unref(varp->vval.v_string);
21270 /*FALLTHROUGH*/
21271 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021272 vim_free(varp->vval.v_string);
21273 break;
21274 case VAR_LIST:
21275 list_unref(varp->vval.v_list);
21276 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021277 case VAR_DICT:
21278 dict_unref(varp->vval.v_dict);
21279 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021280 case VAR_JOB:
21281#ifdef FEAT_JOB
21282 job_unref(varp->vval.v_job);
21283 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021284#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021285 case VAR_NUMBER:
21286 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021287 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021288 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021289 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 vim_free(varp);
21292 }
21293}
21294
21295/*
21296 * Free the memory for a variable value and set the value to NULL or 0.
21297 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021299clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300{
21301 if (varp != NULL)
21302 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021303 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021305 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021306 func_unref(varp->vval.v_string);
21307 /*FALLTHROUGH*/
21308 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021309 vim_free(varp->vval.v_string);
21310 varp->vval.v_string = NULL;
21311 break;
21312 case VAR_LIST:
21313 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021314 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021315 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021316 case VAR_DICT:
21317 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021318 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021319 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021320 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021321 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021322 varp->vval.v_number = 0;
21323 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021324 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021325#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021326 varp->vval.v_float = 0.0;
21327 break;
21328#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021329 case VAR_JOB:
21330#ifdef FEAT_JOB
21331 job_unref(varp->vval.v_job);
21332 varp->vval.v_job = NULL;
21333#endif
21334 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021335 case VAR_UNKNOWN:
21336 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021338 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 }
21340}
21341
21342/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021343 * Set the value of a variable to NULL without freeing items.
21344 */
21345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021346init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021347{
21348 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021349 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021350}
21351
21352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 * Get the number value of a variable.
21354 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021355 * For incompatible types, return 0.
21356 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21357 * caller of incompatible types: it sets *denote to TRUE if "denote"
21358 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 */
21360 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021361get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021363 int error = FALSE;
21364
21365 return get_tv_number_chk(varp, &error); /* return 0L on error */
21366}
21367
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021368 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021369get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021370{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021371 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021373 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021375 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021376 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021377 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021378#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021379 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021380 break;
21381#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021382 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021383 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021384 break;
21385 case VAR_STRING:
21386 if (varp->vval.v_string != NULL)
21387 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021388 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021389 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021390 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021391 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021392 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021393 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021394 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021395 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021396 case VAR_SPECIAL:
21397 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21398 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021399 case VAR_JOB:
21400#ifdef FEAT_JOB
21401 EMSG(_("E910: Using a Job as a Number"));
21402 break;
21403#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021404 case VAR_UNKNOWN:
21405 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021406 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021408 if (denote == NULL) /* useful for values that must be unsigned */
21409 n = -1;
21410 else
21411 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021412 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413}
21414
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021415#ifdef FEAT_FLOAT
21416 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021417get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021418{
21419 switch (varp->v_type)
21420 {
21421 case VAR_NUMBER:
21422 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021423 case VAR_FLOAT:
21424 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021425 case VAR_FUNC:
21426 EMSG(_("E891: Using a Funcref as a Float"));
21427 break;
21428 case VAR_STRING:
21429 EMSG(_("E892: Using a String as a Float"));
21430 break;
21431 case VAR_LIST:
21432 EMSG(_("E893: Using a List as a Float"));
21433 break;
21434 case VAR_DICT:
21435 EMSG(_("E894: Using a Dictionary as a Float"));
21436 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021437 case VAR_SPECIAL:
21438 EMSG(_("E907: Using a special value as a Float"));
21439 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021440 case VAR_JOB:
21441# ifdef FEAT_JOB
21442 EMSG(_("E911: Using a Job as a Float"));
21443 break;
21444# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021445 case VAR_UNKNOWN:
21446 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021447 break;
21448 }
21449 return 0;
21450}
21451#endif
21452
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021454 * Get the lnum from the first argument.
21455 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021456 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 */
21458 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021459get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460{
Bram Moolenaar33570922005-01-25 22:26:29 +000021461 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 linenr_T lnum;
21463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021464 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 if (lnum == 0) /* no valid number, try using line() */
21466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021467 rettv.v_type = VAR_NUMBER;
21468 f_line(argvars, &rettv);
21469 lnum = rettv.vval.v_number;
21470 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 }
21472 return lnum;
21473}
21474
21475/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021476 * Get the lnum from the first argument.
21477 * Also accepts "$", then "buf" is used.
21478 * Returns 0 on error.
21479 */
21480 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021481get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021482{
21483 if (argvars[0].v_type == VAR_STRING
21484 && argvars[0].vval.v_string != NULL
21485 && argvars[0].vval.v_string[0] == '$'
21486 && buf != NULL)
21487 return buf->b_ml.ml_line_count;
21488 return get_tv_number_chk(&argvars[0], NULL);
21489}
21490
21491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 * Get the string value of a variable.
21493 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021494 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21495 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 * If the String variable has never been set, return an empty string.
21497 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021498 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21499 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 */
21501 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021502get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503{
21504 static char_u mybuf[NUMBUFLEN];
21505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021506 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507}
21508
21509 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021510get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021512 char_u *res = get_tv_string_buf_chk(varp, buf);
21513
21514 return res != NULL ? res : (char_u *)"";
21515}
21516
Bram Moolenaar7d647822014-04-05 21:28:56 +020021517/*
21518 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21519 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021520 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021521get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021522{
21523 static char_u mybuf[NUMBUFLEN];
21524
21525 return get_tv_string_buf_chk(varp, mybuf);
21526}
21527
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021528 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021529get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021530{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021531 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021533 case VAR_NUMBER:
21534 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21535 return buf;
21536 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021537 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021538 break;
21539 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021540 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021541 break;
21542 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021543 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021544 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021545 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021546#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021547 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021548 break;
21549#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021550 case VAR_STRING:
21551 if (varp->vval.v_string != NULL)
21552 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021553 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021554 case VAR_SPECIAL:
21555 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21556 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021557 case VAR_JOB:
21558#ifdef FEAT_JOB
21559 {
21560 job_T *job = varp->vval.v_job;
21561 char *status = job->jv_status == JOB_FAILED ? "fail"
21562 : job->jv_status == JOB_ENDED ? "dead"
21563 : "run";
21564# ifdef UNIX
21565 vim_snprintf((char *)buf, NUMBUFLEN,
21566 "process %ld %s", (long)job->jv_pid, status);
21567# else
21568 /* TODO */
21569 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21570# endif
21571 return buf;
21572 }
21573#endif
21574 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021575 case VAR_UNKNOWN:
21576 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021577 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021579 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580}
21581
21582/*
21583 * Find variable "name" in the list of variables.
21584 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021585 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021586 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021587 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021589 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021590find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021593 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594
Bram Moolenaara7043832005-01-21 11:56:39 +000021595 ht = find_var_ht(name, &varname);
21596 if (htp != NULL)
21597 *htp = ht;
21598 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021600 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601}
21602
21603/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021604 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021605 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021607 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021608find_var_in_ht(
21609 hashtab_T *ht,
21610 int htname,
21611 char_u *varname,
21612 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021613{
Bram Moolenaar33570922005-01-25 22:26:29 +000021614 hashitem_T *hi;
21615
21616 if (*varname == NUL)
21617 {
21618 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021619 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021620 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021621 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021622 case 'g': return &globvars_var;
21623 case 'v': return &vimvars_var;
21624 case 'b': return &curbuf->b_bufvar;
21625 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021626#ifdef FEAT_WINDOWS
21627 case 't': return &curtab->tp_winvar;
21628#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021629 case 'l': return current_funccal == NULL
21630 ? NULL : &current_funccal->l_vars_var;
21631 case 'a': return current_funccal == NULL
21632 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021633 }
21634 return NULL;
21635 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021636
21637 hi = hash_find(ht, varname);
21638 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021639 {
21640 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021641 * worked find the variable again. Don't auto-load a script if it was
21642 * loaded already, otherwise it would be loaded every time when
21643 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021644 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021645 {
21646 /* Note: script_autoload() may make "hi" invalid. It must either
21647 * be obtained again or not used. */
21648 if (!script_autoload(varname, FALSE) || aborting())
21649 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021650 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021651 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021652 if (HASHITEM_EMPTY(hi))
21653 return NULL;
21654 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021655 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021656}
21657
21658/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021660 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021661 * Set "varname" to the start of name without ':'.
21662 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021663 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021664find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021666 hashitem_T *hi;
21667
Bram Moolenaar73627d02015-08-11 15:46:09 +020021668 if (name[0] == NUL)
21669 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 if (name[1] != ':')
21671 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021672 /* The name must not start with a colon or #. */
21673 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 return NULL;
21675 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021676
21677 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021678 hi = hash_find(&compat_hashtab, name);
21679 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021680 return &compat_hashtab;
21681
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021684 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 }
21686 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021687 if (*name == 'g') /* global variable */
21688 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021689 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21690 */
21691 if (vim_strchr(name + 2, ':') != NULL
21692 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021693 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021695 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021697 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021698#ifdef FEAT_WINDOWS
21699 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021700 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021701#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021702 if (*name == 'v') /* v: variable */
21703 return &vimvarht;
21704 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021705 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021706 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021707 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 if (*name == 's' /* script variable */
21709 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21710 return &SCRIPT_VARS(current_SID);
21711 return NULL;
21712}
21713
21714/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021715 * Get function call environment based on bactrace debug level
21716 */
21717 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021718get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021719{
21720 int i;
21721 funccall_T *funccal;
21722 funccall_T *temp_funccal;
21723
21724 funccal = current_funccal;
21725 if (debug_backtrace_level > 0)
21726 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021727 for (i = 0; i < debug_backtrace_level; i++)
21728 {
21729 temp_funccal = funccal->caller;
21730 if (temp_funccal)
21731 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021732 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021733 /* backtrace level overflow. reset to max */
21734 debug_backtrace_level = i;
21735 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021736 }
21737 return funccal;
21738}
21739
21740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021742 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 * Returns NULL when it doesn't exist.
21744 */
21745 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021746get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747{
Bram Moolenaar33570922005-01-25 22:26:29 +000021748 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021750 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751 if (v == NULL)
21752 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021753 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754}
21755
21756/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 * sourcing this script and when executing functions defined in the script.
21759 */
21760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021761new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762{
Bram Moolenaara7043832005-01-21 11:56:39 +000021763 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021764 hashtab_T *ht;
21765 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021766
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21768 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021769 /* Re-allocating ga_data means that an ht_array pointing to
21770 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021771 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021772 for (i = 1; i <= ga_scripts.ga_len; ++i)
21773 {
21774 ht = &SCRIPT_VARS(i);
21775 if (ht->ht_mask == HT_INIT_SIZE - 1)
21776 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021777 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021778 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021779 }
21780
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 while (ga_scripts.ga_len < id)
21782 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021783 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021784 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021785 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 }
21788 }
21789}
21790
21791/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21793 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 */
21795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021796init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797{
Bram Moolenaar33570922005-01-25 22:26:29 +000021798 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021799 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021800 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021801 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021802 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021803 dict_var->di_tv.vval.v_dict = dict;
21804 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021805 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021806 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21807 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808}
21809
21810/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021811 * Unreference a dictionary initialized by init_var_dict().
21812 */
21813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021814unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021815{
21816 /* Now the dict needs to be freed if no one else is using it, go back to
21817 * normal reference counting. */
21818 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21819 dict_unref(dict);
21820}
21821
21822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021824 * Frees all allocated variables and the value they contain.
21825 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826 */
21827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021828vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021829{
21830 vars_clear_ext(ht, TRUE);
21831}
21832
21833/*
21834 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21835 */
21836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021837vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838{
Bram Moolenaara7043832005-01-21 11:56:39 +000021839 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021840 hashitem_T *hi;
21841 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021844 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021845 for (hi = ht->ht_array; todo > 0; ++hi)
21846 {
21847 if (!HASHITEM_EMPTY(hi))
21848 {
21849 --todo;
21850
Bram Moolenaar33570922005-01-25 22:26:29 +000021851 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021852 * ht_array might change then. hash_clear() takes care of it
21853 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021854 v = HI2DI(hi);
21855 if (free_val)
21856 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021857 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021858 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021859 }
21860 }
21861 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021862 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863}
21864
Bram Moolenaara7043832005-01-21 11:56:39 +000021865/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021866 * Delete a variable from hashtab "ht" at item "hi".
21867 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021870delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871{
Bram Moolenaar33570922005-01-25 22:26:29 +000021872 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021873
21874 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 clear_tv(&di->di_tv);
21876 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877}
21878
21879/*
21880 * List the value of one internal variable.
21881 */
21882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021883list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021885 char_u *tofree;
21886 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021887 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021888
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021889 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021890 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021891 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021892 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893}
21894
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021896list_one_var_a(
21897 char_u *prefix,
21898 char_u *name,
21899 int type,
21900 char_u *string,
21901 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902{
Bram Moolenaar31859182007-08-14 20:41:13 +000021903 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21904 msg_start();
21905 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 if (name != NULL) /* "a:" vars don't have a name stored */
21907 msg_puts(name);
21908 msg_putchar(' ');
21909 msg_advance(22);
21910 if (type == VAR_NUMBER)
21911 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021912 else if (type == VAR_FUNC)
21913 msg_putchar('*');
21914 else if (type == VAR_LIST)
21915 {
21916 msg_putchar('[');
21917 if (*string == '[')
21918 ++string;
21919 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021920 else if (type == VAR_DICT)
21921 {
21922 msg_putchar('{');
21923 if (*string == '{')
21924 ++string;
21925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 else
21927 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021928
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021930
21931 if (type == VAR_FUNC)
21932 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021933 if (*first)
21934 {
21935 msg_clr_eos();
21936 *first = FALSE;
21937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938}
21939
21940/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021941 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 * If the variable already exists, the value is updated.
21943 * Otherwise the variable is created.
21944 */
21945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021946set_var(
21947 char_u *name,
21948 typval_T *tv,
21949 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950{
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021953 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021955 ht = find_var_ht(name, &varname);
21956 if (ht == NULL || *varname == NUL)
21957 {
21958 EMSG2(_(e_illvar), name);
21959 return;
21960 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021961 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021962
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021963 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21964 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021965
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021968 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021969 if (var_check_ro(v->di_flags, name, FALSE)
21970 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021971 return;
21972 if (v->di_tv.v_type != tv->v_type
21973 && !((v->di_tv.v_type == VAR_STRING
21974 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021975 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021976 || tv->v_type == VAR_NUMBER))
21977#ifdef FEAT_FLOAT
21978 && !((v->di_tv.v_type == VAR_NUMBER
21979 || v->di_tv.v_type == VAR_FLOAT)
21980 && (tv->v_type == VAR_NUMBER
21981 || tv->v_type == VAR_FLOAT))
21982#endif
21983 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021984 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021985 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021986 return;
21987 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021988
21989 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021990 * Handle setting internal v: variables separately where needed to
21991 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021992 */
21993 if (ht == &vimvarht)
21994 {
21995 if (v->di_tv.v_type == VAR_STRING)
21996 {
21997 vim_free(v->di_tv.vval.v_string);
21998 if (copy || tv->v_type != VAR_STRING)
21999 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22000 else
22001 {
22002 /* Take over the string to avoid an extra alloc/free. */
22003 v->di_tv.vval.v_string = tv->vval.v_string;
22004 tv->vval.v_string = NULL;
22005 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022006 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022007 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022008 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022009 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022011 if (STRCMP(varname, "searchforward") == 0)
22012 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022013#ifdef FEAT_SEARCH_EXTRA
22014 else if (STRCMP(varname, "hlsearch") == 0)
22015 {
22016 no_hlsearch = !v->di_tv.vval.v_number;
22017 redraw_all_later(SOME_VALID);
22018 }
22019#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022020 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022021 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022022 else if (v->di_tv.v_type != tv->v_type)
22023 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022024 }
22025
22026 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 }
22028 else /* add a new variable */
22029 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022030 /* Can't add "v:" variable. */
22031 if (ht == &vimvarht)
22032 {
22033 EMSG2(_(e_illvar), name);
22034 return;
22035 }
22036
Bram Moolenaar92124a32005-06-17 22:03:40 +000022037 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022038 if (!valid_varname(varname))
22039 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022040
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022041 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22042 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022043 if (v == NULL)
22044 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022045 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022046 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022048 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 return;
22050 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022051 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022053
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022054 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022055 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022056 else
22057 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022058 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022059 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022060 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062}
22063
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022064/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022065 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022066 * Also give an error message.
22067 */
22068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022069var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022070{
22071 if (flags & DI_FLAGS_RO)
22072 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022073 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022074 return TRUE;
22075 }
22076 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22077 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022078 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022079 return TRUE;
22080 }
22081 return FALSE;
22082}
22083
22084/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022085 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22086 * Also give an error message.
22087 */
22088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022089var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022090{
22091 if (flags & DI_FLAGS_FIX)
22092 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022093 EMSG2(_("E795: Cannot delete variable %s"),
22094 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022095 return TRUE;
22096 }
22097 return FALSE;
22098}
22099
22100/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022101 * Check if a funcref is assigned to a valid variable name.
22102 * Return TRUE and give an error if not.
22103 */
22104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022105var_check_func_name(
22106 char_u *name, /* points to start of variable name */
22107 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022108{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022109 /* Allow for w: b: s: and t:. */
22110 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022111 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22112 ? name[2] : name[0]))
22113 {
22114 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22115 name);
22116 return TRUE;
22117 }
22118 /* Don't allow hiding a function. When "v" is not NULL we might be
22119 * assigning another function to the same var, the type is checked
22120 * below. */
22121 if (new_var && function_exists(name))
22122 {
22123 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22124 name);
22125 return TRUE;
22126 }
22127 return FALSE;
22128}
22129
22130/*
22131 * Check if a variable name is valid.
22132 * Return FALSE and give an error if not.
22133 */
22134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022135valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022136{
22137 char_u *p;
22138
22139 for (p = varname; *p != NUL; ++p)
22140 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22141 && *p != AUTOLOAD_CHAR)
22142 {
22143 EMSG2(_(e_illvar), varname);
22144 return FALSE;
22145 }
22146 return TRUE;
22147}
22148
22149/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022150 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022151 * Also give an error message, using "name" or _("name") when use_gettext is
22152 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022153 */
22154 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022155tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022156{
22157 if (lock & VAR_LOCKED)
22158 {
22159 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022160 name == NULL ? (char_u *)_("Unknown")
22161 : use_gettext ? (char_u *)_(name)
22162 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022163 return TRUE;
22164 }
22165 if (lock & VAR_FIXED)
22166 {
22167 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022168 name == NULL ? (char_u *)_("Unknown")
22169 : use_gettext ? (char_u *)_(name)
22170 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022171 return TRUE;
22172 }
22173 return FALSE;
22174}
22175
22176/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022177 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022178 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022179 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022180 * It is OK for "from" and "to" to point to the same item. This is used to
22181 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022182 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022183 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022184copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022186 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022187 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022188 switch (from->v_type)
22189 {
22190 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022191 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022192 to->vval.v_number = from->vval.v_number;
22193 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022194 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022195#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022196 to->vval.v_float = from->vval.v_float;
22197 break;
22198#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022199 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022200#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022201 to->vval.v_job = from->vval.v_job;
22202 ++to->vval.v_job->jv_refcount;
22203 break;
22204#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022205 case VAR_STRING:
22206 case VAR_FUNC:
22207 if (from->vval.v_string == NULL)
22208 to->vval.v_string = NULL;
22209 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022210 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022211 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022212 if (from->v_type == VAR_FUNC)
22213 func_ref(to->vval.v_string);
22214 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022215 break;
22216 case VAR_LIST:
22217 if (from->vval.v_list == NULL)
22218 to->vval.v_list = NULL;
22219 else
22220 {
22221 to->vval.v_list = from->vval.v_list;
22222 ++to->vval.v_list->lv_refcount;
22223 }
22224 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022225 case VAR_DICT:
22226 if (from->vval.v_dict == NULL)
22227 to->vval.v_dict = NULL;
22228 else
22229 {
22230 to->vval.v_dict = from->vval.v_dict;
22231 ++to->vval.v_dict->dv_refcount;
22232 }
22233 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022234 case VAR_UNKNOWN:
22235 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022236 break;
22237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238}
22239
22240/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022241 * Make a copy of an item.
22242 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022243 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22244 * reference to an already copied list/dict can be used.
22245 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022246 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022248item_copy(
22249 typval_T *from,
22250 typval_T *to,
22251 int deep,
22252 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022253{
22254 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022255 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022256
Bram Moolenaar33570922005-01-25 22:26:29 +000022257 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022258 {
22259 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022260 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022261 }
22262 ++recurse;
22263
22264 switch (from->v_type)
22265 {
22266 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022267 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022268 case VAR_STRING:
22269 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022270 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022271 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022272 copy_tv(from, to);
22273 break;
22274 case VAR_LIST:
22275 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022276 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022277 if (from->vval.v_list == NULL)
22278 to->vval.v_list = NULL;
22279 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22280 {
22281 /* use the copy made earlier */
22282 to->vval.v_list = from->vval.v_list->lv_copylist;
22283 ++to->vval.v_list->lv_refcount;
22284 }
22285 else
22286 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22287 if (to->vval.v_list == NULL)
22288 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022289 break;
22290 case VAR_DICT:
22291 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022292 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022293 if (from->vval.v_dict == NULL)
22294 to->vval.v_dict = NULL;
22295 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22296 {
22297 /* use the copy made earlier */
22298 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22299 ++to->vval.v_dict->dv_refcount;
22300 }
22301 else
22302 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22303 if (to->vval.v_dict == NULL)
22304 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022305 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022306 case VAR_UNKNOWN:
22307 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022308 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022309 }
22310 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022311 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022312}
22313
22314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 * ":echo expr1 ..." print each argument separated with a space, add a
22316 * newline at the end.
22317 * ":echon expr1 ..." print each argument plain.
22318 */
22319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022320ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321{
22322 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022323 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022324 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325 char_u *p;
22326 int needclr = TRUE;
22327 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022328 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329
22330 if (eap->skip)
22331 ++emsg_skip;
22332 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22333 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022334 /* If eval1() causes an error message the text from the command may
22335 * still need to be cleared. E.g., "echo 22,44". */
22336 need_clr_eos = needclr;
22337
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022339 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 {
22341 /*
22342 * Report the invalid expression unless the expression evaluation
22343 * has been cancelled due to an aborting error, an interrupt, or an
22344 * exception.
22345 */
22346 if (!aborting())
22347 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022348 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 break;
22350 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022351 need_clr_eos = FALSE;
22352
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 if (!eap->skip)
22354 {
22355 if (atstart)
22356 {
22357 atstart = FALSE;
22358 /* Call msg_start() after eval1(), evaluating the expression
22359 * may cause a message to appear. */
22360 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022361 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022362 /* Mark the saved text as finishing the line, so that what
22363 * follows is displayed on a new line when scrolling back
22364 * at the more prompt. */
22365 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 }
22369 else if (eap->cmdidx == CMD_echo)
22370 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022371 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022372 if (p != NULL)
22373 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022375 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022377 if (*p != TAB && needclr)
22378 {
22379 /* remove any text still there from the command */
22380 msg_clr_eos();
22381 needclr = FALSE;
22382 }
22383 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 }
22385 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022386 {
22387#ifdef FEAT_MBYTE
22388 if (has_mbyte)
22389 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022390 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022391
22392 (void)msg_outtrans_len_attr(p, i, echo_attr);
22393 p += i - 1;
22394 }
22395 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022396#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022397 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022400 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022402 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 arg = skipwhite(arg);
22404 }
22405 eap->nextcmd = check_nextcmd(arg);
22406
22407 if (eap->skip)
22408 --emsg_skip;
22409 else
22410 {
22411 /* remove text that may still be there from the command */
22412 if (needclr)
22413 msg_clr_eos();
22414 if (eap->cmdidx == CMD_echo)
22415 msg_end();
22416 }
22417}
22418
22419/*
22420 * ":echohl {name}".
22421 */
22422 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022423ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424{
22425 int id;
22426
22427 id = syn_name2id(eap->arg);
22428 if (id == 0)
22429 echo_attr = 0;
22430 else
22431 echo_attr = syn_id2attr(id);
22432}
22433
22434/*
22435 * ":execute expr1 ..." execute the result of an expression.
22436 * ":echomsg expr1 ..." Print a message
22437 * ":echoerr expr1 ..." Print an error
22438 * Each gets spaces around each argument and a newline at the end for
22439 * echo commands
22440 */
22441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022442ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443{
22444 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022445 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 int ret = OK;
22447 char_u *p;
22448 garray_T ga;
22449 int len;
22450 int save_did_emsg;
22451
22452 ga_init2(&ga, 1, 80);
22453
22454 if (eap->skip)
22455 ++emsg_skip;
22456 while (*arg != NUL && *arg != '|' && *arg != '\n')
22457 {
22458 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022459 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460 {
22461 /*
22462 * Report the invalid expression unless the expression evaluation
22463 * has been cancelled due to an aborting error, an interrupt, or an
22464 * exception.
22465 */
22466 if (!aborting())
22467 EMSG2(_(e_invexpr2), p);
22468 ret = FAIL;
22469 break;
22470 }
22471
22472 if (!eap->skip)
22473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022474 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 len = (int)STRLEN(p);
22476 if (ga_grow(&ga, len + 2) == FAIL)
22477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022478 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 ret = FAIL;
22480 break;
22481 }
22482 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 ga.ga_len += len;
22486 }
22487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022488 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 arg = skipwhite(arg);
22490 }
22491
22492 if (ret != FAIL && ga.ga_data != NULL)
22493 {
22494 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022497 out_flush();
22498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 else if (eap->cmdidx == CMD_echoerr)
22500 {
22501 /* We don't want to abort following commands, restore did_emsg. */
22502 save_did_emsg = did_emsg;
22503 EMSG((char_u *)ga.ga_data);
22504 if (!force_abort)
22505 did_emsg = save_did_emsg;
22506 }
22507 else if (eap->cmdidx == CMD_execute)
22508 do_cmdline((char_u *)ga.ga_data,
22509 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22510 }
22511
22512 ga_clear(&ga);
22513
22514 if (eap->skip)
22515 --emsg_skip;
22516
22517 eap->nextcmd = check_nextcmd(arg);
22518}
22519
22520/*
22521 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22522 * "arg" points to the "&" or '+' when called, to "option" when returning.
22523 * Returns NULL when no option name found. Otherwise pointer to the char
22524 * after the option name.
22525 */
22526 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022527find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528{
22529 char_u *p = *arg;
22530
22531 ++p;
22532 if (*p == 'g' && p[1] == ':')
22533 {
22534 *opt_flags = OPT_GLOBAL;
22535 p += 2;
22536 }
22537 else if (*p == 'l' && p[1] == ':')
22538 {
22539 *opt_flags = OPT_LOCAL;
22540 p += 2;
22541 }
22542 else
22543 *opt_flags = 0;
22544
22545 if (!ASCII_ISALPHA(*p))
22546 return NULL;
22547 *arg = p;
22548
22549 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22550 p += 4; /* termcap option */
22551 else
22552 while (ASCII_ISALPHA(*p))
22553 ++p;
22554 return p;
22555}
22556
22557/*
22558 * ":function"
22559 */
22560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022561ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562{
22563 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022564 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 int j;
22566 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022568 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 char_u *name = NULL;
22570 char_u *p;
22571 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022572 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 garray_T newargs;
22574 garray_T newlines;
22575 int varargs = FALSE;
22576 int mustend = FALSE;
22577 int flags = 0;
22578 ufunc_T *fp;
22579 int indent;
22580 int nesting;
22581 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022582 dictitem_T *v;
22583 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022584 static int func_nr = 0; /* number for nameless function */
22585 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022586 hashtab_T *ht;
22587 int todo;
22588 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022589 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590
22591 /*
22592 * ":function" without argument: list functions.
22593 */
22594 if (ends_excmd(*eap->arg))
22595 {
22596 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022597 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022598 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022599 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022600 {
22601 if (!HASHITEM_EMPTY(hi))
22602 {
22603 --todo;
22604 fp = HI2UF(hi);
22605 if (!isdigit(*fp->uf_name))
22606 list_func_head(fp, FALSE);
22607 }
22608 }
22609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 eap->nextcmd = check_nextcmd(eap->arg);
22611 return;
22612 }
22613
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022614 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022615 * ":function /pat": list functions matching pattern.
22616 */
22617 if (*eap->arg == '/')
22618 {
22619 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22620 if (!eap->skip)
22621 {
22622 regmatch_T regmatch;
22623
22624 c = *p;
22625 *p = NUL;
22626 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22627 *p = c;
22628 if (regmatch.regprog != NULL)
22629 {
22630 regmatch.rm_ic = p_ic;
22631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022632 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022633 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22634 {
22635 if (!HASHITEM_EMPTY(hi))
22636 {
22637 --todo;
22638 fp = HI2UF(hi);
22639 if (!isdigit(*fp->uf_name)
22640 && vim_regexec(&regmatch, fp->uf_name, 0))
22641 list_func_head(fp, FALSE);
22642 }
22643 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022644 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022645 }
22646 }
22647 if (*p == '/')
22648 ++p;
22649 eap->nextcmd = check_nextcmd(p);
22650 return;
22651 }
22652
22653 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022654 * Get the function name. There are these situations:
22655 * func normal function name
22656 * "name" == func, "fudi.fd_dict" == NULL
22657 * dict.func new dictionary entry
22658 * "name" == NULL, "fudi.fd_dict" set,
22659 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22660 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022661 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022662 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22663 * dict.func existing dict entry that's not a Funcref
22664 * "name" == NULL, "fudi.fd_dict" set,
22665 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022666 * s:func script-local function name
22667 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022670 name = trans_function_name(&p, eap->skip, 0, &fudi);
22671 paren = (vim_strchr(p, '(') != NULL);
22672 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 {
22674 /*
22675 * Return on an invalid expression in braces, unless the expression
22676 * evaluation has been cancelled due to an aborting error, an
22677 * interrupt, or an exception.
22678 */
22679 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022680 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022681 if (!eap->skip && fudi.fd_newkey != NULL)
22682 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022683 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 else
22687 eap->skip = TRUE;
22688 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022689
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 /* An error in a function call during evaluation of an expression in magic
22691 * braces should not cause the function not to be defined. */
22692 saved_did_emsg = did_emsg;
22693 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694
22695 /*
22696 * ":function func" with only function name: list function.
22697 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022698 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 {
22700 if (!ends_excmd(*skipwhite(p)))
22701 {
22702 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022703 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 }
22705 eap->nextcmd = check_nextcmd(p);
22706 if (eap->nextcmd != NULL)
22707 *p = NUL;
22708 if (!eap->skip && !got_int)
22709 {
22710 fp = find_func(name);
22711 if (fp != NULL)
22712 {
22713 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022714 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022716 if (FUNCLINE(fp, j) == NULL)
22717 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 msg_putchar('\n');
22719 msg_outnum((long)(j + 1));
22720 if (j < 9)
22721 msg_putchar(' ');
22722 if (j < 99)
22723 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022724 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 out_flush(); /* show a line at a time */
22726 ui_breakcheck();
22727 }
22728 if (!got_int)
22729 {
22730 msg_putchar('\n');
22731 msg_puts((char_u *)" endfunction");
22732 }
22733 }
22734 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022735 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022737 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 }
22739
22740 /*
22741 * ":function name(arg1, arg2)" Define function.
22742 */
22743 p = skipwhite(p);
22744 if (*p != '(')
22745 {
22746 if (!eap->skip)
22747 {
22748 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022749 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 }
22751 /* attempt to continue by skipping some text */
22752 if (vim_strchr(p, '(') != NULL)
22753 p = vim_strchr(p, '(');
22754 }
22755 p = skipwhite(p + 1);
22756
22757 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22758 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22759
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022760 if (!eap->skip)
22761 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022762 /* Check the name of the function. Unless it's a dictionary function
22763 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022764 if (name != NULL)
22765 arg = name;
22766 else
22767 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022768 if (arg != NULL && (fudi.fd_di == NULL
22769 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022770 {
22771 if (*arg == K_SPECIAL)
22772 j = 3;
22773 else
22774 j = 0;
22775 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22776 : eval_isnamec(arg[j])))
22777 ++j;
22778 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022779 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022780 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022781 /* Disallow using the g: dict. */
22782 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22783 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022784 }
22785
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 /*
22787 * Isolate the arguments: "arg1, arg2, ...)"
22788 */
22789 while (*p != ')')
22790 {
22791 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22792 {
22793 varargs = TRUE;
22794 p += 3;
22795 mustend = TRUE;
22796 }
22797 else
22798 {
22799 arg = p;
22800 while (ASCII_ISALNUM(*p) || *p == '_')
22801 ++p;
22802 if (arg == p || isdigit(*arg)
22803 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22804 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22805 {
22806 if (!eap->skip)
22807 EMSG2(_("E125: Illegal argument: %s"), arg);
22808 break;
22809 }
22810 if (ga_grow(&newargs, 1) == FAIL)
22811 goto erret;
22812 c = *p;
22813 *p = NUL;
22814 arg = vim_strsave(arg);
22815 if (arg == NULL)
22816 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022817
22818 /* Check for duplicate argument name. */
22819 for (i = 0; i < newargs.ga_len; ++i)
22820 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22821 {
22822 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022823 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022824 goto erret;
22825 }
22826
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22828 *p = c;
22829 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 if (*p == ',')
22831 ++p;
22832 else
22833 mustend = TRUE;
22834 }
22835 p = skipwhite(p);
22836 if (mustend && *p != ')')
22837 {
22838 if (!eap->skip)
22839 EMSG2(_(e_invarg2), eap->arg);
22840 break;
22841 }
22842 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022843 if (*p != ')')
22844 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 ++p; /* skip the ')' */
22846
Bram Moolenaare9a41262005-01-15 22:18:47 +000022847 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 for (;;)
22849 {
22850 p = skipwhite(p);
22851 if (STRNCMP(p, "range", 5) == 0)
22852 {
22853 flags |= FC_RANGE;
22854 p += 5;
22855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022856 else if (STRNCMP(p, "dict", 4) == 0)
22857 {
22858 flags |= FC_DICT;
22859 p += 4;
22860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861 else if (STRNCMP(p, "abort", 5) == 0)
22862 {
22863 flags |= FC_ABORT;
22864 p += 5;
22865 }
22866 else
22867 break;
22868 }
22869
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022870 /* When there is a line break use what follows for the function body.
22871 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22872 if (*p == '\n')
22873 line_arg = p + 1;
22874 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 EMSG(_(e_trailing));
22876
22877 /*
22878 * Read the body of the function, until ":endfunction" is found.
22879 */
22880 if (KeyTyped)
22881 {
22882 /* Check if the function already exists, don't let the user type the
22883 * whole function before telling him it doesn't work! For a script we
22884 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022885 if (!eap->skip && !eap->forceit)
22886 {
22887 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22888 EMSG(_(e_funcdict));
22889 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022890 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022893 if (!eap->skip && did_emsg)
22894 goto erret;
22895
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 msg_putchar('\n'); /* don't overwrite the function name */
22897 cmdline_row = msg_row;
22898 }
22899
22900 indent = 2;
22901 nesting = 0;
22902 for (;;)
22903 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022904 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022905 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022906 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022907 saved_wait_return = FALSE;
22908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022910 sourcing_lnum_off = sourcing_lnum;
22911
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022912 if (line_arg != NULL)
22913 {
22914 /* Use eap->arg, split up in parts by line breaks. */
22915 theline = line_arg;
22916 p = vim_strchr(theline, '\n');
22917 if (p == NULL)
22918 line_arg += STRLEN(line_arg);
22919 else
22920 {
22921 *p = NUL;
22922 line_arg = p + 1;
22923 }
22924 }
22925 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 theline = getcmdline(':', 0L, indent);
22927 else
22928 theline = eap->getline(':', eap->cookie, indent);
22929 if (KeyTyped)
22930 lines_left = Rows - 1;
22931 if (theline == NULL)
22932 {
22933 EMSG(_("E126: Missing :endfunction"));
22934 goto erret;
22935 }
22936
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022937 /* Detect line continuation: sourcing_lnum increased more than one. */
22938 if (sourcing_lnum > sourcing_lnum_off + 1)
22939 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22940 else
22941 sourcing_lnum_off = 0;
22942
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 if (skip_until != NULL)
22944 {
22945 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22946 * don't check for ":endfunc". */
22947 if (STRCMP(theline, skip_until) == 0)
22948 {
22949 vim_free(skip_until);
22950 skip_until = NULL;
22951 }
22952 }
22953 else
22954 {
22955 /* skip ':' and blanks*/
22956 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22957 ;
22958
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022959 /* Check for "endfunction". */
22960 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022962 if (line_arg == NULL)
22963 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 break;
22965 }
22966
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022967 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 * at "end". */
22969 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22970 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022971 else if (STRNCMP(p, "if", 2) == 0
22972 || STRNCMP(p, "wh", 2) == 0
22973 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 || STRNCMP(p, "try", 3) == 0)
22975 indent += 2;
22976
22977 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022978 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022980 if (*p == '!')
22981 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022983 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22984 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022986 ++nesting;
22987 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 }
22989 }
22990
22991 /* Check for ":append" or ":insert". */
22992 p = skip_range(p, NULL);
22993 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22994 || (p[0] == 'i'
22995 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22996 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22997 skip_until = vim_strsave((char_u *)".");
22998
22999 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23000 arg = skipwhite(skiptowhite(p));
23001 if (arg[0] == '<' && arg[1] =='<'
23002 && ((p[0] == 'p' && p[1] == 'y'
23003 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23004 || (p[0] == 'p' && p[1] == 'e'
23005 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23006 || (p[0] == 't' && p[1] == 'c'
23007 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023008 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23009 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23011 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023012 || (p[0] == 'm' && p[1] == 'z'
23013 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 ))
23015 {
23016 /* ":python <<" continues until a dot, like ":append" */
23017 p = skipwhite(arg + 2);
23018 if (*p == NUL)
23019 skip_until = vim_strsave((char_u *)".");
23020 else
23021 skip_until = vim_strsave(p);
23022 }
23023 }
23024
23025 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023026 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023027 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023028 if (line_arg == NULL)
23029 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023031 }
23032
23033 /* Copy the line to newly allocated memory. get_one_sourceline()
23034 * allocates 250 bytes per line, this saves 80% on average. The cost
23035 * is an extra alloc/free. */
23036 p = vim_strsave(theline);
23037 if (p != NULL)
23038 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023039 if (line_arg == NULL)
23040 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023041 theline = p;
23042 }
23043
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023044 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23045
23046 /* Add NULL lines for continuation lines, so that the line count is
23047 * equal to the index in the growarray. */
23048 while (sourcing_lnum_off-- > 0)
23049 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023050
23051 /* Check for end of eap->arg. */
23052 if (line_arg != NULL && *line_arg == NUL)
23053 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 }
23055
23056 /* Don't define the function when skipping commands or when an error was
23057 * detected. */
23058 if (eap->skip || did_emsg)
23059 goto erret;
23060
23061 /*
23062 * If there are no errors, add the function
23063 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023064 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023065 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023066 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023067 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023068 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023069 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023070 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023071 goto erret;
23072 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023074 fp = find_func(name);
23075 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023077 if (!eap->forceit)
23078 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023079 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023080 goto erret;
23081 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023082 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023083 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023084 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023085 name);
23086 goto erret;
23087 }
23088 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023089 ga_clear_strings(&(fp->uf_args));
23090 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023091 vim_free(name);
23092 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 }
23095 else
23096 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023097 char numbuf[20];
23098
23099 fp = NULL;
23100 if (fudi.fd_newkey == NULL && !eap->forceit)
23101 {
23102 EMSG(_(e_funcdict));
23103 goto erret;
23104 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023105 if (fudi.fd_di == NULL)
23106 {
23107 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023108 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023109 goto erret;
23110 }
23111 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023112 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023113 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023114
23115 /* Give the function a sequential number. Can only be used with a
23116 * Funcref! */
23117 vim_free(name);
23118 sprintf(numbuf, "%d", ++func_nr);
23119 name = vim_strsave((char_u *)numbuf);
23120 if (name == NULL)
23121 goto erret;
23122 }
23123
23124 if (fp == NULL)
23125 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023126 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023127 {
23128 int slen, plen;
23129 char_u *scriptname;
23130
23131 /* Check that the autoload name matches the script name. */
23132 j = FAIL;
23133 if (sourcing_name != NULL)
23134 {
23135 scriptname = autoload_name(name);
23136 if (scriptname != NULL)
23137 {
23138 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023139 plen = (int)STRLEN(p);
23140 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023141 if (slen > plen && fnamecmp(p,
23142 sourcing_name + slen - plen) == 0)
23143 j = OK;
23144 vim_free(scriptname);
23145 }
23146 }
23147 if (j == FAIL)
23148 {
23149 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23150 goto erret;
23151 }
23152 }
23153
23154 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 if (fp == NULL)
23156 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023157
23158 if (fudi.fd_dict != NULL)
23159 {
23160 if (fudi.fd_di == NULL)
23161 {
23162 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023163 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023164 if (fudi.fd_di == NULL)
23165 {
23166 vim_free(fp);
23167 goto erret;
23168 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023169 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23170 {
23171 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023172 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023173 goto erret;
23174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023175 }
23176 else
23177 /* overwrite existing dict entry */
23178 clear_tv(&fudi.fd_di->di_tv);
23179 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023180 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023181 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023182 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023183
23184 /* behave like "dict" was used */
23185 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023186 }
23187
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023189 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023190 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23191 {
23192 vim_free(fp);
23193 goto erret;
23194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023196 fp->uf_args = newargs;
23197 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023198#ifdef FEAT_PROFILE
23199 fp->uf_tml_count = NULL;
23200 fp->uf_tml_total = NULL;
23201 fp->uf_tml_self = NULL;
23202 fp->uf_profiling = FALSE;
23203 if (prof_def_func())
23204 func_do_profile(fp);
23205#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023206 fp->uf_varargs = varargs;
23207 fp->uf_flags = flags;
23208 fp->uf_calls = 0;
23209 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023210 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211
23212erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 ga_clear_strings(&newargs);
23214 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023215ret_free:
23216 vim_free(skip_until);
23217 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023220 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221}
23222
23223/*
23224 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023225 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023227 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023228 * TFN_INT: internal function name OK
23229 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023230 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 * Advances "pp" to just after the function name (if no error).
23232 */
23233 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023234trans_function_name(
23235 char_u **pp,
23236 int skip, /* only find the end, don't evaluate */
23237 int flags,
23238 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023240 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 char_u *start;
23242 char_u *end;
23243 int lead;
23244 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023246 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023247
23248 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023249 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023251
23252 /* Check for hard coded <SNR>: already translated function ID (from a user
23253 * command). */
23254 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23255 && (*pp)[2] == (int)KE_SNR)
23256 {
23257 *pp += 3;
23258 len = get_id_len(pp) + 3;
23259 return vim_strnsave(start, len);
23260 }
23261
23262 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23263 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023265 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023267
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023268 /* Note that TFN_ flags use the same values as GLV_ flags. */
23269 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023270 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023271 if (end == start)
23272 {
23273 if (!skip)
23274 EMSG(_("E129: Function name required"));
23275 goto theend;
23276 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023277 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023278 {
23279 /*
23280 * Report an invalid expression in braces, unless the expression
23281 * evaluation has been cancelled due to an aborting error, an
23282 * interrupt, or an exception.
23283 */
23284 if (!aborting())
23285 {
23286 if (end != NULL)
23287 EMSG2(_(e_invarg2), start);
23288 }
23289 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023290 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023291 goto theend;
23292 }
23293
23294 if (lv.ll_tv != NULL)
23295 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023296 if (fdp != NULL)
23297 {
23298 fdp->fd_dict = lv.ll_dict;
23299 fdp->fd_newkey = lv.ll_newkey;
23300 lv.ll_newkey = NULL;
23301 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023302 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023303 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23304 {
23305 name = vim_strsave(lv.ll_tv->vval.v_string);
23306 *pp = end;
23307 }
23308 else
23309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023310 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23311 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023312 EMSG(_(e_funcref));
23313 else
23314 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023315 name = NULL;
23316 }
23317 goto theend;
23318 }
23319
23320 if (lv.ll_name == NULL)
23321 {
23322 /* Error found, but continue after the function name. */
23323 *pp = end;
23324 goto theend;
23325 }
23326
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023327 /* Check if the name is a Funcref. If so, use the value. */
23328 if (lv.ll_exp_name != NULL)
23329 {
23330 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023331 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023332 if (name == lv.ll_exp_name)
23333 name = NULL;
23334 }
23335 else
23336 {
23337 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023338 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023339 if (name == *pp)
23340 name = NULL;
23341 }
23342 if (name != NULL)
23343 {
23344 name = vim_strsave(name);
23345 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023346 if (STRNCMP(name, "<SNR>", 5) == 0)
23347 {
23348 /* Change "<SNR>" to the byte sequence. */
23349 name[0] = K_SPECIAL;
23350 name[1] = KS_EXTRA;
23351 name[2] = (int)KE_SNR;
23352 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23353 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023354 goto theend;
23355 }
23356
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023357 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023358 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023359 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023360 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23361 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23362 {
23363 /* When there was "s:" already or the name expanded to get a
23364 * leading "s:" then remove it. */
23365 lv.ll_name += 2;
23366 len -= 2;
23367 lead = 2;
23368 }
23369 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023370 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023371 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023372 /* skip over "s:" and "g:" */
23373 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023374 lv.ll_name += 2;
23375 len = (int)(end - lv.ll_name);
23376 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023377
23378 /*
23379 * Copy the function name to allocated memory.
23380 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23381 * Accept <SNR>123_name() outside a script.
23382 */
23383 if (skip)
23384 lead = 0; /* do nothing */
23385 else if (lead > 0)
23386 {
23387 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023388 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23389 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023390 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023391 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023392 if (current_SID <= 0)
23393 {
23394 EMSG(_(e_usingsid));
23395 goto theend;
23396 }
23397 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23398 lead += (int)STRLEN(sid_buf);
23399 }
23400 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023401 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023402 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023403 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023404 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023405 goto theend;
23406 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023407 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023408 {
23409 char_u *cp = vim_strchr(lv.ll_name, ':');
23410
23411 if (cp != NULL && cp < end)
23412 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023413 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023414 goto theend;
23415 }
23416 }
23417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023418 name = alloc((unsigned)(len + lead + 1));
23419 if (name != NULL)
23420 {
23421 if (lead > 0)
23422 {
23423 name[0] = K_SPECIAL;
23424 name[1] = KS_EXTRA;
23425 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023426 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023427 STRCPY(name + 3, sid_buf);
23428 }
23429 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023430 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023431 }
23432 *pp = end;
23433
23434theend:
23435 clear_lval(&lv);
23436 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437}
23438
23439/*
23440 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23441 * Return 2 if "p" starts with "s:".
23442 * Return 0 otherwise.
23443 */
23444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023445eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023447 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23448 * the standard library function. */
23449 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23450 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 return 5;
23452 if (p[0] == 's' && p[1] == ':')
23453 return 2;
23454 return 0;
23455}
23456
23457/*
23458 * Return TRUE if "p" starts with "<SID>" or "s:".
23459 * Only works if eval_fname_script() returned non-zero for "p"!
23460 */
23461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023462eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023463{
23464 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23465}
23466
23467/*
23468 * List the head of the function: "name(arg1, arg2)".
23469 */
23470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023471list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472{
23473 int j;
23474
23475 msg_start();
23476 if (indent)
23477 MSG_PUTS(" ");
23478 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023479 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 {
23481 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023482 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 }
23484 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023485 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023487 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 {
23489 if (j)
23490 MSG_PUTS(", ");
23491 msg_puts(FUNCARG(fp, j));
23492 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023493 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 {
23495 if (j)
23496 MSG_PUTS(", ");
23497 MSG_PUTS("...");
23498 }
23499 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023500 if (fp->uf_flags & FC_ABORT)
23501 MSG_PUTS(" abort");
23502 if (fp->uf_flags & FC_RANGE)
23503 MSG_PUTS(" range");
23504 if (fp->uf_flags & FC_DICT)
23505 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023506 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023507 if (p_verbose > 0)
23508 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509}
23510
23511/*
23512 * Find a function by name, return pointer to it in ufuncs.
23513 * Return NULL for unknown function.
23514 */
23515 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023516find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023518 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023520 hi = hash_find(&func_hashtab, name);
23521 if (!HASHITEM_EMPTY(hi))
23522 return HI2UF(hi);
23523 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524}
23525
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023526#if defined(EXITFREE) || defined(PROTO)
23527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023528free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023529{
23530 hashitem_T *hi;
23531
23532 /* Need to start all over every time, because func_free() may change the
23533 * hash table. */
23534 while (func_hashtab.ht_used > 0)
23535 for (hi = func_hashtab.ht_array; ; ++hi)
23536 if (!HASHITEM_EMPTY(hi))
23537 {
23538 func_free(HI2UF(hi));
23539 break;
23540 }
23541}
23542#endif
23543
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023544 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023545translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023546{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023547 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023548 return find_internal_func(name) >= 0;
23549 return find_func(name) != NULL;
23550}
23551
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023552/*
23553 * Return TRUE if a function "name" exists.
23554 */
23555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023556function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023557{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023558 char_u *nm = name;
23559 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023560 int n = FALSE;
23561
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023562 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23563 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023564 nm = skipwhite(nm);
23565
23566 /* Only accept "funcname", "funcname ", "funcname (..." and
23567 * "funcname(...", not "funcname!...". */
23568 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023569 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023570 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023571 return n;
23572}
23573
Bram Moolenaara1544c02013-05-30 12:35:52 +020023574 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023575get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023576{
23577 char_u *nm = name;
23578 char_u *p;
23579
23580 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23581
23582 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023583 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023584 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023585
Bram Moolenaara1544c02013-05-30 12:35:52 +020023586 vim_free(p);
23587 return NULL;
23588}
23589
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023590/*
23591 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023592 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23593 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023594 */
23595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023596builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023597{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023598 char_u *p;
23599
23600 if (!ASCII_ISLOWER(name[0]))
23601 return FALSE;
23602 p = vim_strchr(name, AUTOLOAD_CHAR);
23603 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023604}
23605
Bram Moolenaar05159a02005-02-26 23:04:13 +000023606#if defined(FEAT_PROFILE) || defined(PROTO)
23607/*
23608 * Start profiling function "fp".
23609 */
23610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023611func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023612{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023613 int len = fp->uf_lines.ga_len;
23614
23615 if (len == 0)
23616 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023617 fp->uf_tm_count = 0;
23618 profile_zero(&fp->uf_tm_self);
23619 profile_zero(&fp->uf_tm_total);
23620 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023621 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023622 if (fp->uf_tml_total == NULL)
23623 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023624 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023625 if (fp->uf_tml_self == NULL)
23626 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023627 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023628 fp->uf_tml_idx = -1;
23629 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23630 || fp->uf_tml_self == NULL)
23631 return; /* out of memory */
23632
23633 fp->uf_profiling = TRUE;
23634}
23635
23636/*
23637 * Dump the profiling results for all functions in file "fd".
23638 */
23639 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023640func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023641{
23642 hashitem_T *hi;
23643 int todo;
23644 ufunc_T *fp;
23645 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023646 ufunc_T **sorttab;
23647 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023648
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023649 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023650 if (todo == 0)
23651 return; /* nothing to dump */
23652
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023653 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023654
Bram Moolenaar05159a02005-02-26 23:04:13 +000023655 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23656 {
23657 if (!HASHITEM_EMPTY(hi))
23658 {
23659 --todo;
23660 fp = HI2UF(hi);
23661 if (fp->uf_profiling)
23662 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023663 if (sorttab != NULL)
23664 sorttab[st_len++] = fp;
23665
Bram Moolenaar05159a02005-02-26 23:04:13 +000023666 if (fp->uf_name[0] == K_SPECIAL)
23667 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23668 else
23669 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23670 if (fp->uf_tm_count == 1)
23671 fprintf(fd, "Called 1 time\n");
23672 else
23673 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23674 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23675 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23676 fprintf(fd, "\n");
23677 fprintf(fd, "count total (s) self (s)\n");
23678
23679 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23680 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023681 if (FUNCLINE(fp, i) == NULL)
23682 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023683 prof_func_line(fd, fp->uf_tml_count[i],
23684 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023685 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23686 }
23687 fprintf(fd, "\n");
23688 }
23689 }
23690 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023691
23692 if (sorttab != NULL && st_len > 0)
23693 {
23694 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23695 prof_total_cmp);
23696 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23697 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23698 prof_self_cmp);
23699 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23700 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023701
23702 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023703}
Bram Moolenaar73830342005-02-28 22:48:19 +000023704
23705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023706prof_sort_list(
23707 FILE *fd,
23708 ufunc_T **sorttab,
23709 int st_len,
23710 char *title,
23711 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023712{
23713 int i;
23714 ufunc_T *fp;
23715
23716 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23717 fprintf(fd, "count total (s) self (s) function\n");
23718 for (i = 0; i < 20 && i < st_len; ++i)
23719 {
23720 fp = sorttab[i];
23721 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23722 prefer_self);
23723 if (fp->uf_name[0] == K_SPECIAL)
23724 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23725 else
23726 fprintf(fd, " %s()\n", fp->uf_name);
23727 }
23728 fprintf(fd, "\n");
23729}
23730
23731/*
23732 * Print the count and times for one function or function line.
23733 */
23734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023735prof_func_line(
23736 FILE *fd,
23737 int count,
23738 proftime_T *total,
23739 proftime_T *self,
23740 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023741{
23742 if (count > 0)
23743 {
23744 fprintf(fd, "%5d ", count);
23745 if (prefer_self && profile_equal(total, self))
23746 fprintf(fd, " ");
23747 else
23748 fprintf(fd, "%s ", profile_msg(total));
23749 if (!prefer_self && profile_equal(total, self))
23750 fprintf(fd, " ");
23751 else
23752 fprintf(fd, "%s ", profile_msg(self));
23753 }
23754 else
23755 fprintf(fd, " ");
23756}
23757
23758/*
23759 * Compare function for total time sorting.
23760 */
23761 static int
23762#ifdef __BORLANDC__
23763_RTLENTRYF
23764#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023765prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023766{
23767 ufunc_T *p1, *p2;
23768
23769 p1 = *(ufunc_T **)s1;
23770 p2 = *(ufunc_T **)s2;
23771 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23772}
23773
23774/*
23775 * Compare function for self time sorting.
23776 */
23777 static int
23778#ifdef __BORLANDC__
23779_RTLENTRYF
23780#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023781prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023782{
23783 ufunc_T *p1, *p2;
23784
23785 p1 = *(ufunc_T **)s1;
23786 p2 = *(ufunc_T **)s2;
23787 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23788}
23789
Bram Moolenaar05159a02005-02-26 23:04:13 +000023790#endif
23791
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023792/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023793 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023794 * Return TRUE if a package was loaded.
23795 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023796 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023797script_autoload(
23798 char_u *name,
23799 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023800{
23801 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023802 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023803 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023804 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023805
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023806 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023807 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023808 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023809 return FALSE;
23810
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023811 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023812
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023813 /* Find the name in the list of previously loaded package names. Skip
23814 * "autoload/", it's always the same. */
23815 for (i = 0; i < ga_loaded.ga_len; ++i)
23816 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23817 break;
23818 if (!reload && i < ga_loaded.ga_len)
23819 ret = FALSE; /* was loaded already */
23820 else
23821 {
23822 /* Remember the name if it wasn't loaded already. */
23823 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23824 {
23825 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23826 tofree = NULL;
23827 }
23828
23829 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023830 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023831 ret = TRUE;
23832 }
23833
23834 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023835 return ret;
23836}
23837
23838/*
23839 * Return the autoload script name for a function or variable name.
23840 * Returns NULL when out of memory.
23841 */
23842 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023843autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023844{
23845 char_u *p;
23846 char_u *scriptname;
23847
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023848 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023849 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23850 if (scriptname == NULL)
23851 return FALSE;
23852 STRCPY(scriptname, "autoload/");
23853 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023854 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023855 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023856 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023857 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023858 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023859}
23860
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23862
23863/*
23864 * Function given to ExpandGeneric() to obtain the list of user defined
23865 * function names.
23866 */
23867 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023868get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023870 static long_u done;
23871 static hashitem_T *hi;
23872 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873
23874 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023876 done = 0;
23877 hi = func_hashtab.ht_array;
23878 }
23879 if (done < func_hashtab.ht_used)
23880 {
23881 if (done++ > 0)
23882 ++hi;
23883 while (HASHITEM_EMPTY(hi))
23884 ++hi;
23885 fp = HI2UF(hi);
23886
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023887 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023888 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023889
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023890 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23891 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892
23893 cat_func_name(IObuff, fp);
23894 if (xp->xp_context != EXPAND_USER_FUNC)
23895 {
23896 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023897 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 STRCAT(IObuff, ")");
23899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 return IObuff;
23901 }
23902 return NULL;
23903}
23904
23905#endif /* FEAT_CMDL_COMPL */
23906
23907/*
23908 * Copy the function name of "fp" to buffer "buf".
23909 * "buf" must be able to hold the function name plus three bytes.
23910 * Takes care of script-local function names.
23911 */
23912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023913cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023914{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023915 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 {
23917 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023918 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 }
23920 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023921 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922}
23923
23924/*
23925 * ":delfunction {name}"
23926 */
23927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023928ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023930 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931 char_u *p;
23932 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023933 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934
23935 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023936 name = trans_function_name(&p, eap->skip, 0, &fudi);
23937 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023939 {
23940 if (fudi.fd_dict != NULL && !eap->skip)
23941 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 if (!ends_excmd(*skipwhite(p)))
23945 {
23946 vim_free(name);
23947 EMSG(_(e_trailing));
23948 return;
23949 }
23950 eap->nextcmd = check_nextcmd(p);
23951 if (eap->nextcmd != NULL)
23952 *p = NUL;
23953
23954 if (!eap->skip)
23955 fp = find_func(name);
23956 vim_free(name);
23957
23958 if (!eap->skip)
23959 {
23960 if (fp == NULL)
23961 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023962 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 return;
23964 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023965 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 {
23967 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23968 return;
23969 }
23970
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023971 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023973 /* Delete the dict item that refers to the function, it will
23974 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023975 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023977 else
23978 func_free(fp);
23979 }
23980}
23981
23982/*
23983 * Free a function and remove it from the list of functions.
23984 */
23985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023986func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023987{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023988 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023989
23990 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023991 ga_clear_strings(&(fp->uf_args));
23992 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023993#ifdef FEAT_PROFILE
23994 vim_free(fp->uf_tml_count);
23995 vim_free(fp->uf_tml_total);
23996 vim_free(fp->uf_tml_self);
23997#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023998
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023999 /* remove the function from the function hashtable */
24000 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24001 if (HASHITEM_EMPTY(hi))
24002 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024003 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024004 hash_remove(&func_hashtab, hi);
24005
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024006 vim_free(fp);
24007}
24008
24009/*
24010 * Unreference a Function: decrement the reference count and free it when it
24011 * becomes zero. Only for numbered functions.
24012 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024014func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024015{
24016 ufunc_T *fp;
24017
24018 if (name != NULL && isdigit(*name))
24019 {
24020 fp = find_func(name);
24021 if (fp == NULL)
24022 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024023 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024024 {
24025 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024026 * when "uf_calls" becomes zero. */
24027 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024028 func_free(fp);
24029 }
24030 }
24031}
24032
24033/*
24034 * Count a reference to a Function.
24035 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024037func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024038{
24039 ufunc_T *fp;
24040
24041 if (name != NULL && isdigit(*name))
24042 {
24043 fp = find_func(name);
24044 if (fp == NULL)
24045 EMSG2(_(e_intern2), "func_ref()");
24046 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024047 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 }
24049}
24050
24051/*
24052 * Call a user function.
24053 */
24054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024055call_user_func(
24056 ufunc_T *fp, /* pointer to function */
24057 int argcount, /* nr of args */
24058 typval_T *argvars, /* arguments */
24059 typval_T *rettv, /* return value */
24060 linenr_T firstline, /* first line of range */
24061 linenr_T lastline, /* last line of range */
24062 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063{
Bram Moolenaar33570922005-01-25 22:26:29 +000024064 char_u *save_sourcing_name;
24065 linenr_T save_sourcing_lnum;
24066 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024067 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024068 int save_did_emsg;
24069 static int depth = 0;
24070 dictitem_T *v;
24071 int fixvar_idx = 0; /* index in fixvar[] */
24072 int i;
24073 int ai;
24074 char_u numbuf[NUMBUFLEN];
24075 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024076 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024077#ifdef FEAT_PROFILE
24078 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024079 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081
24082 /* If depth of calling is getting too high, don't execute the function */
24083 if (depth >= p_mfd)
24084 {
24085 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024086 rettv->v_type = VAR_NUMBER;
24087 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 return;
24089 }
24090 ++depth;
24091
24092 line_breakcheck(); /* check for CTRL-C hit */
24093
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024094 fc = (funccall_T *)alloc(sizeof(funccall_T));
24095 fc->caller = current_funccal;
24096 current_funccal = fc;
24097 fc->func = fp;
24098 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024099 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024100 fc->linenr = 0;
24101 fc->returned = FALSE;
24102 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024104 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24105 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106
Bram Moolenaar33570922005-01-25 22:26:29 +000024107 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024108 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024109 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24110 * each argument variable and saves a lot of time.
24111 */
24112 /*
24113 * Init l: variables.
24114 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024115 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024116 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024117 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024118 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24119 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024120 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024121 name = v->di_key;
24122 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024123 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024124 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024125 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024126 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024127 v->di_tv.vval.v_dict = selfdict;
24128 ++selfdict->dv_refcount;
24129 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024130
Bram Moolenaar33570922005-01-25 22:26:29 +000024131 /*
24132 * Init a: variables.
24133 * Set a:0 to "argcount".
24134 * Set a:000 to a list with room for the "..." arguments.
24135 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024136 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024137 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024138 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024139 /* Use "name" to avoid a warning from some compiler that checks the
24140 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024141 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024142 name = v->di_key;
24143 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024144 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024145 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024146 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024147 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024148 v->di_tv.vval.v_list = &fc->l_varlist;
24149 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24150 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24151 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024152
24153 /*
24154 * Set a:firstline to "firstline" and a:lastline to "lastline".
24155 * Set a:name to named arguments.
24156 * Set a:N to the "..." arguments.
24157 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024158 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024159 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024160 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024161 (varnumber_T)lastline);
24162 for (i = 0; i < argcount; ++i)
24163 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024164 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024165 if (ai < 0)
24166 /* named argument a:name */
24167 name = FUNCARG(fp, i);
24168 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024169 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024170 /* "..." argument a:1, a:2, etc. */
24171 sprintf((char *)numbuf, "%d", ai + 1);
24172 name = numbuf;
24173 }
24174 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24175 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024176 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024177 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24178 }
24179 else
24180 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024181 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24182 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024183 if (v == NULL)
24184 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024185 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024186 }
24187 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024188 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024189
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024190 /* Note: the values are copied directly to avoid alloc/free.
24191 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024192 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024193 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024194
24195 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24196 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024197 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24198 fc->l_listitems[ai].li_tv = argvars[i];
24199 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024200 }
24201 }
24202
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 /* Don't redraw while executing the function. */
24204 ++RedrawingDisabled;
24205 save_sourcing_name = sourcing_name;
24206 save_sourcing_lnum = sourcing_lnum;
24207 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024208 /* need space for function name + ("function " + 3) or "[number]" */
24209 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24210 + STRLEN(fp->uf_name) + 20;
24211 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 if (sourcing_name != NULL)
24213 {
24214 if (save_sourcing_name != NULL
24215 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024216 sprintf((char *)sourcing_name, "%s[%d]..",
24217 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 else
24219 STRCPY(sourcing_name, "function ");
24220 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24221
24222 if (p_verbose >= 12)
24223 {
24224 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024225 verbose_enter_scroll();
24226
Bram Moolenaar555b2802005-05-19 21:08:39 +000024227 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 if (p_verbose >= 14)
24229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024231 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024232 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024233 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234
24235 msg_puts((char_u *)"(");
24236 for (i = 0; i < argcount; ++i)
24237 {
24238 if (i > 0)
24239 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024240 if (argvars[i].v_type == VAR_NUMBER)
24241 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 else
24243 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024244 /* Do not want errors such as E724 here. */
24245 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024246 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024247 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024248 if (s != NULL)
24249 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024250 if (vim_strsize(s) > MSG_BUF_CLEN)
24251 {
24252 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24253 s = buf;
24254 }
24255 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024256 vim_free(tofree);
24257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 }
24259 }
24260 msg_puts((char_u *)")");
24261 }
24262 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024263
24264 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 --no_wait_return;
24266 }
24267 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024268#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024269 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024270 {
24271 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24272 func_do_profile(fp);
24273 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024274 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024275 {
24276 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024277 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024278 profile_zero(&fp->uf_tm_children);
24279 }
24280 script_prof_save(&wait_start);
24281 }
24282#endif
24283
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024285 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 save_did_emsg = did_emsg;
24287 did_emsg = FALSE;
24288
24289 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024290 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24292
24293 --RedrawingDisabled;
24294
24295 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024296 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024298 clear_tv(rettv);
24299 rettv->v_type = VAR_NUMBER;
24300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301 }
24302
Bram Moolenaar05159a02005-02-26 23:04:13 +000024303#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024304 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024305 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024306 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024307 profile_end(&call_start);
24308 profile_sub_wait(&wait_start, &call_start);
24309 profile_add(&fp->uf_tm_total, &call_start);
24310 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024311 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024312 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024313 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24314 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024315 }
24316 }
24317#endif
24318
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319 /* when being verbose, mention the return value */
24320 if (p_verbose >= 12)
24321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024323 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024326 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024327 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024328 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024329 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024332 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024333 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024334 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024335 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024336
Bram Moolenaar555b2802005-05-19 21:08:39 +000024337 /* The value may be very long. Skip the middle part, so that we
24338 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024339 * truncate it at the end. Don't want errors such as E724 here. */
24340 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024341 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024342 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024343 if (s != NULL)
24344 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024345 if (vim_strsize(s) > MSG_BUF_CLEN)
24346 {
24347 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24348 s = buf;
24349 }
24350 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024351 vim_free(tofree);
24352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353 }
24354 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024355
24356 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 --no_wait_return;
24358 }
24359
24360 vim_free(sourcing_name);
24361 sourcing_name = save_sourcing_name;
24362 sourcing_lnum = save_sourcing_lnum;
24363 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024364#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024365 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024366 script_prof_restore(&wait_start);
24367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368
24369 if (p_verbose >= 12 && sourcing_name != NULL)
24370 {
24371 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024372 verbose_enter_scroll();
24373
Bram Moolenaar555b2802005-05-19 21:08:39 +000024374 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024376
24377 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 --no_wait_return;
24379 }
24380
24381 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024382 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024384
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024385 /* If the a:000 list and the l: and a: dicts are not referenced we can
24386 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024387 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24388 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24389 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24390 {
24391 free_funccal(fc, FALSE);
24392 }
24393 else
24394 {
24395 hashitem_T *hi;
24396 listitem_T *li;
24397 int todo;
24398
24399 /* "fc" is still in use. This can happen when returning "a:000" or
24400 * assigning "l:" to a global variable.
24401 * Link "fc" in the list for garbage collection later. */
24402 fc->caller = previous_funccal;
24403 previous_funccal = fc;
24404
24405 /* Make a copy of the a: variables, since we didn't do that above. */
24406 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24407 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24408 {
24409 if (!HASHITEM_EMPTY(hi))
24410 {
24411 --todo;
24412 v = HI2DI(hi);
24413 copy_tv(&v->di_tv, &v->di_tv);
24414 }
24415 }
24416
24417 /* Make a copy of the a:000 items, since we didn't do that above. */
24418 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24419 copy_tv(&li->li_tv, &li->li_tv);
24420 }
24421}
24422
24423/*
24424 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024425 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024426 */
24427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024428can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024429{
24430 return (fc->l_varlist.lv_copyID != copyID
24431 && fc->l_vars.dv_copyID != copyID
24432 && fc->l_avars.dv_copyID != copyID);
24433}
24434
24435/*
24436 * Free "fc" and what it contains.
24437 */
24438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024439free_funccal(
24440 funccall_T *fc,
24441 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024442{
24443 listitem_T *li;
24444
24445 /* The a: variables typevals may not have been allocated, only free the
24446 * allocated variables. */
24447 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24448
24449 /* free all l: variables */
24450 vars_clear(&fc->l_vars.dv_hashtab);
24451
24452 /* Free the a:000 variables if they were allocated. */
24453 if (free_val)
24454 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24455 clear_tv(&li->li_tv);
24456
24457 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458}
24459
24460/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024461 * Add a number variable "name" to dict "dp" with value "nr".
24462 */
24463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024464add_nr_var(
24465 dict_T *dp,
24466 dictitem_T *v,
24467 char *name,
24468 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024469{
24470 STRCPY(v->di_key, name);
24471 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24472 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24473 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024474 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024475 v->di_tv.vval.v_number = nr;
24476}
24477
24478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479 * ":return [expr]"
24480 */
24481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024482ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483{
24484 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024485 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486 int returning = FALSE;
24487
24488 if (current_funccal == NULL)
24489 {
24490 EMSG(_("E133: :return not inside a function"));
24491 return;
24492 }
24493
24494 if (eap->skip)
24495 ++emsg_skip;
24496
24497 eap->nextcmd = NULL;
24498 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024499 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500 {
24501 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024502 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024504 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 }
24506 /* It's safer to return also on error. */
24507 else if (!eap->skip)
24508 {
24509 /*
24510 * Return unless the expression evaluation has been cancelled due to an
24511 * aborting error, an interrupt, or an exception.
24512 */
24513 if (!aborting())
24514 returning = do_return(eap, FALSE, TRUE, NULL);
24515 }
24516
24517 /* When skipping or the return gets pending, advance to the next command
24518 * in this line (!returning). Otherwise, ignore the rest of the line.
24519 * Following lines will be ignored by get_func_line(). */
24520 if (returning)
24521 eap->nextcmd = NULL;
24522 else if (eap->nextcmd == NULL) /* no argument */
24523 eap->nextcmd = check_nextcmd(arg);
24524
24525 if (eap->skip)
24526 --emsg_skip;
24527}
24528
24529/*
24530 * Return from a function. Possibly makes the return pending. Also called
24531 * for a pending return at the ":endtry" or after returning from an extra
24532 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024533 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024534 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 * FALSE when the return gets pending.
24536 */
24537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024538do_return(
24539 exarg_T *eap,
24540 int reanimate,
24541 int is_cmd,
24542 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024543{
24544 int idx;
24545 struct condstack *cstack = eap->cstack;
24546
24547 if (reanimate)
24548 /* Undo the return. */
24549 current_funccal->returned = FALSE;
24550
24551 /*
24552 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24553 * not in its finally clause (which then is to be executed next) is found.
24554 * In this case, make the ":return" pending for execution at the ":endtry".
24555 * Otherwise, return normally.
24556 */
24557 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24558 if (idx >= 0)
24559 {
24560 cstack->cs_pending[idx] = CSTP_RETURN;
24561
24562 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024563 /* A pending return again gets pending. "rettv" points to an
24564 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024565 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024566 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 else
24568 {
24569 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024570 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024572 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024574 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 {
24576 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024577 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024578 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579 else
24580 EMSG(_(e_outofmem));
24581 }
24582 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024583 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584
24585 if (reanimate)
24586 {
24587 /* The pending return value could be overwritten by a ":return"
24588 * without argument in a finally clause; reset the default
24589 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024590 current_funccal->rettv->v_type = VAR_NUMBER;
24591 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024592 }
24593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024594 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 }
24596 else
24597 {
24598 current_funccal->returned = TRUE;
24599
24600 /* If the return is carried out now, store the return value. For
24601 * a return immediately after reanimation, the value is already
24602 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024603 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024605 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024606 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024608 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 }
24610 }
24611
24612 return idx < 0;
24613}
24614
24615/*
24616 * Free the variable with a pending return value.
24617 */
24618 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024619discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620{
Bram Moolenaar33570922005-01-25 22:26:29 +000024621 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622}
24623
24624/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024625 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 * is an allocated string. Used by report_pending() for verbose messages.
24627 */
24628 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024629get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024631 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024632 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024633 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024635 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024636 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024637 if (s == NULL)
24638 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024639
24640 STRCPY(IObuff, ":return ");
24641 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24642 if (STRLEN(s) + 8 >= IOSIZE)
24643 STRCPY(IObuff + IOSIZE - 4, "...");
24644 vim_free(tofree);
24645 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646}
24647
24648/*
24649 * Get next function line.
24650 * Called by do_cmdline() to get the next line.
24651 * Returns allocated string, or NULL for end of function.
24652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024654get_func_line(
24655 int c UNUSED,
24656 void *cookie,
24657 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658{
Bram Moolenaar33570922005-01-25 22:26:29 +000024659 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024660 ufunc_T *fp = fcp->func;
24661 char_u *retval;
24662 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663
24664 /* If breakpoints have been added/deleted need to check for it. */
24665 if (fcp->dbg_tick != debug_tick)
24666 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024667 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 sourcing_lnum);
24669 fcp->dbg_tick = debug_tick;
24670 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024671#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024672 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024673 func_line_end(cookie);
24674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675
Bram Moolenaar05159a02005-02-26 23:04:13 +000024676 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024677 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24678 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 retval = NULL;
24680 else
24681 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024682 /* Skip NULL lines (continuation lines). */
24683 while (fcp->linenr < gap->ga_len
24684 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24685 ++fcp->linenr;
24686 if (fcp->linenr >= gap->ga_len)
24687 retval = NULL;
24688 else
24689 {
24690 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24691 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024692#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024693 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024694 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024695#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 }
24698
24699 /* Did we encounter a breakpoint? */
24700 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24701 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024702 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024704 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705 sourcing_lnum);
24706 fcp->dbg_tick = debug_tick;
24707 }
24708
24709 return retval;
24710}
24711
Bram Moolenaar05159a02005-02-26 23:04:13 +000024712#if defined(FEAT_PROFILE) || defined(PROTO)
24713/*
24714 * Called when starting to read a function line.
24715 * "sourcing_lnum" must be correct!
24716 * When skipping lines it may not actually be executed, but we won't find out
24717 * until later and we need to store the time now.
24718 */
24719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024720func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024721{
24722 funccall_T *fcp = (funccall_T *)cookie;
24723 ufunc_T *fp = fcp->func;
24724
24725 if (fp->uf_profiling && sourcing_lnum >= 1
24726 && sourcing_lnum <= fp->uf_lines.ga_len)
24727 {
24728 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024729 /* Skip continuation lines. */
24730 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24731 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024732 fp->uf_tml_execed = FALSE;
24733 profile_start(&fp->uf_tml_start);
24734 profile_zero(&fp->uf_tml_children);
24735 profile_get_wait(&fp->uf_tml_wait);
24736 }
24737}
24738
24739/*
24740 * Called when actually executing a function line.
24741 */
24742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024743func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024744{
24745 funccall_T *fcp = (funccall_T *)cookie;
24746 ufunc_T *fp = fcp->func;
24747
24748 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24749 fp->uf_tml_execed = TRUE;
24750}
24751
24752/*
24753 * Called when done with a function line.
24754 */
24755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024756func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024757{
24758 funccall_T *fcp = (funccall_T *)cookie;
24759 ufunc_T *fp = fcp->func;
24760
24761 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24762 {
24763 if (fp->uf_tml_execed)
24764 {
24765 ++fp->uf_tml_count[fp->uf_tml_idx];
24766 profile_end(&fp->uf_tml_start);
24767 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024768 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024769 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24770 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024771 }
24772 fp->uf_tml_idx = -1;
24773 }
24774}
24775#endif
24776
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777/*
24778 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024779 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780 */
24781 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024782func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783{
Bram Moolenaar33570922005-01-25 22:26:29 +000024784 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785
24786 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24787 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024788 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 || fcp->returned);
24790}
24791
24792/*
24793 * return TRUE if cookie indicates a function which "abort"s on errors.
24794 */
24795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024796func_has_abort(
24797 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024798{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024799 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800}
24801
24802#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24803typedef enum
24804{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024805 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24806 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24807 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808} var_flavour_T;
24809
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024810static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024811
24812 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024813var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814{
24815 char_u *p = varname;
24816
24817 if (ASCII_ISUPPER(*p))
24818 {
24819 while (*(++p))
24820 if (ASCII_ISLOWER(*p))
24821 return VAR_FLAVOUR_SESSION;
24822 return VAR_FLAVOUR_VIMINFO;
24823 }
24824 else
24825 return VAR_FLAVOUR_DEFAULT;
24826}
24827#endif
24828
24829#if defined(FEAT_VIMINFO) || defined(PROTO)
24830/*
24831 * Restore global vars that start with a capital from the viminfo file
24832 */
24833 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024834read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835{
24836 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024837 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024838 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024839 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840
24841 if (!writing && (find_viminfo_parameter('!') != NULL))
24842 {
24843 tab = vim_strchr(virp->vir_line + 1, '\t');
24844 if (tab != NULL)
24845 {
24846 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024847 switch (*tab)
24848 {
24849 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024850#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024851 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024852#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024853 case 'D': type = VAR_DICT; break;
24854 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024855 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857
24858 tab = vim_strchr(tab, '\t');
24859 if (tab != NULL)
24860 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024861 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024862 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024863 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024865#ifdef FEAT_FLOAT
24866 else if (type == VAR_FLOAT)
24867 (void)string2float(tab + 1, &tv.vval.v_float);
24868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024870 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024871 if (type == VAR_DICT || type == VAR_LIST)
24872 {
24873 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24874
24875 if (etv == NULL)
24876 /* Failed to parse back the dict or list, use it as a
24877 * string. */
24878 tv.v_type = VAR_STRING;
24879 else
24880 {
24881 vim_free(tv.vval.v_string);
24882 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024883 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024884 }
24885 }
24886
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024887 /* when in a function use global variables */
24888 save_funccal = current_funccal;
24889 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024890 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024891 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024892
24893 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024894 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024895 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24896 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 }
24898 }
24899 }
24900
24901 return viminfo_readline(virp);
24902}
24903
24904/*
24905 * Write global vars that start with a capital to the viminfo file
24906 */
24907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024908write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909{
Bram Moolenaar33570922005-01-25 22:26:29 +000024910 hashitem_T *hi;
24911 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024912 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024913 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024914 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024915 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024916 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917
24918 if (find_viminfo_parameter('!') == NULL)
24919 return;
24920
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024921 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024922
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024923 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024924 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024926 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024928 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024929 this_var = HI2DI(hi);
24930 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024931 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024932 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024933 {
24934 case VAR_STRING: s = "STR"; break;
24935 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024936 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024937 case VAR_DICT: s = "DIC"; break;
24938 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024939 case VAR_SPECIAL: s = "XPL"; break;
24940
24941 case VAR_UNKNOWN:
24942 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010024943 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +010024944 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000024945 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024946 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024947 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024948 if (p != NULL)
24949 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024950 vim_free(tofree);
24951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952 }
24953 }
24954}
24955#endif
24956
24957#if defined(FEAT_SESSION) || defined(PROTO)
24958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024959store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960{
Bram Moolenaar33570922005-01-25 22:26:29 +000024961 hashitem_T *hi;
24962 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024963 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 char_u *p, *t;
24965
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024966 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024967 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024968 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024969 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024971 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024972 this_var = HI2DI(hi);
24973 if ((this_var->di_tv.v_type == VAR_NUMBER
24974 || this_var->di_tv.v_type == VAR_STRING)
24975 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024976 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024977 /* Escape special characters with a backslash. Turn a LF and
24978 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024979 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024980 (char_u *)"\\\"\n\r");
24981 if (p == NULL) /* out of memory */
24982 break;
24983 for (t = p; *t != NUL; ++t)
24984 if (*t == '\n')
24985 *t = 'n';
24986 else if (*t == '\r')
24987 *t = 'r';
24988 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024989 this_var->di_key,
24990 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24991 : ' ',
24992 p,
24993 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24994 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024995 || put_eol(fd) == FAIL)
24996 {
24997 vim_free(p);
24998 return FAIL;
24999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000 vim_free(p);
25001 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025002#ifdef FEAT_FLOAT
25003 else if (this_var->di_tv.v_type == VAR_FLOAT
25004 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25005 {
25006 float_T f = this_var->di_tv.vval.v_float;
25007 int sign = ' ';
25008
25009 if (f < 0)
25010 {
25011 f = -f;
25012 sign = '-';
25013 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025014 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025015 this_var->di_key, sign, f) < 0)
25016 || put_eol(fd) == FAIL)
25017 return FAIL;
25018 }
25019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020 }
25021 }
25022 return OK;
25023}
25024#endif
25025
Bram Moolenaar661b1822005-07-28 22:36:45 +000025026/*
25027 * Display script name where an item was last set.
25028 * Should only be invoked when 'verbose' is non-zero.
25029 */
25030 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025031last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025032{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025033 char_u *p;
25034
Bram Moolenaar661b1822005-07-28 22:36:45 +000025035 if (scriptID != 0)
25036 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025037 p = home_replace_save(NULL, get_scriptname(scriptID));
25038 if (p != NULL)
25039 {
25040 verbose_enter();
25041 MSG_PUTS(_("\n\tLast set from "));
25042 MSG_PUTS(p);
25043 vim_free(p);
25044 verbose_leave();
25045 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025046 }
25047}
25048
Bram Moolenaard812df62008-11-09 12:46:09 +000025049/*
25050 * List v:oldfiles in a nice way.
25051 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025053ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025054{
25055 list_T *l = vimvars[VV_OLDFILES].vv_list;
25056 listitem_T *li;
25057 int nr = 0;
25058
25059 if (l == NULL)
25060 msg((char_u *)_("No old files"));
25061 else
25062 {
25063 msg_start();
25064 msg_scroll = TRUE;
25065 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25066 {
25067 msg_outnum((long)++nr);
25068 MSG_PUTS(": ");
25069 msg_outtrans(get_tv_string(&li->li_tv));
25070 msg_putchar('\n');
25071 out_flush(); /* output one line at a time */
25072 ui_breakcheck();
25073 }
25074 /* Assume "got_int" was set to truncate the listing. */
25075 got_int = FALSE;
25076
25077#ifdef FEAT_BROWSE_CMD
25078 if (cmdmod.browse)
25079 {
25080 quit_more = FALSE;
25081 nr = prompt_for_number(FALSE);
25082 msg_starthere();
25083 if (nr > 0)
25084 {
25085 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25086 (long)nr);
25087
25088 if (p != NULL)
25089 {
25090 p = expand_env_save(p);
25091 eap->arg = p;
25092 eap->cmdidx = CMD_edit;
25093 cmdmod.browse = FALSE;
25094 do_exedit(eap, NULL);
25095 vim_free(p);
25096 }
25097 }
25098 }
25099#endif
25100 }
25101}
25102
Bram Moolenaar53744302015-07-17 17:38:22 +020025103/* reset v:option_new, v:option_old and v:option_type */
25104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025105reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025106{
25107 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25108 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25109 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25110}
25111
25112
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113#endif /* FEAT_EVAL */
25114
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025116#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117
25118#ifdef WIN3264
25119/*
25120 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25121 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025122static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25123static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25124static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125
25126/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025127 * Get the short path (8.3) for the filename in "fnamep".
25128 * Only works for a valid file name.
25129 * When the path gets longer "fnamep" is changed and the allocated buffer
25130 * is put in "bufp".
25131 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25132 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133 */
25134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025135get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025137 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 char_u *newbuf;
25139
25140 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025141 l = GetShortPathName(*fnamep, *fnamep, len);
25142 if (l > len - 1)
25143 {
25144 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025145 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146 newbuf = vim_strnsave(*fnamep, l);
25147 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025148 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149
25150 vim_free(*bufp);
25151 *fnamep = *bufp = newbuf;
25152
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025153 /* Really should always succeed, as the buffer is big enough. */
25154 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155 }
25156
25157 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025158 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159}
25160
25161/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025162 * Get the short path (8.3) for the filename in "fname". The converted
25163 * path is returned in "bufp".
25164 *
25165 * Some of the directories specified in "fname" may not exist. This function
25166 * will shorten the existing directories at the beginning of the path and then
25167 * append the remaining non-existing path.
25168 *
25169 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025170 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025171 * bufp - Pointer to an allocated buffer for the filename.
25172 * fnamelen - Length of the filename pointed to by fname
25173 *
25174 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175 */
25176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025177shortpath_for_invalid_fname(
25178 char_u **fname,
25179 char_u **bufp,
25180 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025182 char_u *short_fname, *save_fname, *pbuf_unused;
25183 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025185 int old_len, len;
25186 int new_len, sfx_len;
25187 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025188
25189 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025190 old_len = *fnamelen;
25191 save_fname = vim_strnsave(*fname, old_len);
25192 pbuf_unused = NULL;
25193 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025195 endp = save_fname + old_len - 1; /* Find the end of the copy */
25196 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025198 /*
25199 * Try shortening the supplied path till it succeeds by removing one
25200 * directory at a time from the tail of the path.
25201 */
25202 len = 0;
25203 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025205 /* go back one path-separator */
25206 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25207 --endp;
25208 if (endp <= save_fname)
25209 break; /* processed the complete path */
25210
25211 /*
25212 * Replace the path separator with a NUL and try to shorten the
25213 * resulting path.
25214 */
25215 ch = *endp;
25216 *endp = 0;
25217 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025218 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025219 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25220 {
25221 retval = FAIL;
25222 goto theend;
25223 }
25224 *endp = ch; /* preserve the string */
25225
25226 if (len > 0)
25227 break; /* successfully shortened the path */
25228
25229 /* failed to shorten the path. Skip the path separator */
25230 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231 }
25232
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025233 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025235 /*
25236 * Succeeded in shortening the path. Now concatenate the shortened
25237 * path with the remaining path at the tail.
25238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025240 /* Compute the length of the new path. */
25241 sfx_len = (int)(save_endp - endp) + 1;
25242 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025244 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025246 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025248 /* There is not enough space in the currently allocated string,
25249 * copy it to a buffer big enough. */
25250 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025251 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025252 {
25253 retval = FAIL;
25254 goto theend;
25255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256 }
25257 else
25258 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025259 /* Transfer short_fname to the main buffer (it's big enough),
25260 * unless get_short_pathname() did its work in-place. */
25261 *fname = *bufp = save_fname;
25262 if (short_fname != save_fname)
25263 vim_strncpy(save_fname, short_fname, len);
25264 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025266
25267 /* concat the not-shortened part of the path */
25268 vim_strncpy(*fname + len, endp, sfx_len);
25269 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025271
25272theend:
25273 vim_free(pbuf_unused);
25274 vim_free(save_fname);
25275
25276 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277}
25278
25279/*
25280 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025281 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282 */
25283 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025284shortpath_for_partial(
25285 char_u **fnamep,
25286 char_u **bufp,
25287 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288{
25289 int sepcount, len, tflen;
25290 char_u *p;
25291 char_u *pbuf, *tfname;
25292 int hasTilde;
25293
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025294 /* Count up the path separators from the RHS.. so we know which part
25295 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025297 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298 if (vim_ispathsep(*p))
25299 ++sepcount;
25300
25301 /* Need full path first (use expand_env() to remove a "~/") */
25302 hasTilde = (**fnamep == '~');
25303 if (hasTilde)
25304 pbuf = tfname = expand_env_save(*fnamep);
25305 else
25306 pbuf = tfname = FullName_save(*fnamep, FALSE);
25307
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025308 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025309
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025310 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25311 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312
25313 if (len == 0)
25314 {
25315 /* Don't have a valid filename, so shorten the rest of the
25316 * path if we can. This CAN give us invalid 8.3 filenames, but
25317 * there's not a lot of point in guessing what it might be.
25318 */
25319 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025320 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322 }
25323
25324 /* Count the paths backward to find the beginning of the desired string. */
25325 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025326 {
25327#ifdef FEAT_MBYTE
25328 if (has_mbyte)
25329 p -= mb_head_off(tfname, p);
25330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 if (vim_ispathsep(*p))
25332 {
25333 if (sepcount == 0 || (hasTilde && sepcount == 1))
25334 break;
25335 else
25336 sepcount --;
25337 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025339 if (hasTilde)
25340 {
25341 --p;
25342 if (p >= tfname)
25343 *p = '~';
25344 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346 }
25347 else
25348 ++p;
25349
25350 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25351 vim_free(*bufp);
25352 *fnamelen = (int)STRLEN(p);
25353 *bufp = pbuf;
25354 *fnamep = p;
25355
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025356 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357}
25358#endif /* WIN3264 */
25359
25360/*
25361 * Adjust a filename, according to a string of modifiers.
25362 * *fnamep must be NUL terminated when called. When returning, the length is
25363 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025364 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025365 * When there is an error, *fnamep is set to NULL.
25366 */
25367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025368modify_fname(
25369 char_u *src, /* string with modifiers */
25370 int *usedlen, /* characters after src that are used */
25371 char_u **fnamep, /* file name so far */
25372 char_u **bufp, /* buffer for allocated file name or NULL */
25373 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374{
25375 int valid = 0;
25376 char_u *tail;
25377 char_u *s, *p, *pbuf;
25378 char_u dirname[MAXPATHL];
25379 int c;
25380 int has_fullname = 0;
25381#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025382 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 int has_shortname = 0;
25384#endif
25385
25386repeat:
25387 /* ":p" - full path/file_name */
25388 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25389 {
25390 has_fullname = 1;
25391
25392 valid |= VALID_PATH;
25393 *usedlen += 2;
25394
25395 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25396 if ((*fnamep)[0] == '~'
25397#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25398 && ((*fnamep)[1] == '/'
25399# ifdef BACKSLASH_IN_FILENAME
25400 || (*fnamep)[1] == '\\'
25401# endif
25402 || (*fnamep)[1] == NUL)
25403
25404#endif
25405 )
25406 {
25407 *fnamep = expand_env_save(*fnamep);
25408 vim_free(*bufp); /* free any allocated file name */
25409 *bufp = *fnamep;
25410 if (*fnamep == NULL)
25411 return -1;
25412 }
25413
25414 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025415 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 {
25417 if (vim_ispathsep(*p)
25418 && p[1] == '.'
25419 && (p[2] == NUL
25420 || vim_ispathsep(p[2])
25421 || (p[2] == '.'
25422 && (p[3] == NUL || vim_ispathsep(p[3])))))
25423 break;
25424 }
25425
25426 /* FullName_save() is slow, don't use it when not needed. */
25427 if (*p != NUL || !vim_isAbsName(*fnamep))
25428 {
25429 *fnamep = FullName_save(*fnamep, *p != NUL);
25430 vim_free(*bufp); /* free any allocated file name */
25431 *bufp = *fnamep;
25432 if (*fnamep == NULL)
25433 return -1;
25434 }
25435
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025436#ifdef WIN3264
25437# if _WIN32_WINNT >= 0x0500
25438 if (vim_strchr(*fnamep, '~') != NULL)
25439 {
25440 /* Expand 8.3 filename to full path. Needed to make sure the same
25441 * file does not have two different names.
25442 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25443 p = alloc(_MAX_PATH + 1);
25444 if (p != NULL)
25445 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025446 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025447 {
25448 vim_free(*bufp);
25449 *bufp = *fnamep = p;
25450 }
25451 else
25452 vim_free(p);
25453 }
25454 }
25455# endif
25456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 /* Append a path separator to a directory. */
25458 if (mch_isdir(*fnamep))
25459 {
25460 /* Make room for one or two extra characters. */
25461 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25462 vim_free(*bufp); /* free any allocated file name */
25463 *bufp = *fnamep;
25464 if (*fnamep == NULL)
25465 return -1;
25466 add_pathsep(*fnamep);
25467 }
25468 }
25469
25470 /* ":." - path relative to the current directory */
25471 /* ":~" - path relative to the home directory */
25472 /* ":8" - shortname path - postponed till after */
25473 while (src[*usedlen] == ':'
25474 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25475 {
25476 *usedlen += 2;
25477 if (c == '8')
25478 {
25479#ifdef WIN3264
25480 has_shortname = 1; /* Postpone this. */
25481#endif
25482 continue;
25483 }
25484 pbuf = NULL;
25485 /* Need full path first (use expand_env() to remove a "~/") */
25486 if (!has_fullname)
25487 {
25488 if (c == '.' && **fnamep == '~')
25489 p = pbuf = expand_env_save(*fnamep);
25490 else
25491 p = pbuf = FullName_save(*fnamep, FALSE);
25492 }
25493 else
25494 p = *fnamep;
25495
25496 has_fullname = 0;
25497
25498 if (p != NULL)
25499 {
25500 if (c == '.')
25501 {
25502 mch_dirname(dirname, MAXPATHL);
25503 s = shorten_fname(p, dirname);
25504 if (s != NULL)
25505 {
25506 *fnamep = s;
25507 if (pbuf != NULL)
25508 {
25509 vim_free(*bufp); /* free any allocated file name */
25510 *bufp = pbuf;
25511 pbuf = NULL;
25512 }
25513 }
25514 }
25515 else
25516 {
25517 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25518 /* Only replace it when it starts with '~' */
25519 if (*dirname == '~')
25520 {
25521 s = vim_strsave(dirname);
25522 if (s != NULL)
25523 {
25524 *fnamep = s;
25525 vim_free(*bufp);
25526 *bufp = s;
25527 }
25528 }
25529 }
25530 vim_free(pbuf);
25531 }
25532 }
25533
25534 tail = gettail(*fnamep);
25535 *fnamelen = (int)STRLEN(*fnamep);
25536
25537 /* ":h" - head, remove "/file_name", can be repeated */
25538 /* Don't remove the first "/" or "c:\" */
25539 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25540 {
25541 valid |= VALID_HEAD;
25542 *usedlen += 2;
25543 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025544 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025545 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546 *fnamelen = (int)(tail - *fnamep);
25547#ifdef VMS
25548 if (*fnamelen > 0)
25549 *fnamelen += 1; /* the path separator is part of the path */
25550#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025551 if (*fnamelen == 0)
25552 {
25553 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25554 p = vim_strsave((char_u *)".");
25555 if (p == NULL)
25556 return -1;
25557 vim_free(*bufp);
25558 *bufp = *fnamep = tail = p;
25559 *fnamelen = 1;
25560 }
25561 else
25562 {
25563 while (tail > s && !after_pathsep(s, tail))
25564 mb_ptr_back(*fnamep, tail);
25565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025566 }
25567
25568 /* ":8" - shortname */
25569 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25570 {
25571 *usedlen += 2;
25572#ifdef WIN3264
25573 has_shortname = 1;
25574#endif
25575 }
25576
25577#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025578 /*
25579 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025580 */
25581 if (has_shortname)
25582 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025583 /* Copy the string if it is shortened by :h and when it wasn't copied
25584 * yet, because we are going to change it in place. Avoids changing
25585 * the buffer name for "%:8". */
25586 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025587 {
25588 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025589 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590 return -1;
25591 vim_free(*bufp);
25592 *bufp = *fnamep = p;
25593 }
25594
25595 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025596 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025597 if (!has_fullname && !vim_isAbsName(*fnamep))
25598 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025599 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025600 return -1;
25601 }
25602 else
25603 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025604 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025605
Bram Moolenaardc935552011-08-17 15:23:23 +020025606 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025608 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609 return -1;
25610
25611 if (l == 0)
25612 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025613 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025615 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616 return -1;
25617 }
25618 *fnamelen = l;
25619 }
25620 }
25621#endif /* WIN3264 */
25622
25623 /* ":t" - tail, just the basename */
25624 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25625 {
25626 *usedlen += 2;
25627 *fnamelen -= (int)(tail - *fnamep);
25628 *fnamep = tail;
25629 }
25630
25631 /* ":e" - extension, can be repeated */
25632 /* ":r" - root, without extension, can be repeated */
25633 while (src[*usedlen] == ':'
25634 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25635 {
25636 /* find a '.' in the tail:
25637 * - for second :e: before the current fname
25638 * - otherwise: The last '.'
25639 */
25640 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25641 s = *fnamep - 2;
25642 else
25643 s = *fnamep + *fnamelen - 1;
25644 for ( ; s > tail; --s)
25645 if (s[0] == '.')
25646 break;
25647 if (src[*usedlen + 1] == 'e') /* :e */
25648 {
25649 if (s > tail)
25650 {
25651 *fnamelen += (int)(*fnamep - (s + 1));
25652 *fnamep = s + 1;
25653#ifdef VMS
25654 /* cut version from the extension */
25655 s = *fnamep + *fnamelen - 1;
25656 for ( ; s > *fnamep; --s)
25657 if (s[0] == ';')
25658 break;
25659 if (s > *fnamep)
25660 *fnamelen = s - *fnamep;
25661#endif
25662 }
25663 else if (*fnamep <= tail)
25664 *fnamelen = 0;
25665 }
25666 else /* :r */
25667 {
25668 if (s > tail) /* remove one extension */
25669 *fnamelen = (int)(s - *fnamep);
25670 }
25671 *usedlen += 2;
25672 }
25673
25674 /* ":s?pat?foo?" - substitute */
25675 /* ":gs?pat?foo?" - global substitute */
25676 if (src[*usedlen] == ':'
25677 && (src[*usedlen + 1] == 's'
25678 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25679 {
25680 char_u *str;
25681 char_u *pat;
25682 char_u *sub;
25683 int sep;
25684 char_u *flags;
25685 int didit = FALSE;
25686
25687 flags = (char_u *)"";
25688 s = src + *usedlen + 2;
25689 if (src[*usedlen + 1] == 'g')
25690 {
25691 flags = (char_u *)"g";
25692 ++s;
25693 }
25694
25695 sep = *s++;
25696 if (sep)
25697 {
25698 /* find end of pattern */
25699 p = vim_strchr(s, sep);
25700 if (p != NULL)
25701 {
25702 pat = vim_strnsave(s, (int)(p - s));
25703 if (pat != NULL)
25704 {
25705 s = p + 1;
25706 /* find end of substitution */
25707 p = vim_strchr(s, sep);
25708 if (p != NULL)
25709 {
25710 sub = vim_strnsave(s, (int)(p - s));
25711 str = vim_strnsave(*fnamep, *fnamelen);
25712 if (sub != NULL && str != NULL)
25713 {
25714 *usedlen = (int)(p + 1 - src);
25715 s = do_string_sub(str, pat, sub, flags);
25716 if (s != NULL)
25717 {
25718 *fnamep = s;
25719 *fnamelen = (int)STRLEN(s);
25720 vim_free(*bufp);
25721 *bufp = s;
25722 didit = TRUE;
25723 }
25724 }
25725 vim_free(sub);
25726 vim_free(str);
25727 }
25728 vim_free(pat);
25729 }
25730 }
25731 /* after using ":s", repeat all the modifiers */
25732 if (didit)
25733 goto repeat;
25734 }
25735 }
25736
Bram Moolenaar26df0922014-02-23 23:39:13 +010025737 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25738 {
25739 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25740 if (p == NULL)
25741 return -1;
25742 vim_free(*bufp);
25743 *bufp = *fnamep = p;
25744 *fnamelen = (int)STRLEN(p);
25745 *usedlen += 2;
25746 }
25747
Bram Moolenaar071d4272004-06-13 20:20:40 +000025748 return valid;
25749}
25750
25751/*
25752 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25753 * "flags" can be "g" to do a global substitute.
25754 * Returns an allocated string, NULL for error.
25755 */
25756 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025757do_string_sub(
25758 char_u *str,
25759 char_u *pat,
25760 char_u *sub,
25761 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762{
25763 int sublen;
25764 regmatch_T regmatch;
25765 int i;
25766 int do_all;
25767 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025768 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 garray_T ga;
25770 char_u *ret;
25771 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025772 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773
25774 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25775 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025776 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777
25778 ga_init2(&ga, 1, 200);
25779
25780 do_all = (flags[0] == 'g');
25781
25782 regmatch.rm_ic = p_ic;
25783 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25784 if (regmatch.regprog != NULL)
25785 {
25786 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025787 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25789 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025790 /* Skip empty match except for first match. */
25791 if (regmatch.startp[0] == regmatch.endp[0])
25792 {
25793 if (zero_width == regmatch.startp[0])
25794 {
25795 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025796 i = MB_PTR2LEN(tail);
25797 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25798 (size_t)i);
25799 ga.ga_len += i;
25800 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025801 continue;
25802 }
25803 zero_width = regmatch.startp[0];
25804 }
25805
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 /*
25807 * Get some space for a temporary buffer to do the substitution
25808 * into. It will contain:
25809 * - The text up to where the match is.
25810 * - The substituted text.
25811 * - The text after the match.
25812 */
25813 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025814 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25816 {
25817 ga_clear(&ga);
25818 break;
25819 }
25820
25821 /* copy the text up to where the match is */
25822 i = (int)(regmatch.startp[0] - tail);
25823 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25824 /* add the substituted text */
25825 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25826 + ga.ga_len + i, TRUE, TRUE, FALSE);
25827 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025828 tail = regmatch.endp[0];
25829 if (*tail == NUL)
25830 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025831 if (!do_all)
25832 break;
25833 }
25834
25835 if (ga.ga_data != NULL)
25836 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25837
Bram Moolenaar473de612013-06-08 18:19:48 +020025838 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839 }
25840
25841 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25842 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025843 if (p_cpo == empty_option)
25844 p_cpo = save_cpo;
25845 else
25846 /* Darn, evaluating {sub} expression changed the value. */
25847 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848
25849 return ret;
25850}
25851
25852#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */