blob: 787f0f03431e1f606dbbb88363bbc961d438942f [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);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100693#ifdef FEAT_FLOAT
694static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
695#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_reltimestr(typval_T *argvars, typval_T *rettv);
697static void f_remote_expr(typval_T *argvars, typval_T *rettv);
698static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
699static void f_remote_peek(typval_T *argvars, typval_T *rettv);
700static void f_remote_read(typval_T *argvars, typval_T *rettv);
701static void f_remote_send(typval_T *argvars, typval_T *rettv);
702static void f_remove(typval_T *argvars, typval_T *rettv);
703static void f_rename(typval_T *argvars, typval_T *rettv);
704static void f_repeat(typval_T *argvars, typval_T *rettv);
705static void f_resolve(typval_T *argvars, typval_T *rettv);
706static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000707#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_screenattr(typval_T *argvars, typval_T *rettv);
711static void f_screenchar(typval_T *argvars, typval_T *rettv);
712static void f_screencol(typval_T *argvars, typval_T *rettv);
713static void f_screenrow(typval_T *argvars, typval_T *rettv);
714static void f_search(typval_T *argvars, typval_T *rettv);
715static void f_searchdecl(typval_T *argvars, typval_T *rettv);
716static void f_searchpair(typval_T *argvars, typval_T *rettv);
717static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
718static void f_searchpos(typval_T *argvars, typval_T *rettv);
719static void f_server2client(typval_T *argvars, typval_T *rettv);
720static void f_serverlist(typval_T *argvars, typval_T *rettv);
721static void f_setbufvar(typval_T *argvars, typval_T *rettv);
722static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
723static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
724static void f_setline(typval_T *argvars, typval_T *rettv);
725static void f_setloclist(typval_T *argvars, typval_T *rettv);
726static void f_setmatches(typval_T *argvars, typval_T *rettv);
727static void f_setpos(typval_T *argvars, typval_T *rettv);
728static void f_setqflist(typval_T *argvars, typval_T *rettv);
729static void f_setreg(typval_T *argvars, typval_T *rettv);
730static void f_settabvar(typval_T *argvars, typval_T *rettv);
731static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
732static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100733#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100735#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_shellescape(typval_T *argvars, typval_T *rettv);
737static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
738static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100740static void f_sin(typval_T *argvars, typval_T *rettv);
741static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_sort(typval_T *argvars, typval_T *rettv);
744static void f_soundfold(typval_T *argvars, typval_T *rettv);
745static void f_spellbadword(typval_T *argvars, typval_T *rettv);
746static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
747static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000748#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_sqrt(typval_T *argvars, typval_T *rettv);
750static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_str2nr(typval_T *argvars, typval_T *rettv);
753static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000754#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_stridx(typval_T *argvars, typval_T *rettv);
758static void f_string(typval_T *argvars, typval_T *rettv);
759static void f_strlen(typval_T *argvars, typval_T *rettv);
760static void f_strpart(typval_T *argvars, typval_T *rettv);
761static void f_strridx(typval_T *argvars, typval_T *rettv);
762static void f_strtrans(typval_T *argvars, typval_T *rettv);
763static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
764static void f_strwidth(typval_T *argvars, typval_T *rettv);
765static void f_submatch(typval_T *argvars, typval_T *rettv);
766static void f_substitute(typval_T *argvars, typval_T *rettv);
767static void f_synID(typval_T *argvars, typval_T *rettv);
768static void f_synIDattr(typval_T *argvars, typval_T *rettv);
769static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
770static void f_synstack(typval_T *argvars, typval_T *rettv);
771static void f_synconcealed(typval_T *argvars, typval_T *rettv);
772static void f_system(typval_T *argvars, typval_T *rettv);
773static void f_systemlist(typval_T *argvars, typval_T *rettv);
774static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
775static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
776static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
777static void f_taglist(typval_T *argvars, typval_T *rettv);
778static void f_tagfiles(typval_T *argvars, typval_T *rettv);
779static void f_tempname(typval_T *argvars, typval_T *rettv);
780static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200781#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_tan(typval_T *argvars, typval_T *rettv);
783static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200784#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100785static void f_tolower(typval_T *argvars, typval_T *rettv);
786static void f_toupper(typval_T *argvars, typval_T *rettv);
787static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000788#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000790#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100791static void f_type(typval_T *argvars, typval_T *rettv);
792static void f_undofile(typval_T *argvars, typval_T *rettv);
793static void f_undotree(typval_T *argvars, typval_T *rettv);
794static void f_uniq(typval_T *argvars, typval_T *rettv);
795static void f_values(typval_T *argvars, typval_T *rettv);
796static void f_virtcol(typval_T *argvars, typval_T *rettv);
797static void f_visualmode(typval_T *argvars, typval_T *rettv);
798static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
799static void f_winbufnr(typval_T *argvars, typval_T *rettv);
800static void f_wincol(typval_T *argvars, typval_T *rettv);
801static void f_winheight(typval_T *argvars, typval_T *rettv);
802static void f_winline(typval_T *argvars, typval_T *rettv);
803static void f_winnr(typval_T *argvars, typval_T *rettv);
804static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
805static void f_winrestview(typval_T *argvars, typval_T *rettv);
806static void f_winsaveview(typval_T *argvars, typval_T *rettv);
807static void f_winwidth(typval_T *argvars, typval_T *rettv);
808static void f_writefile(typval_T *argvars, typval_T *rettv);
809static void f_wordcount(typval_T *argvars, typval_T *rettv);
810static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000811
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100812static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
813static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
814static int get_env_len(char_u **arg);
815static int get_id_len(char_u **arg);
816static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
817static 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 +0000818#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
819#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
820 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
822static int eval_isnamec(int c);
823static int eval_isnamec1(int c);
824static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
825static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static typval_T *alloc_string_tv(char_u *string);
827static void init_tv(typval_T *varp);
828static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100829#ifdef FEAT_FLOAT
830static float_T get_tv_float(typval_T *varp);
831#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static linenr_T get_tv_lnum(typval_T *argvars);
833static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
834static char_u *get_tv_string(typval_T *varp);
835static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
836static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
837static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
838static hashtab_T *find_var_ht(char_u *name, char_u **varname);
839static funccall_T *get_funccal(void);
840static void vars_clear_ext(hashtab_T *ht, int free_val);
841static void delete_var(hashtab_T *ht, hashitem_T *hi);
842static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
843static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
844static void set_var(char_u *name, typval_T *varp, int copy);
845static int var_check_ro(int flags, char_u *name, int use_gettext);
846static int var_check_fixed(int flags, char_u *name, int use_gettext);
847static int var_check_func_name(char_u *name, int new_var);
848static int valid_varname(char_u *varname);
849static int tv_check_lock(int lock, char_u *name, int use_gettext);
850static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
851static char_u *find_option_end(char_u **arg, int *opt_flags);
852static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
853static int eval_fname_script(char_u *p);
854static int eval_fname_sid(char_u *p);
855static void list_func_head(ufunc_T *fp, int indent);
856static ufunc_T *find_func(char_u *name);
857static int function_exists(char_u *name);
858static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000859#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100860static void func_do_profile(ufunc_T *fp);
861static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
862static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000863static int
864# ifdef __BORLANDC__
865 _RTLENTRYF
866# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100867 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000868static int
869# ifdef __BORLANDC__
870 _RTLENTRYF
871# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100872 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000873#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static int script_autoload(char_u *name, int reload);
875static char_u *autoload_name(char_u *name);
876static void cat_func_name(char_u *buf, ufunc_T *fp);
877static void func_free(ufunc_T *fp);
878static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
879static int can_free_funccal(funccall_T *fc, int copyID) ;
880static void free_funccal(funccall_T *fc, int free_val);
881static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
882static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
883static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
884static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
885static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
886static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
887static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
888static int write_list(FILE *fd, list_T *list, int binary);
889static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000890
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200891
892#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100893static int compare_func_name(const void *s1, const void *s2);
894static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200895#endif
896
Bram Moolenaar33570922005-01-25 22:26:29 +0000897/*
898 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000899 */
900 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100901eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000902{
Bram Moolenaar33570922005-01-25 22:26:29 +0000903 int i;
904 struct vimvar *p;
905
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200906 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
907 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200908 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000909 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000911
912 for (i = 0; i < VV_LEN; ++i)
913 {
914 p = &vimvars[i];
915 STRCPY(p->vv_di.di_key, p->vv_name);
916 if (p->vv_flags & VV_RO)
917 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
918 else if (p->vv_flags & VV_RO_SBX)
919 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
920 else
921 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000922
923 /* add to v: scope dict, unless the value is not always available */
924 if (p->vv_type != VAR_UNKNOWN)
925 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000926 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000927 /* add to compat scope dict */
928 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000929 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100930 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
931
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100933 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200934 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100935 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100936
937 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
938 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
939 set_vim_var_nr(VV_NONE, VVAL_NONE);
940 set_vim_var_nr(VV_NULL, VVAL_NULL);
941
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200942 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200943
944#ifdef EBCDIC
945 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100946 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200947 */
948 sortFunctions();
949#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000950}
951
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952#if defined(EXITFREE) || defined(PROTO)
953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100954eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000955{
956 int i;
957 struct vimvar *p;
958
959 for (i = 0; i < VV_LEN; ++i)
960 {
961 p = &vimvars[i];
962 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000963 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000964 vim_free(p->vv_str);
965 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000966 }
967 else if (p->vv_di.di_tv.v_type == VAR_LIST)
968 {
969 list_unref(p->vv_list);
970 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000971 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972 }
973 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000974 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000975 hash_clear(&compat_hashtab);
976
Bram Moolenaard9fba312005-06-26 22:34:35 +0000977 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100978# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200979 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100980# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000981
982 /* global variables */
983 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000984
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000985 /* autoloaded script names */
986 ga_clear_strings(&ga_loaded);
987
Bram Moolenaarcca74132013-09-25 21:00:28 +0200988 /* Script-local variables. First clear all the variables and in a second
989 * loop free the scriptvar_T, because a variable in one script might hold
990 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200991 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200992 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200993 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200994 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200995 ga_clear(&ga_scripts);
996
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000997 /* unreferenced lists and dicts */
998 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000999
1000 /* functions */
1001 free_all_functions();
1002 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001003}
1004#endif
1005
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * Return the name of the executed function.
1008 */
1009 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001010func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014
1015/*
1016 * Return the address holding the next breakpoint line for a funccall cookie.
1017 */
1018 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001019func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar33570922005-01-25 22:26:29 +00001021 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/*
1025 * Return the address holding the debug tick for a funccall cookie.
1026 */
1027 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001028func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
1034 * Return the nesting level for a funccall cookie.
1035 */
1036 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001037func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001043funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001045/* pointer to list of previously used funccal, still around because some
1046 * item in it is still being used. */
1047funccall_T *previous_funccal = NULL;
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049/*
1050 * Return TRUE when a function was ended by a ":return" command.
1051 */
1052 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001053current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
1055 return current_funccal->returned;
1056}
1057
1058
1059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 * Set an internal variable to a string value. Creates the variable if it does
1061 * not already exist.
1062 */
1063 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001064set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001066 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001067 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069 val = vim_strsave(value);
1070 if (val != NULL)
1071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001072 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001073 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001075 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 }
1079}
1080
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001082static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083static char_u *redir_endp = NULL;
1084static char_u *redir_varname = NULL;
1085
1086/*
1087 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001088 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 * Returns OK if successfully completed the setup. FAIL otherwise.
1090 */
1091 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001092var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093{
1094 int save_emsg;
1095 int err;
1096 typval_T tv;
1097
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001098 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001099 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 {
1101 EMSG(_(e_invarg));
1102 return FAIL;
1103 }
1104
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001105 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 redir_varname = vim_strsave(name);
1107 if (redir_varname == NULL)
1108 return FAIL;
1109
1110 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1111 if (redir_lval == NULL)
1112 {
1113 var_redir_stop();
1114 return FAIL;
1115 }
1116
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 /* The output is stored in growarray "redir_ga" until redirection ends. */
1118 ga_init2(&redir_ga, (int)sizeof(char), 500);
1119
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001121 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001122 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1124 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001125 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 if (redir_endp != NULL && *redir_endp != NUL)
1127 /* Trailing characters are present after the variable name */
1128 EMSG(_(e_trailing));
1129 else
1130 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001131 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 var_redir_stop();
1133 return FAIL;
1134 }
1135
1136 /* check if we can write to the variable: set it to or append an empty
1137 * string */
1138 save_emsg = did_emsg;
1139 did_emsg = FALSE;
1140 tv.v_type = VAR_STRING;
1141 tv.vval.v_string = (char_u *)"";
1142 if (append)
1143 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1144 else
1145 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001146 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001148 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 if (err)
1150 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001151 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 var_redir_stop();
1153 return FAIL;
1154 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155
1156 return OK;
1157}
1158
1159/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160 * Append "value[value_len]" to the variable set by var_redir_start().
1161 * The actual appending is postponed until redirection ends, because the value
1162 * appended may in fact be the string we write to, changing it may cause freed
1163 * memory to be used:
1164 * :redir => foo
1165 * :let foo
1166 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 */
1168 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001169var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001171 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172
1173 if (redir_lval == NULL)
1174 return;
1175
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001176 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001177 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001179 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001181 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001182 {
1183 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001184 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185 }
1186 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188}
1189
1190/*
1191 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001192 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193 */
1194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001195var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197 typval_T tv;
1198
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199 if (redir_lval != NULL)
1200 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 /* If there was no error: assign the text to the variable. */
1202 if (redir_endp != NULL)
1203 {
1204 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1205 tv.v_type = VAR_STRING;
1206 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001207 /* Call get_lval() again, if it's inside a Dict or List it may
1208 * have changed. */
1209 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001210 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001211 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1212 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1213 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001214 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001215
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001216 /* free the collected output */
1217 vim_free(redir_ga.ga_data);
1218 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001219
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220 vim_free(redir_lval);
1221 redir_lval = NULL;
1222 }
1223 vim_free(redir_varname);
1224 redir_varname = NULL;
1225}
1226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227# if defined(FEAT_MBYTE) || defined(PROTO)
1228 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001229eval_charconvert(
1230 char_u *enc_from,
1231 char_u *enc_to,
1232 char_u *fname_from,
1233 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234{
1235 int err = FALSE;
1236
1237 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1238 set_vim_var_string(VV_CC_TO, enc_to, -1);
1239 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1240 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1241 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1242 err = TRUE;
1243 set_vim_var_string(VV_CC_FROM, NULL, -1);
1244 set_vim_var_string(VV_CC_TO, NULL, -1);
1245 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1246 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1247
1248 if (err)
1249 return FAIL;
1250 return OK;
1251}
1252# endif
1253
1254# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1255 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001256eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
1258 int err = FALSE;
1259
1260 set_vim_var_string(VV_FNAME_IN, fname, -1);
1261 set_vim_var_string(VV_CMDARG, args, -1);
1262 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1263 err = TRUE;
1264 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1265 set_vim_var_string(VV_CMDARG, NULL, -1);
1266
1267 if (err)
1268 {
1269 mch_remove(fname);
1270 return FAIL;
1271 }
1272 return OK;
1273}
1274# endif
1275
1276# if defined(FEAT_DIFF) || defined(PROTO)
1277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278eval_diff(
1279 char_u *origfile,
1280 char_u *newfile,
1281 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282{
1283 int err = FALSE;
1284
1285 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1286 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1287 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1288 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1289 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1290 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1291 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1292}
1293
1294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001295eval_patch(
1296 char_u *origfile,
1297 char_u *difffile,
1298 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 int err;
1301
1302 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1303 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1304 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1305 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1306 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1307 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1308 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1309}
1310# endif
1311
1312/*
1313 * Top level evaluation function, returning a boolean.
1314 * Sets "error" to TRUE if there was an error.
1315 * Return TRUE or FALSE.
1316 */
1317 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318eval_to_bool(
1319 char_u *arg,
1320 int *error,
1321 char_u **nextcmd,
1322 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaar33570922005-01-25 22:26:29 +00001324 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 int retval = FALSE;
1326
1327 if (skip)
1328 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 else
1332 {
1333 *error = FALSE;
1334 if (!skip)
1335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001336 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
1339 }
1340 if (skip)
1341 --emsg_skip;
1342
1343 return retval;
1344}
1345
1346/*
1347 * Top level evaluation function, returning a string. If "skip" is TRUE,
1348 * only parsing to "nextcmd" is done, without reporting errors. Return
1349 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1350 */
1351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001352eval_to_string_skip(
1353 char_u *arg,
1354 char_u **nextcmd,
1355 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
Bram Moolenaar33570922005-01-25 22:26:29 +00001357 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 char_u *retval;
1359
1360 if (skip)
1361 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 retval = NULL;
1364 else
1365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 retval = vim_strsave(get_tv_string(&tv));
1367 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369 if (skip)
1370 --emsg_skip;
1371
1372 return retval;
1373}
1374
1375/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001376 * Skip over an expression at "*pp".
1377 * Return FAIL for an error, OK otherwise.
1378 */
1379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001380skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001381{
Bram Moolenaar33570922005-01-25 22:26:29 +00001382 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001383
1384 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001385 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001386}
1387
1388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001390 * When "convert" is TRUE convert a List into a sequence of lines and convert
1391 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 * Return pointer to allocated memory, or NULL for failure.
1393 */
1394 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001395eval_to_string(
1396 char_u *arg,
1397 char_u **nextcmd,
1398 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
Bram Moolenaar33570922005-01-25 22:26:29 +00001400 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001402 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001403#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001404 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 retval = NULL;
1409 else
1410 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001411 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001412 {
1413 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001414 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001415 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001416 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001417 if (tv.vval.v_list->lv_len > 0)
1418 ga_append(&ga, NL);
1419 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001420 ga_append(&ga, NUL);
1421 retval = (char_u *)ga.ga_data;
1422 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001423#ifdef FEAT_FLOAT
1424 else if (convert && tv.v_type == VAR_FLOAT)
1425 {
1426 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1427 retval = vim_strsave(numbuf);
1428 }
1429#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001430 else
1431 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
1434
1435 return retval;
1436}
1437
1438/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001439 * Call eval_to_string() without using current local variables and using
1440 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 */
1442 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001443eval_to_string_safe(
1444 char_u *arg,
1445 char_u **nextcmd,
1446 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 char_u *retval;
1449 void *save_funccalp;
1450
1451 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001452 if (use_sandbox)
1453 ++sandbox;
1454 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001455 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001456 if (use_sandbox)
1457 --sandbox;
1458 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 restore_funccal(save_funccalp);
1460 return retval;
1461}
1462
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463/*
1464 * Top level evaluation function, returning a number.
1465 * Evaluates "expr" silently.
1466 * Returns -1 for an error.
1467 */
1468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001469eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
Bram Moolenaar33570922005-01-25 22:26:29 +00001471 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001473 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
1475 ++emsg_off;
1476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001477 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 retval = -1;
1479 else
1480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001481 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484 --emsg_off;
1485
1486 return retval;
1487}
1488
Bram Moolenaara40058a2005-07-11 22:42:07 +00001489/*
1490 * Prepare v: variable "idx" to be used.
1491 * Save the current typeval in "save_tv".
1492 * When not used yet add the variable to the v: hashtable.
1493 */
1494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001496{
1497 *save_tv = vimvars[idx].vv_tv;
1498 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1499 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1500}
1501
1502/*
1503 * Restore v: variable "idx" to typeval "save_tv".
1504 * When no longer defined, remove the variable from the v: hashtable.
1505 */
1506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001507restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001508{
1509 hashitem_T *hi;
1510
Bram Moolenaara40058a2005-07-11 22:42:07 +00001511 vimvars[idx].vv_tv = *save_tv;
1512 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1513 {
1514 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1515 if (HASHITEM_EMPTY(hi))
1516 EMSG2(_(e_intern2), "restore_vimvar()");
1517 else
1518 hash_remove(&vimvarht, hi);
1519 }
1520}
1521
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001522#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001523/*
1524 * Evaluate an expression to a list with suggestions.
1525 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001526 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001527 */
1528 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001529eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001530{
1531 typval_T save_val;
1532 typval_T rettv;
1533 list_T *list = NULL;
1534 char_u *p = skipwhite(expr);
1535
1536 /* Set "v:val" to the bad word. */
1537 prepare_vimvar(VV_VAL, &save_val);
1538 vimvars[VV_VAL].vv_type = VAR_STRING;
1539 vimvars[VV_VAL].vv_str = badword;
1540 if (p_verbose == 0)
1541 ++emsg_off;
1542
1543 if (eval1(&p, &rettv, TRUE) == OK)
1544 {
1545 if (rettv.v_type != VAR_LIST)
1546 clear_tv(&rettv);
1547 else
1548 list = rettv.vval.v_list;
1549 }
1550
1551 if (p_verbose == 0)
1552 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001553 restore_vimvar(VV_VAL, &save_val);
1554
1555 return list;
1556}
1557
1558/*
1559 * "list" is supposed to contain two items: a word and a number. Return the
1560 * word in "pp" and the number as the return value.
1561 * Return -1 if anything isn't right.
1562 * Used to get the good word and score from the eval_spell_expr() result.
1563 */
1564 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001565get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001566{
1567 listitem_T *li;
1568
1569 li = list->lv_first;
1570 if (li == NULL)
1571 return -1;
1572 *pp = get_tv_string(&li->li_tv);
1573
1574 li = li->li_next;
1575 if (li == NULL)
1576 return -1;
1577 return get_tv_number(&li->li_tv);
1578}
1579#endif
1580
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001581/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001582 * Top level evaluation function.
1583 * Returns an allocated typval_T with the result.
1584 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001585 */
1586 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001587eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001588{
1589 typval_T *tv;
1590
1591 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001592 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001593 {
1594 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001595 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001596 }
1597
1598 return tv;
1599}
1600
1601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001604 * Uses argv[argc] for the function arguments. Only Number and String
1605 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001606 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001609call_vim_function(
1610 char_u *func,
1611 int argc,
1612 char_u **argv,
1613 int safe, /* use the sandbox */
1614 int str_arg_only, /* all arguments are strings */
1615 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
Bram Moolenaar33570922005-01-25 22:26:29 +00001617 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 long n;
1619 int len;
1620 int i;
1621 int doesrange;
1622 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001625 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 for (i = 0; i < argc; i++)
1630 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001631 /* Pass a NULL or empty argument as an empty string */
1632 if (argv[i] == NULL || *argv[i] == NUL)
1633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001634 argvars[i].v_type = VAR_STRING;
1635 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001636 continue;
1637 }
1638
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001639 if (str_arg_only)
1640 len = 0;
1641 else
1642 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001643 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (len != 0 && len == (int)STRLEN(argv[i]))
1645 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001646 argvars[i].v_type = VAR_NUMBER;
1647 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
1649 else
1650 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001651 argvars[i].v_type = VAR_STRING;
1652 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654 }
1655
1656 if (safe)
1657 {
1658 save_funccalp = save_funccal();
1659 ++sandbox;
1660 }
1661
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1663 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (safe)
1667 {
1668 --sandbox;
1669 restore_funccal(save_funccalp);
1670 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 vim_free(argvars);
1672
1673 if (ret == FAIL)
1674 clear_tv(rettv);
1675
1676 return ret;
1677}
1678
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001679/*
1680 * Call vimL function "func" and return the result as a number.
1681 * Returns -1 when calling the function fails.
1682 * Uses argv[argc] for the function arguments.
1683 */
1684 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001685call_func_retnr(
1686 char_u *func,
1687 int argc,
1688 char_u **argv,
1689 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001690{
1691 typval_T rettv;
1692 long retval;
1693
1694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1696 return -1;
1697
1698 retval = get_tv_number_chk(&rettv, NULL);
1699 clear_tv(&rettv);
1700 return retval;
1701}
1702
1703#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1704 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1705
Bram Moolenaar4f688582007-07-24 12:34:30 +00001706# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001708 * Call vimL function "func" and return the result as a string.
1709 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001710 * Uses argv[argc] for the function arguments.
1711 */
1712 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001713call_func_retstr(
1714 char_u *func,
1715 int argc,
1716 char_u **argv,
1717 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718{
1719 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001720 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001722 /* All arguments are passed as strings, no conversion to number. */
1723 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 return NULL;
1725
1726 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 return retval;
1729}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001730# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001732/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001733 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001735 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 */
1737 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001738call_func_retlist(
1739 char_u *func,
1740 int argc,
1741 char_u **argv,
1742 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743{
1744 typval_T rettv;
1745
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001746 /* All arguments are passed as strings, no conversion to number. */
1747 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 return NULL;
1749
1750 if (rettv.v_type != VAR_LIST)
1751 {
1752 clear_tv(&rettv);
1753 return NULL;
1754 }
1755
1756 return rettv.vval.v_list;
1757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#endif
1759
1760/*
1761 * Save the current function call pointer, and set it to NULL.
1762 * Used when executing autocommands and for ":source".
1763 */
1764 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001767 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 current_funccal = NULL;
1770 return (void *)fc;
1771}
1772
1773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001776 funccall_T *fc = (funccall_T *)vfc;
1777
1778 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779}
1780
Bram Moolenaar05159a02005-02-26 23:04:13 +00001781#if defined(FEAT_PROFILE) || defined(PROTO)
1782/*
1783 * Prepare profiling for entering a child or something else that is not
1784 * counted for the script/function itself.
1785 * Should always be called in pair with prof_child_exit().
1786 */
1787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788prof_child_enter(
1789 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790{
1791 funccall_T *fc = current_funccal;
1792
1793 if (fc != NULL && fc->func->uf_profiling)
1794 profile_start(&fc->prof_child);
1795 script_prof_save(tm);
1796}
1797
1798/*
1799 * Take care of time spent in a child.
1800 * Should always be called after prof_child_enter().
1801 */
1802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803prof_child_exit(
1804 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001805{
1806 funccall_T *fc = current_funccal;
1807
1808 if (fc != NULL && fc->func->uf_profiling)
1809 {
1810 profile_end(&fc->prof_child);
1811 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1812 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1813 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1814 }
1815 script_prof_restore(tm);
1816}
1817#endif
1818
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#ifdef FEAT_FOLDING
1821/*
1822 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1823 * it in "*cp". Doesn't give error messages.
1824 */
1825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001826eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
Bram Moolenaar33570922005-01-25 22:26:29 +00001828 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 int retval;
1830 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001831 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1832 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 ++sandbox;
1837 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 retval = 0;
1841 else
1842 {
1843 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 if (tv.v_type == VAR_NUMBER)
1845 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001846 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 retval = 0;
1848 else
1849 {
1850 /* If the result is a string, check if there is a non-digit before
1851 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (!VIM_ISDIGIT(*s) && *s != '-')
1854 *cp = *s++;
1855 retval = atol((char *)s);
1856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001860 if (use_sandbox)
1861 --sandbox;
1862 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864 return retval;
1865}
1866#endif
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 * ":let" list all variable values
1870 * ":let var1 var2" list variable values
1871 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872 * ":let var += expr" assignment command.
1873 * ":let var -= expr" assignment command.
1874 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 */
1877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001878ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879{
1880 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 int var_count = 0;
1885 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001887 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001888 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
Bram Moolenaardb552d602006-03-23 22:59:57 +00001890 argend = skip_var_list(arg, &var_count, &semicolon);
1891 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001893 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1894 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 expr = skipwhite(argend);
1896 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1897 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001899 /*
1900 * ":let" without "=": list variables
1901 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 if (*arg == '[')
1903 EMSG(_(e_invarg));
1904 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001910 list_glob_vars(&first);
1911 list_buf_vars(&first);
1912 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001913#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001914 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001915#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001916 list_script_vars(&first);
1917 list_func_vars(&first);
1918 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 eap->nextcmd = check_nextcmd(arg);
1921 }
1922 else
1923 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 op[0] = '=';
1925 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001926 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001928 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1929 op[0] = *expr; /* +=, -= or .= */
1930 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001932 else
1933 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (eap->skip)
1936 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (eap->skip)
1939 {
1940 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 --emsg_skip;
1943 }
1944 else if (i != FAIL)
1945 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950 }
1951}
1952
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953/*
1954 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1955 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 * When "nextchars" is not NULL it points to a string with characters that
1957 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1958 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 * Returns OK or FAIL;
1960 */
1961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001962ex_let_vars(
1963 char_u *arg_start,
1964 typval_T *tv,
1965 int copy, /* copy values from "tv", don't move */
1966 int semicolon, /* from skip_var_list() */
1967 int var_count, /* from skip_var_list() */
1968 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969{
1970 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001971 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001973 listitem_T *item;
1974 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975
1976 if (*arg != '[')
1977 {
1978 /*
1979 * ":let var = expr" or ":for var in list"
1980 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 return FAIL;
1983 return OK;
1984 }
1985
1986 /*
1987 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1988 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001989 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 {
1991 EMSG(_(e_listreq));
1992 return FAIL;
1993 }
1994
1995 i = list_len(l);
1996 if (semicolon == 0 && var_count < i)
1997 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001998 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 return FAIL;
2000 }
2001 if (var_count - semicolon > i)
2002 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002003 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 return FAIL;
2005 }
2006
2007 item = l->lv_first;
2008 while (*arg != ']')
2009 {
2010 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 item = item->li_next;
2013 if (arg == NULL)
2014 return FAIL;
2015
2016 arg = skipwhite(arg);
2017 if (*arg == ';')
2018 {
2019 /* Put the rest of the list (may be empty) in the var after ';'.
2020 * Create a new list for this. */
2021 l = list_alloc();
2022 if (l == NULL)
2023 return FAIL;
2024 while (item != NULL)
2025 {
2026 list_append_tv(l, &item->li_tv);
2027 item = item->li_next;
2028 }
2029
2030 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002031 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 ltv.vval.v_list = l;
2033 l->lv_refcount = 1;
2034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2036 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 clear_tv(&ltv);
2038 if (arg == NULL)
2039 return FAIL;
2040 break;
2041 }
2042 else if (*arg != ',' && *arg != ']')
2043 {
2044 EMSG2(_(e_intern2), "ex_let_vars()");
2045 return FAIL;
2046 }
2047 }
2048
2049 return OK;
2050}
2051
2052/*
2053 * Skip over assignable variable "var" or list of variables "[var, var]".
2054 * Used for ":let varvar = expr" and ":for varvar in expr".
2055 * For "[var, var]" increment "*var_count" for each variable.
2056 * for "[var, var; var]" set "semicolon".
2057 * Return NULL for an error.
2058 */
2059 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002060skip_var_list(
2061 char_u *arg,
2062 int *var_count,
2063 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064{
2065 char_u *p, *s;
2066
2067 if (*arg == '[')
2068 {
2069 /* "[var, var]": find the matching ']'. */
2070 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002071 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 {
2073 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2074 s = skip_var_one(p);
2075 if (s == p)
2076 {
2077 EMSG2(_(e_invarg2), p);
2078 return NULL;
2079 }
2080 ++*var_count;
2081
2082 p = skipwhite(s);
2083 if (*p == ']')
2084 break;
2085 else if (*p == ';')
2086 {
2087 if (*semicolon == 1)
2088 {
2089 EMSG(_("Double ; in list of variables"));
2090 return NULL;
2091 }
2092 *semicolon = 1;
2093 }
2094 else if (*p != ',')
2095 {
2096 EMSG2(_(e_invarg2), p);
2097 return NULL;
2098 }
2099 }
2100 return p + 1;
2101 }
2102 else
2103 return skip_var_one(arg);
2104}
2105
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002107 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002108 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002111skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002112{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002113 if (*arg == '@' && arg[1] != NUL)
2114 return arg + 2;
2115 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2116 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117}
2118
Bram Moolenaara7043832005-01-21 11:56:39 +00002119/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 * List variables for hashtab "ht" with prefix "prefix".
2121 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002124list_hashtable_vars(
2125 hashtab_T *ht,
2126 char_u *prefix,
2127 int empty,
2128 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002129{
Bram Moolenaar33570922005-01-25 22:26:29 +00002130 hashitem_T *hi;
2131 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002132 int todo;
2133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002134 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002135 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2136 {
2137 if (!HASHITEM_EMPTY(hi))
2138 {
2139 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 di = HI2DI(hi);
2141 if (empty || di->di_tv.v_type != VAR_STRING
2142 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002144 }
2145 }
2146}
2147
2148/*
2149 * List global variables.
2150 */
2151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002152list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002153{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002155}
2156
2157/*
2158 * List buffer variables.
2159 */
2160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002162{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163 char_u numbuf[NUMBUFLEN];
2164
Bram Moolenaar429fa852013-04-15 12:27:36 +02002165 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002167
2168 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2170 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002171}
2172
2173/*
2174 * List window variables.
2175 */
2176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002178{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002181}
2182
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002183#ifdef FEAT_WINDOWS
2184/*
2185 * List tab page variables.
2186 */
2187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002190 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002192}
2193#endif
2194
Bram Moolenaara7043832005-01-21 11:56:39 +00002195/*
2196 * List Vim variables.
2197 */
2198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202}
2203
2204/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002205 * List script-local variables, if there is a script.
2206 */
2207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002209{
2210 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2212 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002213}
2214
2215/*
2216 * List function variables, if there is a function.
2217 */
2218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002219list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220{
2221 if (current_funccal != NULL)
2222 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224}
2225
2226/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 * List variables in "arg".
2228 */
2229 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002230list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231{
2232 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 char_u *name_start;
2236 char_u *arg_subsc;
2237 char_u *tofree;
2238 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239
2240 while (!ends_excmd(*arg) && !got_int)
2241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002244 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2246 {
2247 emsg_severe = TRUE;
2248 EMSG(_(e_trailing));
2249 break;
2250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* get_name_len() takes care of expanding curly braces */
2255 name_start = name = arg;
2256 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2257 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 /* This is mainly to keep test 49 working: when expanding
2260 * curly braces fails overrule the exception error message. */
2261 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 emsg_severe = TRUE;
2264 EMSG2(_(e_invarg2), arg);
2265 break;
2266 }
2267 error = TRUE;
2268 }
2269 else
2270 {
2271 if (tofree != NULL)
2272 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002273 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 else
2276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 /* handle d.key, l[idx], f(expr) */
2278 arg_subsc = arg;
2279 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002280 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002287 case 'g': list_glob_vars(first); break;
2288 case 'b': list_buf_vars(first); break;
2289 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002290#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002291 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002292#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002293 case 'v': list_vim_vars(first); break;
2294 case 's': list_script_vars(first); break;
2295 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 default:
2297 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
2300 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 {
2302 char_u numbuf[NUMBUFLEN];
2303 char_u *tf;
2304 int c;
2305 char_u *s;
2306
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002307 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 c = *arg;
2309 *arg = NUL;
2310 list_one_var_a((char_u *)"",
2311 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002312 tv.v_type,
2313 s == NULL ? (char_u *)"" : s,
2314 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 *arg = c;
2316 vim_free(tf);
2317 }
2318 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
2321 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322
2323 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325
2326 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
2328
2329 return arg;
2330}
2331
2332/*
2333 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2334 * Returns a pointer to the char just after the var name.
2335 * Returns NULL if there is an error.
2336 */
2337 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002338ex_let_one(
2339 char_u *arg, /* points to variable name */
2340 typval_T *tv, /* value to assign to variable */
2341 int copy, /* copy value from "tv" */
2342 char_u *endchars, /* valid chars after variable name or NULL */
2343 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344{
2345 int c1;
2346 char_u *name;
2347 char_u *p;
2348 char_u *arg_end = NULL;
2349 int len;
2350 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002351 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352
2353 /*
2354 * ":let $VAR = expr": Set environment variable.
2355 */
2356 if (*arg == '$')
2357 {
2358 /* Find the end of the name. */
2359 ++arg;
2360 name = arg;
2361 len = get_env_len(&arg);
2362 if (len == 0)
2363 EMSG2(_(e_invarg2), name - 1);
2364 else
2365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 if (op != NULL && (*op == '+' || *op == '-'))
2367 EMSG2(_(e_letwrong), op);
2368 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002369 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002371 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 {
2373 c1 = name[len];
2374 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002375 p = get_tv_string_chk(tv);
2376 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 {
2378 int mustfree = FALSE;
2379 char_u *s = vim_getenv(name, &mustfree);
2380
2381 if (s != NULL)
2382 {
2383 p = tofree = concat_str(s, p);
2384 if (mustfree)
2385 vim_free(s);
2386 }
2387 }
2388 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002391 if (STRICMP(name, "HOME") == 0)
2392 init_homedir();
2393 else if (didset_vim && STRICMP(name, "VIM") == 0)
2394 didset_vim = FALSE;
2395 else if (didset_vimruntime
2396 && STRICMP(name, "VIMRUNTIME") == 0)
2397 didset_vimruntime = FALSE;
2398 arg_end = arg;
2399 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 }
2403 }
2404 }
2405
2406 /*
2407 * ":let &option = expr": Set option value.
2408 * ":let &l:option = expr": Set local option value.
2409 * ":let &g:option = expr": Set global option value.
2410 */
2411 else if (*arg == '&')
2412 {
2413 /* Find the end of the name. */
2414 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002415 if (p == NULL || (endchars != NULL
2416 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 EMSG(_(e_letunexp));
2418 else
2419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 long n;
2421 int opt_type;
2422 long numval;
2423 char_u *stringval = NULL;
2424 char_u *s;
2425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 c1 = *p;
2427 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428
2429 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002430 s = get_tv_string_chk(tv); /* != NULL if number or string */
2431 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 {
2433 opt_type = get_option_value(arg, &numval,
2434 &stringval, opt_flags);
2435 if ((opt_type == 1 && *op == '.')
2436 || (opt_type == 0 && *op != '.'))
2437 EMSG2(_(e_letwrong), op);
2438 else
2439 {
2440 if (opt_type == 1) /* number */
2441 {
2442 if (*op == '+')
2443 n = numval + n;
2444 else
2445 n = numval - n;
2446 }
2447 else if (opt_type == 0 && stringval != NULL) /* string */
2448 {
2449 s = concat_str(stringval, s);
2450 vim_free(stringval);
2451 stringval = s;
2452 }
2453 }
2454 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 if (s != NULL)
2456 {
2457 set_option_value(arg, n, s, opt_flags);
2458 arg_end = p;
2459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 }
2463 }
2464
2465 /*
2466 * ":let @r = expr": Set register contents.
2467 */
2468 else if (*arg == '@')
2469 {
2470 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 if (op != NULL && (*op == '+' || *op == '-'))
2472 EMSG2(_(e_letwrong), op);
2473 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002474 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 EMSG(_(e_letunexp));
2476 else
2477 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002478 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 char_u *s;
2480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 p = get_tv_string_chk(tv);
2482 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002484 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 if (s != NULL)
2486 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002487 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 vim_free(s);
2489 }
2490 }
2491 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002494 arg_end = arg + 1;
2495 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498 }
2499
2500 /*
2501 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002504 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002508 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2512 EMSG(_(e_letunexp));
2513 else
2514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 arg_end = p;
2517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 }
2521
2522 else
2523 EMSG2(_(e_invarg2), arg);
2524
2525 return arg_end;
2526}
2527
2528/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2530 */
2531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002532check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533{
2534 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2535 {
2536 EMSG2(_(e_readonlyvar), arg);
2537 return TRUE;
2538 }
2539 return FALSE;
2540}
2541
2542/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Get an lval: variable, Dict item or List item that can be assigned a value
2544 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2545 * "name.key", "name.key[expr]" etc.
2546 * Indexing only works if "name" is an existing List or Dictionary.
2547 * "name" points to the start of the name.
2548 * If "rettv" is not NULL it points to the value to be assigned.
2549 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2550 * wrong; must end in space or cmd separator.
2551 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002552 * flags:
2553 * GLV_QUIET: do not give error messages
2554 * GLV_NO_AUTOLOAD: do not use script autoloading
2555 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002557 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 */
2560 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002561get_lval(
2562 char_u *name,
2563 typval_T *rettv,
2564 lval_T *lp,
2565 int unlet,
2566 int skip,
2567 int flags, /* GLV_ values */
2568 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 char_u *expr_start, *expr_end;
2572 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 dictitem_T *v;
2574 typval_T var1;
2575 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002581 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585
2586 if (skip)
2587 {
2588 /* When skipping just find the end of the name. */
2589 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002590 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 }
2592
2593 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002594 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (expr_start != NULL)
2596 {
2597 /* Don't expand the name when we already know there is an error. */
2598 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2599 && *p != '[' && *p != '.')
2600 {
2601 EMSG(_(e_trailing));
2602 return NULL;
2603 }
2604
2605 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2606 if (lp->ll_exp_name == NULL)
2607 {
2608 /* Report an invalid expression in braces, unless the
2609 * expression evaluation has been cancelled due to an
2610 * aborting error, an interrupt, or an exception. */
2611 if (!aborting() && !quiet)
2612 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002613 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 EMSG2(_(e_invarg2), name);
2615 return NULL;
2616 }
2617 }
2618 lp->ll_name = lp->ll_exp_name;
2619 }
2620 else
2621 lp->ll_name = name;
2622
2623 /* Without [idx] or .key we are done. */
2624 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2625 return p;
2626
2627 cc = *p;
2628 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002629 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (v == NULL && !quiet)
2631 EMSG2(_(e_undefvar), lp->ll_name);
2632 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 if (v == NULL)
2634 return NULL;
2635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 /*
2637 * Loop until no more [idx] or .key is following.
2638 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2643 && !(lp->ll_tv->v_type == VAR_DICT
2644 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_("E689: Can only index a List or Dictionary"));
2648 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_("E708: [:] must come last"));
2654 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 len = -1;
2658 if (*p == '.')
2659 {
2660 key = p + 1;
2661 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2662 ;
2663 if (len == 0)
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_(e_emptykey));
2667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669 p = key + len;
2670 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671 else
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (*p == ':')
2676 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677 else
2678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 empty1 = FALSE;
2680 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var1) == NULL)
2683 {
2684 /* not a number or string */
2685 clear_tv(&var1);
2686 return NULL;
2687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689
2690 /* Optionally get the second index [ :expr]. */
2691 if (*p == ':')
2692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002696 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 if (!empty1)
2698 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (rettv != NULL && (rettv->v_type != VAR_LIST
2702 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!quiet)
2705 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
2710 p = skipwhite(p + 1);
2711 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 else
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 if (get_tv_string_chk(&var2) == NULL)
2723 {
2724 /* not a number or string */
2725 if (!empty1)
2726 clear_tv(&var1);
2727 clear_tv(&var2);
2728 return NULL;
2729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002735
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (!quiet)
2739 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (!empty1)
2741 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746
2747 /* Skip to past ']'. */
2748 ++p;
2749 }
2750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
2753 if (len == -1)
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (*key == NUL)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (!quiet)
2760 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 }
2764 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002765 lp->ll_list = NULL;
2766 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002767 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002769 /* When assigning to a scope dictionary check that a function and
2770 * variable name is valid (only variable name unless it is l: or
2771 * g: dictionary). Disallow overwriting a builtin function. */
2772 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 int prevval;
2775 int wrong;
2776
2777 if (len != -1)
2778 {
2779 prevval = key[len];
2780 key[len] = NUL;
2781 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002782 else
2783 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002784 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2785 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002787 || !valid_varname(key);
2788 if (len != -1)
2789 key[len] = prevval;
2790 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002791 return NULL;
2792 }
2793
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002796 /* Can't add "v:" variable. */
2797 if (lp->ll_dict == &vimvardict)
2798 {
2799 EMSG2(_(e_illvar), name);
2800 return NULL;
2801 }
2802
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002803 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002807 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 if (len == -1)
2809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (len == -1)
2817 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 p = NULL;
2820 break;
2821 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002822 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002823 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 return NULL;
2825
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 if (len == -1)
2827 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 }
2830 else
2831 {
2832 /*
2833 * Get the number and item for the only or first index of the List.
2834 */
2835 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 else
2838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002839 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 clear_tv(&var1);
2841 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002842 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_list = lp->ll_tv->vval.v_list;
2844 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2845 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002847 if (lp->ll_n1 < 0)
2848 {
2849 lp->ll_n1 = 0;
2850 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2851 }
2852 }
2853 if (lp->ll_li == NULL)
2854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 if (!quiet)
2858 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 }
2861
2862 /*
2863 * May need to find the item or absolute index for the second
2864 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 * When no index given: "lp->ll_empty2" is TRUE.
2866 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002870 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 }
2883
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2885 if (lp->ll_n1 < 0)
2886 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2887 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 {
2889 if (!quiet)
2890 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 return p;
2900}
2901
2902/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 */
2905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002906clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907{
2908 vim_free(lp->ll_exp_name);
2909 vim_free(lp->ll_newkey);
2910}
2911
2912/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002913 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 */
2917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002918set_var_lval(
2919 lval_T *lp,
2920 char_u *endp,
2921 typval_T *rettv,
2922 int copy,
2923 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924{
2925 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 listitem_T *ri;
2927 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928
2929 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 cc = *endp;
2934 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 if (op != NULL && *op != '=')
2936 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002939 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002940 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002941 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002942 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002944 if ((di == NULL
2945 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2946 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2947 FALSE)))
2948 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 set_var(lp->ll_name, &tv, FALSE);
2950 clear_tv(&tv);
2951 }
2952 }
2953 else
2954 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002956 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 else if (tv_check_lock(lp->ll_newkey == NULL
2959 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002960 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 else if (lp->ll_range)
2963 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002964 listitem_T *ll_li = lp->ll_li;
2965 int ll_n1 = lp->ll_n1;
2966
2967 /*
2968 * Check whether any of the list items is locked
2969 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002970 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002971 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002972 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002973 return;
2974 ri = ri->li_next;
2975 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2976 break;
2977 ll_li = ll_li->li_next;
2978 ++ll_n1;
2979 }
2980
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 /*
2982 * Assign the List values to the list items.
2983 */
2984 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002985 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 if (op != NULL && *op != '=')
2987 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2988 else
2989 {
2990 clear_tv(&lp->ll_li->li_tv);
2991 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2992 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 ri = ri->li_next;
2994 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2995 break;
2996 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002999 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 ri = NULL;
3002 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003003 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 lp->ll_li = lp->ll_li->li_next;
3006 ++lp->ll_n1;
3007 }
3008 if (ri != NULL)
3009 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003010 else if (lp->ll_empty2
3011 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 : lp->ll_n1 != lp->ll_n2)
3013 EMSG(_("E711: List value has not enough items"));
3014 }
3015 else
3016 {
3017 /*
3018 * Assign to a List or Dictionary item.
3019 */
3020 if (lp->ll_newkey != NULL)
3021 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 if (op != NULL && *op != '=')
3023 {
3024 EMSG2(_(e_letwrong), op);
3025 return;
3026 }
3027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003029 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 if (di == NULL)
3031 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003032 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3033 {
3034 vim_free(di);
3035 return;
3036 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003038 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 else if (op != NULL && *op != '=')
3040 {
3041 tv_op(lp->ll_tv, rettv, op);
3042 return;
3043 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003044 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003046
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 /*
3048 * Assign the value to the variable or list item.
3049 */
3050 if (copy)
3051 copy_tv(rettv, lp->ll_tv);
3052 else
3053 {
3054 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003055 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057 }
3058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059}
3060
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3063 * Returns OK or FAIL.
3064 */
3065 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003066tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067{
3068 long n;
3069 char_u numbuf[NUMBUFLEN];
3070 char_u *s;
3071
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003072 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3073 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3074 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 {
3076 switch (tv1->v_type)
3077 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003078 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 case VAR_DICT:
3080 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003081 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003082 case VAR_JOB:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003083 break;
3084
3085 case VAR_LIST:
3086 if (*op != '+' || tv2->v_type != VAR_LIST)
3087 break;
3088 /* List += List */
3089 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3090 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3091 return OK;
3092
3093 case VAR_NUMBER:
3094 case VAR_STRING:
3095 if (tv2->v_type == VAR_LIST)
3096 break;
3097 if (*op == '+' || *op == '-')
3098 {
3099 /* nr += nr or nr -= nr*/
3100 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003101#ifdef FEAT_FLOAT
3102 if (tv2->v_type == VAR_FLOAT)
3103 {
3104 float_T f = n;
3105
3106 if (*op == '+')
3107 f += tv2->vval.v_float;
3108 else
3109 f -= tv2->vval.v_float;
3110 clear_tv(tv1);
3111 tv1->v_type = VAR_FLOAT;
3112 tv1->vval.v_float = f;
3113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#endif
3116 {
3117 if (*op == '+')
3118 n += get_tv_number(tv2);
3119 else
3120 n -= get_tv_number(tv2);
3121 clear_tv(tv1);
3122 tv1->v_type = VAR_NUMBER;
3123 tv1->vval.v_number = n;
3124 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003125 }
3126 else
3127 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128 if (tv2->v_type == VAR_FLOAT)
3129 break;
3130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 /* str .= str */
3132 s = get_tv_string(tv1);
3133 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3134 clear_tv(tv1);
3135 tv1->v_type = VAR_STRING;
3136 tv1->vval.v_string = s;
3137 }
3138 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139
3140#ifdef FEAT_FLOAT
3141 case VAR_FLOAT:
3142 {
3143 float_T f;
3144
3145 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3146 && tv2->v_type != VAR_NUMBER
3147 && tv2->v_type != VAR_STRING))
3148 break;
3149 if (tv2->v_type == VAR_FLOAT)
3150 f = tv2->vval.v_float;
3151 else
3152 f = get_tv_number(tv2);
3153 if (*op == '+')
3154 tv1->vval.v_float += f;
3155 else
3156 tv1->vval.v_float -= f;
3157 }
3158 return OK;
3159#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003160 }
3161 }
3162
3163 EMSG2(_(e_letwrong), op);
3164 return FAIL;
3165}
3166
3167/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 * Add a watcher to a list.
3169 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003171list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172{
3173 lw->lw_next = l->lv_watch;
3174 l->lv_watch = lw;
3175}
3176
3177/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003178 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003179 * No warning when it isn't found...
3180 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003182list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183{
Bram Moolenaar33570922005-01-25 22:26:29 +00003184 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 lwp = &l->lv_watch;
3187 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3188 {
3189 if (lw == lwrem)
3190 {
3191 *lwp = lw->lw_next;
3192 break;
3193 }
3194 lwp = &lw->lw_next;
3195 }
3196}
3197
3198/*
3199 * Just before removing an item from a list: advance watchers to the next
3200 * item.
3201 */
3202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003203list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204{
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206
3207 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3208 if (lw->lw_item == item)
3209 lw->lw_item = item->li_next;
3210}
3211
3212/*
3213 * Evaluate the expression used in a ":for var in expr" command.
3214 * "arg" points to "var".
3215 * Set "*errp" to TRUE for an error, FALSE otherwise;
3216 * Return a pointer that holds the info. Null when there is an error.
3217 */
3218 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219eval_for_line(
3220 char_u *arg,
3221 int *errp,
3222 char_u **nextcmdp,
3223 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224{
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 typval_T tv;
3228 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229
3230 *errp = TRUE; /* default: there is an error */
3231
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 if (fi == NULL)
3234 return NULL;
3235
3236 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3237 if (expr == NULL)
3238 return fi;
3239
3240 expr = skipwhite(expr);
3241 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3242 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003243 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 return fi;
3245 }
3246
3247 if (skip)
3248 ++emsg_skip;
3249 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3250 {
3251 *errp = FALSE;
3252 if (!skip)
3253 {
3254 l = tv.vval.v_list;
3255 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 clear_tv(&tv);
3259 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 else
3261 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003262 /* No need to increment the refcount, it's already set for the
3263 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 fi->fi_list = l;
3265 list_add_watch(l, &fi->fi_lw);
3266 fi->fi_lw.lw_item = l->lv_first;
3267 }
3268 }
3269 }
3270 if (skip)
3271 --emsg_skip;
3272
3273 return fi;
3274}
3275
3276/*
3277 * Use the first item in a ":for" list. Advance to the next.
3278 * Assign the values to the variable (list). "arg" points to the first one.
3279 * Return TRUE when a valid item was found, FALSE when at end of list or
3280 * something wrong.
3281 */
3282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003283next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003285 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003287 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288
3289 item = fi->fi_lw.lw_item;
3290 if (item == NULL)
3291 result = FALSE;
3292 else
3293 {
3294 fi->fi_lw.lw_item = item->li_next;
3295 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3296 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3297 }
3298 return result;
3299}
3300
3301/*
3302 * Free the structure used to store info used by ":for".
3303 */
3304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306{
Bram Moolenaar33570922005-01-25 22:26:29 +00003307 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003309 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003310 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003312 list_unref(fi->fi_list);
3313 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 vim_free(fi);
3315}
3316
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3318
3319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320set_context_for_expression(
3321 expand_T *xp,
3322 char_u *arg,
3323 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325 int got_eq = FALSE;
3326 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 if (cmdidx == CMD_let)
3330 {
3331 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003332 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 {
3334 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003335 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 {
3337 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003338 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 if (vim_iswhite(*p))
3340 break;
3341 }
3342 return;
3343 }
3344 }
3345 else
3346 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3347 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 while ((xp->xp_pattern = vim_strpbrk(arg,
3349 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3350 {
3351 c = *xp->xp_pattern;
3352 if (c == '&')
3353 {
3354 c = xp->xp_pattern[1];
3355 if (c == '&')
3356 {
3357 ++xp->xp_pattern;
3358 xp->xp_context = cmdidx != CMD_let || got_eq
3359 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3360 }
3361 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003364 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3365 xp->xp_pattern += 2;
3366
3367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 else if (c == '$')
3370 {
3371 /* environment variable */
3372 xp->xp_context = EXPAND_ENV_VARS;
3373 }
3374 else if (c == '=')
3375 {
3376 got_eq = TRUE;
3377 xp->xp_context = EXPAND_EXPRESSION;
3378 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003379 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 && xp->xp_context == EXPAND_FUNCTIONS
3381 && vim_strchr(xp->xp_pattern, '(') == NULL)
3382 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003383 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 break;
3385 }
3386 else if (cmdidx != CMD_let || got_eq)
3387 {
3388 if (c == '"') /* string */
3389 {
3390 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3391 if (c == '\\' && xp->xp_pattern[1] != NUL)
3392 ++xp->xp_pattern;
3393 xp->xp_context = EXPAND_NOTHING;
3394 }
3395 else if (c == '\'') /* literal string */
3396 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003397 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3399 /* skip */ ;
3400 xp->xp_context = EXPAND_NOTHING;
3401 }
3402 else if (c == '|')
3403 {
3404 if (xp->xp_pattern[1] == '|')
3405 {
3406 ++xp->xp_pattern;
3407 xp->xp_context = EXPAND_EXPRESSION;
3408 }
3409 else
3410 xp->xp_context = EXPAND_COMMANDS;
3411 }
3412 else
3413 xp->xp_context = EXPAND_EXPRESSION;
3414 }
3415 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003416 /* Doesn't look like something valid, expand as an expression
3417 * anyway. */
3418 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 arg = xp->xp_pattern;
3420 if (*arg != NUL)
3421 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3422 /* skip */ ;
3423 }
3424 xp->xp_pattern = arg;
3425}
3426
3427#endif /* FEAT_CMDL_COMPL */
3428
3429/*
3430 * ":1,25call func(arg1, arg2)" function call.
3431 */
3432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003433ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
3435 char_u *arg = eap->arg;
3436 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003438 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003440 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 linenr_T lnum;
3442 int doesrange;
3443 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003446 if (eap->skip)
3447 {
3448 /* trans_function_name() doesn't work well when skipping, use eval0()
3449 * instead to skip to any following command, e.g. for:
3450 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003451 ++emsg_skip;
3452 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3453 clear_tv(&rettv);
3454 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003455 return;
3456 }
3457
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003458 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003459 if (fudi.fd_newkey != NULL)
3460 {
3461 /* Still need to give an error message for missing key. */
3462 EMSG2(_(e_dictkey), fudi.fd_newkey);
3463 vim_free(fudi.fd_newkey);
3464 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003465 if (tofree == NULL)
3466 return;
3467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003468 /* Increase refcount on dictionary, it could get deleted when evaluating
3469 * the arguments. */
3470 if (fudi.fd_dict != NULL)
3471 ++fudi.fd_dict->dv_refcount;
3472
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003473 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003474 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003475 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
Bram Moolenaar532c7802005-01-27 14:44:31 +00003477 /* Skip white space to allow ":call func ()". Not good, but required for
3478 * backward compatibility. */
3479 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
3482 if (*startarg != '(')
3483 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003484 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 goto end;
3486 }
3487
3488 /*
3489 * When skipping, evaluate the function once, to find the end of the
3490 * arguments.
3491 * When the function takes a range, this is discovered after the first
3492 * call, and the loop is broken.
3493 */
3494 if (eap->skip)
3495 {
3496 ++emsg_skip;
3497 lnum = eap->line2; /* do it once, also with an invalid range */
3498 }
3499 else
3500 lnum = eap->line1;
3501 for ( ; lnum <= eap->line2; ++lnum)
3502 {
3503 if (!eap->skip && eap->addr_count > 0)
3504 {
3505 curwin->w_cursor.lnum = lnum;
3506 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003507#ifdef FEAT_VIRTUALEDIT
3508 curwin->w_cursor.coladd = 0;
3509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003512 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003513 eap->line1, eap->line2, &doesrange,
3514 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 failed = TRUE;
3517 break;
3518 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003519
3520 /* Handle a function returning a Funcref, Dictionary or List. */
3521 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3522 {
3523 failed = TRUE;
3524 break;
3525 }
3526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003527 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (doesrange || eap->skip)
3529 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003530
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003532 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003534 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (aborting())
3536 break;
3537 }
3538 if (eap->skip)
3539 --emsg_skip;
3540
3541 if (!failed)
3542 {
3543 /* Check for trailing illegal characters and a following command. */
3544 if (!ends_excmd(*arg))
3545 {
3546 emsg_severe = TRUE;
3547 EMSG(_(e_trailing));
3548 }
3549 else
3550 eap->nextcmd = check_nextcmd(arg);
3551 }
3552
3553end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003554 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003555 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556}
3557
3558/*
3559 * ":unlet[!] var1 ... " command.
3560 */
3561 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003562ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003564 ex_unletlock(eap, eap->arg, 0);
3565}
3566
3567/*
3568 * ":lockvar" and ":unlockvar" commands
3569 */
3570 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003571ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003574 int deep = 2;
3575
3576 if (eap->forceit)
3577 deep = -1;
3578 else if (vim_isdigit(*arg))
3579 {
3580 deep = getdigits(&arg);
3581 arg = skipwhite(arg);
3582 }
3583
3584 ex_unletlock(eap, arg, deep);
3585}
3586
3587/*
3588 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3589 */
3590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003591ex_unletlock(
3592 exarg_T *eap,
3593 char_u *argstart,
3594 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003595{
3596 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
3601 do
3602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003604 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003605 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 if (lv.ll_name == NULL)
3607 error = TRUE; /* error but continue parsing */
3608 if (name_end == NULL || (!vim_iswhite(*name_end)
3609 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 if (name_end != NULL)
3612 {
3613 emsg_severe = TRUE;
3614 EMSG(_(e_trailing));
3615 }
3616 if (!(eap->skip || error))
3617 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 break;
3619 }
3620
3621 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622 {
3623 if (eap->cmdidx == CMD_unlet)
3624 {
3625 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3626 error = TRUE;
3627 }
3628 else
3629 {
3630 if (do_lock_var(&lv, name_end, deep,
3631 eap->cmdidx == CMD_lockvar) == FAIL)
3632 error = TRUE;
3633 }
3634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 if (!eap->skip)
3637 clear_lval(&lv);
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 arg = skipwhite(name_end);
3640 } while (!ends_excmd(*arg));
3641
3642 eap->nextcmd = check_nextcmd(arg);
3643}
3644
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003646do_unlet_var(
3647 lval_T *lp,
3648 char_u *name_end,
3649 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 int ret = OK;
3652 int cc;
3653
3654 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 cc = *name_end;
3657 *name_end = NUL;
3658
3659 /* Normal name or expanded name. */
3660 if (check_changedtick(lp->ll_name))
3661 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003666 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003667 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003668 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003669 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 else if (lp->ll_range)
3672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003674 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003675 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003676
3677 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3678 {
3679 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003680 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003681 return FAIL;
3682 ll_li = li;
3683 ++ll_n1;
3684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685
3686 /* Delete a range of List items. */
3687 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3688 {
3689 li = lp->ll_li->li_next;
3690 listitem_remove(lp->ll_list, lp->ll_li);
3691 lp->ll_li = li;
3692 ++lp->ll_n1;
3693 }
3694 }
3695 else
3696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 /* unlet a List item. */
3699 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003702 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 }
3704
3705 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003706}
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708/*
3709 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003710 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 */
3712 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003713do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 hashtab_T *ht;
3716 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003717 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003718 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003719 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 ht = find_var_ht(name, &varname);
3722 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003724 if (ht == &globvarht)
3725 d = &globvardict;
3726 else if (current_funccal != NULL
3727 && ht == &current_funccal->l_vars.dv_hashtab)
3728 d = &current_funccal->l_vars;
3729 else if (ht == &compat_hashtab)
3730 d = &vimvardict;
3731 else
3732 {
3733 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3734 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3735 }
3736 if (d == NULL)
3737 {
3738 EMSG2(_(e_intern2), "do_unlet()");
3739 return FAIL;
3740 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 hi = hash_find(ht, varname);
3742 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003743 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003744 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003745 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003746 || var_check_ro(di->di_flags, name, FALSE)
3747 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003748 return FAIL;
3749
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003750 delete_var(ht, hi);
3751 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003754 if (forceit)
3755 return OK;
3756 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 return FAIL;
3758}
3759
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760/*
3761 * Lock or unlock variable indicated by "lp".
3762 * "deep" is the levels to go (-1 for unlimited);
3763 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3764 */
3765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766do_lock_var(
3767 lval_T *lp,
3768 char_u *name_end,
3769 int deep,
3770 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771{
3772 int ret = OK;
3773 int cc;
3774 dictitem_T *di;
3775
3776 if (deep == 0) /* nothing to do */
3777 return OK;
3778
3779 if (lp->ll_tv == NULL)
3780 {
3781 cc = *name_end;
3782 *name_end = NUL;
3783
3784 /* Normal name or expanded name. */
3785 if (check_changedtick(lp->ll_name))
3786 ret = FAIL;
3787 else
3788 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003789 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003790 if (di == NULL)
3791 ret = FAIL;
3792 else
3793 {
3794 if (lock)
3795 di->di_flags |= DI_FLAGS_LOCK;
3796 else
3797 di->di_flags &= ~DI_FLAGS_LOCK;
3798 item_lock(&di->di_tv, deep, lock);
3799 }
3800 }
3801 *name_end = cc;
3802 }
3803 else if (lp->ll_range)
3804 {
3805 listitem_T *li = lp->ll_li;
3806
3807 /* (un)lock a range of List items. */
3808 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3809 {
3810 item_lock(&li->li_tv, deep, lock);
3811 li = li->li_next;
3812 ++lp->ll_n1;
3813 }
3814 }
3815 else if (lp->ll_list != NULL)
3816 /* (un)lock a List item. */
3817 item_lock(&lp->ll_li->li_tv, deep, lock);
3818 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003819 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003820 item_lock(&lp->ll_di->di_tv, deep, lock);
3821
3822 return ret;
3823}
3824
3825/*
3826 * Lock or unlock an item. "deep" is nr of levels to go.
3827 */
3828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003830{
3831 static int recurse = 0;
3832 list_T *l;
3833 listitem_T *li;
3834 dict_T *d;
3835 hashitem_T *hi;
3836 int todo;
3837
3838 if (recurse >= DICT_MAXNEST)
3839 {
3840 EMSG(_("E743: variable nested too deep for (un)lock"));
3841 return;
3842 }
3843 if (deep == 0)
3844 return;
3845 ++recurse;
3846
3847 /* lock/unlock the item itself */
3848 if (lock)
3849 tv->v_lock |= VAR_LOCKED;
3850 else
3851 tv->v_lock &= ~VAR_LOCKED;
3852
3853 switch (tv->v_type)
3854 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003855 case VAR_UNKNOWN:
3856 case VAR_NUMBER:
3857 case VAR_STRING:
3858 case VAR_FUNC:
3859 case VAR_FLOAT:
3860 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003861 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003862 break;
3863
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003864 case VAR_LIST:
3865 if ((l = tv->vval.v_list) != NULL)
3866 {
3867 if (lock)
3868 l->lv_lock |= VAR_LOCKED;
3869 else
3870 l->lv_lock &= ~VAR_LOCKED;
3871 if (deep < 0 || deep > 1)
3872 /* recursive: lock/unlock the items the List contains */
3873 for (li = l->lv_first; li != NULL; li = li->li_next)
3874 item_lock(&li->li_tv, deep - 1, lock);
3875 }
3876 break;
3877 case VAR_DICT:
3878 if ((d = tv->vval.v_dict) != NULL)
3879 {
3880 if (lock)
3881 d->dv_lock |= VAR_LOCKED;
3882 else
3883 d->dv_lock &= ~VAR_LOCKED;
3884 if (deep < 0 || deep > 1)
3885 {
3886 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003887 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003888 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3889 {
3890 if (!HASHITEM_EMPTY(hi))
3891 {
3892 --todo;
3893 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3894 }
3895 }
3896 }
3897 }
3898 }
3899 --recurse;
3900}
3901
Bram Moolenaara40058a2005-07-11 22:42:07 +00003902/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003903 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3904 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003905 */
3906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003907tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003908{
3909 return (tv->v_lock & VAR_LOCKED)
3910 || (tv->v_type == VAR_LIST
3911 && tv->vval.v_list != NULL
3912 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3913 || (tv->v_type == VAR_DICT
3914 && tv->vval.v_dict != NULL
3915 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3916}
3917
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3919/*
3920 * Delete all "menutrans_" variables.
3921 */
3922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003923del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaar33570922005-01-25 22:26:29 +00003928 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003929 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 {
3932 if (!HASHITEM_EMPTY(hi))
3933 {
3934 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003935 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3936 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 }
3938 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940}
3941#endif
3942
3943#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3944
3945/*
3946 * Local string buffer for the next two functions to store a variable name
3947 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3948 * get_user_var_name().
3949 */
3950
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003951static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953static char_u *varnamebuf = NULL;
3954static int varnamebuflen = 0;
3955
3956/*
3957 * Function to concatenate a prefix and a variable name.
3958 */
3959 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003960cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
3962 int len;
3963
3964 len = (int)STRLEN(name) + 3;
3965 if (len > varnamebuflen)
3966 {
3967 vim_free(varnamebuf);
3968 len += 10; /* some additional space */
3969 varnamebuf = alloc(len);
3970 if (varnamebuf == NULL)
3971 {
3972 varnamebuflen = 0;
3973 return NULL;
3974 }
3975 varnamebuflen = len;
3976 }
3977 *varnamebuf = prefix;
3978 varnamebuf[1] = ':';
3979 STRCPY(varnamebuf + 2, name);
3980 return varnamebuf;
3981}
3982
3983/*
3984 * Function given to ExpandGeneric() to obtain the list of user defined
3985 * (global/buffer/window/built-in) variable names.
3986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003988get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003990 static long_u gdone;
3991 static long_u bdone;
3992 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003993#ifdef FEAT_WINDOWS
3994 static long_u tdone;
3995#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003996 static int vidx;
3997 static hashitem_T *hi;
3998 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004001 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004002 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004003#ifdef FEAT_WINDOWS
4004 tdone = 0;
4005#endif
4006 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004007
4008 /* Global variables */
4009 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004012 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 else
4014 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 while (HASHITEM_EMPTY(hi))
4016 ++hi;
4017 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4018 return cat_prefix_varname('g', hi->hi_key);
4019 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004021
4022 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004023 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004026 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004027 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004028 else
4029 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004030 while (HASHITEM_EMPTY(hi))
4031 ++hi;
4032 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 return (char_u *)"b:changedtick";
4038 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004039
4040 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004041 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004042 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004044 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004046 else
4047 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004048 while (HASHITEM_EMPTY(hi))
4049 ++hi;
4050 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004053#ifdef FEAT_WINDOWS
4054 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004055 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004056 if (tdone < ht->ht_used)
4057 {
4058 if (tdone++ == 0)
4059 hi = ht->ht_array;
4060 else
4061 ++hi;
4062 while (HASHITEM_EMPTY(hi))
4063 ++hi;
4064 return cat_prefix_varname('t', hi->hi_key);
4065 }
4066#endif
4067
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 /* v: variables */
4069 if (vidx < VV_LEN)
4070 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 vim_free(varnamebuf);
4073 varnamebuf = NULL;
4074 varnamebuflen = 0;
4075 return NULL;
4076}
4077
4078#endif /* FEAT_CMDL_COMPL */
4079
4080/*
4081 * types for expressions.
4082 */
4083typedef enum
4084{
4085 TYPE_UNKNOWN = 0
4086 , TYPE_EQUAL /* == */
4087 , TYPE_NEQUAL /* != */
4088 , TYPE_GREATER /* > */
4089 , TYPE_GEQUAL /* >= */
4090 , TYPE_SMALLER /* < */
4091 , TYPE_SEQUAL /* <= */
4092 , TYPE_MATCH /* =~ */
4093 , TYPE_NOMATCH /* !~ */
4094} exptype_T;
4095
4096/*
4097 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4100 */
4101
4102/*
4103 * Handle zero level expression.
4104 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004106 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 * Return OK or FAIL.
4108 */
4109 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004110eval0(
4111 char_u *arg,
4112 typval_T *rettv,
4113 char_u **nextcmd,
4114 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 int ret;
4117 char_u *p;
4118
4119 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (ret == FAIL || !ends_excmd(*p))
4122 {
4123 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 /*
4126 * Report the invalid expression unless the expression evaluation has
4127 * been cancelled due to an aborting error, an interrupt, or an
4128 * exception.
4129 */
4130 if (!aborting())
4131 EMSG2(_(e_invexpr2), arg);
4132 ret = FAIL;
4133 }
4134 if (nextcmd != NULL)
4135 *nextcmd = check_nextcmd(p);
4136
4137 return ret;
4138}
4139
4140/*
4141 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004142 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 *
4144 * "arg" must point to the first non-white of the expression.
4145 * "arg" is advanced to the next non-white after the recognized expression.
4146 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004147 * Note: "rettv.v_lock" is not set.
4148 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 * Return OK or FAIL.
4150 */
4151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004152eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
4154 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 /*
4158 * Get the first variable.
4159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return FAIL;
4162
4163 if ((*arg)[0] == '?')
4164 {
4165 result = FALSE;
4166 if (evaluate)
4167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 int error = FALSE;
4169
4170 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 if (error)
4174 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176
4177 /*
4178 * Get the second variable.
4179 */
4180 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return FAIL;
4183
4184 /*
4185 * Check for the ":".
4186 */
4187 if ((*arg)[0] != ':')
4188 {
4189 EMSG(_("E109: Missing ':' after '?'"));
4190 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return FAIL;
4193 }
4194
4195 /*
4196 * Get the third variable.
4197 */
4198 *arg = skipwhite(*arg + 1);
4199 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4200 {
4201 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return FAIL;
4204 }
4205 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208
4209 return OK;
4210}
4211
4212/*
4213 * Handle first level expression:
4214 * expr2 || expr2 || expr2 logical OR
4215 *
4216 * "arg" must point to the first non-white of the expression.
4217 * "arg" is advanced to the next non-white after the recognized expression.
4218 *
4219 * Return OK or FAIL.
4220 */
4221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004222eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
Bram Moolenaar33570922005-01-25 22:26:29 +00004224 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 long result;
4226 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
4229 /*
4230 * Get the first variable.
4231 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return FAIL;
4234
4235 /*
4236 * Repeat until there is no following "||".
4237 */
4238 first = TRUE;
4239 result = FALSE;
4240 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4241 {
4242 if (evaluate && first)
4243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 if (error)
4248 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 first = FALSE;
4250 }
4251
4252 /*
4253 * Get the second variable.
4254 */
4255 *arg = skipwhite(*arg + 2);
4256 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4257 return FAIL;
4258
4259 /*
4260 * Compute the result.
4261 */
4262 if (evaluate && !result)
4263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (error)
4268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 if (evaluate)
4271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 rettv->v_type = VAR_NUMBER;
4273 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 }
4276
4277 return OK;
4278}
4279
4280/*
4281 * Handle second level expression:
4282 * expr3 && expr3 && expr3 logical AND
4283 *
4284 * "arg" must point to the first non-white of the expression.
4285 * "arg" is advanced to the next non-white after the recognized expression.
4286 *
4287 * Return OK or FAIL.
4288 */
4289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004290eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291{
Bram Moolenaar33570922005-01-25 22:26:29 +00004292 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 long result;
4294 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
4297 /*
4298 * Get the first variable.
4299 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 return FAIL;
4302
4303 /*
4304 * Repeat until there is no following "&&".
4305 */
4306 first = TRUE;
4307 result = TRUE;
4308 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4309 {
4310 if (evaluate && first)
4311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 if (error)
4316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 first = FALSE;
4318 }
4319
4320 /*
4321 * Get the second variable.
4322 */
4323 *arg = skipwhite(*arg + 2);
4324 if (eval4(arg, &var2, evaluate && result) == FAIL)
4325 return FAIL;
4326
4327 /*
4328 * Compute the result.
4329 */
4330 if (evaluate && result)
4331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004334 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (error)
4336 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338 if (evaluate)
4339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 rettv->v_type = VAR_NUMBER;
4341 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343 }
4344
4345 return OK;
4346}
4347
4348/*
4349 * Handle third level expression:
4350 * var1 == var2
4351 * var1 =~ var2
4352 * var1 != var2
4353 * var1 !~ var2
4354 * var1 > var2
4355 * var1 >= var2
4356 * var1 < var2
4357 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 * var1 is var2
4359 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 *
4361 * "arg" must point to the first non-white of the expression.
4362 * "arg" is advanced to the next non-white after the recognized expression.
4363 *
4364 * Return OK or FAIL.
4365 */
4366 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004367eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
Bram Moolenaar33570922005-01-25 22:26:29 +00004369 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 char_u *p;
4371 int i;
4372 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 int len = 2;
4375 long n1, n2;
4376 char_u *s1, *s2;
4377 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4378 regmatch_T regmatch;
4379 int ic;
4380 char_u *save_cpo;
4381
4382 /*
4383 * Get the first variable.
4384 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 return FAIL;
4387
4388 p = *arg;
4389 switch (p[0])
4390 {
4391 case '=': if (p[1] == '=')
4392 type = TYPE_EQUAL;
4393 else if (p[1] == '~')
4394 type = TYPE_MATCH;
4395 break;
4396 case '!': if (p[1] == '=')
4397 type = TYPE_NEQUAL;
4398 else if (p[1] == '~')
4399 type = TYPE_NOMATCH;
4400 break;
4401 case '>': if (p[1] != '=')
4402 {
4403 type = TYPE_GREATER;
4404 len = 1;
4405 }
4406 else
4407 type = TYPE_GEQUAL;
4408 break;
4409 case '<': if (p[1] != '=')
4410 {
4411 type = TYPE_SMALLER;
4412 len = 1;
4413 }
4414 else
4415 type = TYPE_SEQUAL;
4416 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004417 case 'i': if (p[1] == 's')
4418 {
4419 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4420 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004421 i = p[len];
4422 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 {
4424 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4425 type_is = TRUE;
4426 }
4427 }
4428 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
4430
4431 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004432 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 */
4434 if (type != TYPE_UNKNOWN)
4435 {
4436 /* extra question mark appended: ignore case */
4437 if (p[len] == '?')
4438 {
4439 ic = TRUE;
4440 ++len;
4441 }
4442 /* extra '#' appended: match case */
4443 else if (p[len] == '#')
4444 {
4445 ic = FALSE;
4446 ++len;
4447 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004448 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 else
4450 ic = p_ic;
4451
4452 /*
4453 * Get the second variable.
4454 */
4455 *arg = skipwhite(p + len);
4456 if (eval5(arg, &var2, evaluate) == FAIL)
4457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return FAIL;
4460 }
4461
4462 if (evaluate)
4463 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 if (type_is && rettv->v_type != var2.v_type)
4465 {
4466 /* For "is" a different type always means FALSE, for "notis"
4467 * it means TRUE. */
4468 n1 = (type == TYPE_NEQUAL);
4469 }
4470 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4471 {
4472 if (type_is)
4473 {
4474 n1 = (rettv->v_type == var2.v_type
4475 && rettv->vval.v_list == var2.vval.v_list);
4476 if (type == TYPE_NEQUAL)
4477 n1 = !n1;
4478 }
4479 else if (rettv->v_type != var2.v_type
4480 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4481 {
4482 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004483 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004484 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004485 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 clear_tv(rettv);
4487 clear_tv(&var2);
4488 return FAIL;
4489 }
4490 else
4491 {
4492 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004493 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4494 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 if (type == TYPE_NEQUAL)
4496 n1 = !n1;
4497 }
4498 }
4499
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004500 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4501 {
4502 if (type_is)
4503 {
4504 n1 = (rettv->v_type == var2.v_type
4505 && rettv->vval.v_dict == var2.vval.v_dict);
4506 if (type == TYPE_NEQUAL)
4507 n1 = !n1;
4508 }
4509 else if (rettv->v_type != var2.v_type
4510 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4511 {
4512 if (rettv->v_type != var2.v_type)
4513 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4514 else
4515 EMSG(_("E736: Invalid operation for Dictionary"));
4516 clear_tv(rettv);
4517 clear_tv(&var2);
4518 return FAIL;
4519 }
4520 else
4521 {
4522 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004523 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4524 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004525 if (type == TYPE_NEQUAL)
4526 n1 = !n1;
4527 }
4528 }
4529
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004530 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4531 {
4532 if (rettv->v_type != var2.v_type
4533 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4534 {
4535 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004536 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004537 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004538 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004539 clear_tv(rettv);
4540 clear_tv(&var2);
4541 return FAIL;
4542 }
4543 else
4544 {
4545 /* Compare two Funcrefs for being equal or unequal. */
4546 if (rettv->vval.v_string == NULL
4547 || var2.vval.v_string == NULL)
4548 n1 = FALSE;
4549 else
4550 n1 = STRCMP(rettv->vval.v_string,
4551 var2.vval.v_string) == 0;
4552 if (type == TYPE_NEQUAL)
4553 n1 = !n1;
4554 }
4555 }
4556
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004557#ifdef FEAT_FLOAT
4558 /*
4559 * If one of the two variables is a float, compare as a float.
4560 * When using "=~" or "!~", always compare as string.
4561 */
4562 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4563 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4564 {
4565 float_T f1, f2;
4566
4567 if (rettv->v_type == VAR_FLOAT)
4568 f1 = rettv->vval.v_float;
4569 else
4570 f1 = get_tv_number(rettv);
4571 if (var2.v_type == VAR_FLOAT)
4572 f2 = var2.vval.v_float;
4573 else
4574 f2 = get_tv_number(&var2);
4575 n1 = FALSE;
4576 switch (type)
4577 {
4578 case TYPE_EQUAL: n1 = (f1 == f2); break;
4579 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4580 case TYPE_GREATER: n1 = (f1 > f2); break;
4581 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4582 case TYPE_SMALLER: n1 = (f1 < f2); break;
4583 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4584 case TYPE_UNKNOWN:
4585 case TYPE_MATCH:
4586 case TYPE_NOMATCH: break; /* avoid gcc warning */
4587 }
4588 }
4589#endif
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 /*
4592 * If one of the two variables is a number, compare as a number.
4593 * When using "=~" or "!~", always compare as string.
4594 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004595 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 n1 = get_tv_number(rettv);
4599 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 switch (type)
4601 {
4602 case TYPE_EQUAL: n1 = (n1 == n2); break;
4603 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4604 case TYPE_GREATER: n1 = (n1 > n2); break;
4605 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4606 case TYPE_SMALLER: n1 = (n1 < n2); break;
4607 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4608 case TYPE_UNKNOWN:
4609 case TYPE_MATCH:
4610 case TYPE_NOMATCH: break; /* avoid gcc warning */
4611 }
4612 }
4613 else
4614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 s1 = get_tv_string_buf(rettv, buf1);
4616 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4618 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4619 else
4620 i = 0;
4621 n1 = FALSE;
4622 switch (type)
4623 {
4624 case TYPE_EQUAL: n1 = (i == 0); break;
4625 case TYPE_NEQUAL: n1 = (i != 0); break;
4626 case TYPE_GREATER: n1 = (i > 0); break;
4627 case TYPE_GEQUAL: n1 = (i >= 0); break;
4628 case TYPE_SMALLER: n1 = (i < 0); break;
4629 case TYPE_SEQUAL: n1 = (i <= 0); break;
4630
4631 case TYPE_MATCH:
4632 case TYPE_NOMATCH:
4633 /* avoid 'l' flag in 'cpoptions' */
4634 save_cpo = p_cpo;
4635 p_cpo = (char_u *)"";
4636 regmatch.regprog = vim_regcomp(s2,
4637 RE_MAGIC + RE_STRING);
4638 regmatch.rm_ic = ic;
4639 if (regmatch.regprog != NULL)
4640 {
4641 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004642 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (type == TYPE_NOMATCH)
4644 n1 = !n1;
4645 }
4646 p_cpo = save_cpo;
4647 break;
4648
4649 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4650 }
4651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 clear_tv(rettv);
4653 clear_tv(&var2);
4654 rettv->v_type = VAR_NUMBER;
4655 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
4657 }
4658
4659 return OK;
4660}
4661
4662/*
4663 * Handle fourth level expression:
4664 * + number addition
4665 * - number subtraction
4666 * . string concatenation
4667 *
4668 * "arg" must point to the first non-white of the expression.
4669 * "arg" is advanced to the next non-white after the recognized expression.
4670 *
4671 * Return OK or FAIL.
4672 */
4673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004674eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
Bram Moolenaar33570922005-01-25 22:26:29 +00004676 typval_T var2;
4677 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 int op;
4679 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004680#ifdef FEAT_FLOAT
4681 float_T f1 = 0, f2 = 0;
4682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 char_u *s1, *s2;
4684 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4685 char_u *p;
4686
4687 /*
4688 * Get the first variable.
4689 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004690 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 return FAIL;
4692
4693 /*
4694 * Repeat computing, until no '+', '-' or '.' is following.
4695 */
4696 for (;;)
4697 {
4698 op = **arg;
4699 if (op != '+' && op != '-' && op != '.')
4700 break;
4701
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702 if ((op != '+' || rettv->v_type != VAR_LIST)
4703#ifdef FEAT_FLOAT
4704 && (op == '.' || rettv->v_type != VAR_FLOAT)
4705#endif
4706 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 {
4708 /* For "list + ...", an illegal use of the first operand as
4709 * a number cannot be determined before evaluating the 2nd
4710 * operand: if this is also a list, all is ok.
4711 * For "something . ...", "something - ..." or "non-list + ...",
4712 * we know that the first operand needs to be a string or number
4713 * without evaluating the 2nd operand. So check before to avoid
4714 * side effects after an error. */
4715 if (evaluate && get_tv_string_chk(rettv) == NULL)
4716 {
4717 clear_tv(rettv);
4718 return FAIL;
4719 }
4720 }
4721
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 /*
4723 * Get the second variable.
4724 */
4725 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004726 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004728 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return FAIL;
4730 }
4731
4732 if (evaluate)
4733 {
4734 /*
4735 * Compute the result.
4736 */
4737 if (op == '.')
4738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4740 s2 = get_tv_string_buf_chk(&var2, buf2);
4741 if (s2 == NULL) /* type error ? */
4742 {
4743 clear_tv(rettv);
4744 clear_tv(&var2);
4745 return FAIL;
4746 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004747 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 clear_tv(rettv);
4749 rettv->v_type = VAR_STRING;
4750 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004752 else if (op == '+' && rettv->v_type == VAR_LIST
4753 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004754 {
4755 /* concatenate Lists */
4756 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4757 &var3) == FAIL)
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
4763 clear_tv(rettv);
4764 *rettv = var3;
4765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 else
4767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004768 int error = FALSE;
4769
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#ifdef FEAT_FLOAT
4771 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004772 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004773 f1 = rettv->vval.v_float;
4774 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004776 else
4777#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004779 n1 = get_tv_number_chk(rettv, &error);
4780 if (error)
4781 {
4782 /* This can only happen for "list + non-list". For
4783 * "non-list + ..." or "something - ...", we returned
4784 * before evaluating the 2nd operand. */
4785 clear_tv(rettv);
4786 return FAIL;
4787 }
4788#ifdef FEAT_FLOAT
4789 if (var2.v_type == VAR_FLOAT)
4790 f1 = n1;
4791#endif
4792 }
4793#ifdef FEAT_FLOAT
4794 if (var2.v_type == VAR_FLOAT)
4795 {
4796 f2 = var2.vval.v_float;
4797 n2 = 0;
4798 }
4799 else
4800#endif
4801 {
4802 n2 = get_tv_number_chk(&var2, &error);
4803 if (error)
4804 {
4805 clear_tv(rettv);
4806 clear_tv(&var2);
4807 return FAIL;
4808 }
4809#ifdef FEAT_FLOAT
4810 if (rettv->v_type == VAR_FLOAT)
4811 f2 = n2;
4812#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815
4816#ifdef FEAT_FLOAT
4817 /* If there is a float on either side the result is a float. */
4818 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4819 {
4820 if (op == '+')
4821 f1 = f1 + f2;
4822 else
4823 f1 = f1 - f2;
4824 rettv->v_type = VAR_FLOAT;
4825 rettv->vval.v_float = f1;
4826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828#endif
4829 {
4830 if (op == '+')
4831 n1 = n1 + n2;
4832 else
4833 n1 = n1 - n2;
4834 rettv->v_type = VAR_NUMBER;
4835 rettv->vval.v_number = n1;
4836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840 }
4841 return OK;
4842}
4843
4844/*
4845 * Handle fifth level expression:
4846 * * number multiplication
4847 * / number division
4848 * % number modulo
4849 *
4850 * "arg" must point to the first non-white of the expression.
4851 * "arg" is advanced to the next non-white after the recognized expression.
4852 *
4853 * Return OK or FAIL.
4854 */
4855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004856eval6(
4857 char_u **arg,
4858 typval_T *rettv,
4859 int evaluate,
4860 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
Bram Moolenaar33570922005-01-25 22:26:29 +00004862 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 int op;
4864 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865#ifdef FEAT_FLOAT
4866 int use_float = FALSE;
4867 float_T f1 = 0, f2;
4868#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004869 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 /*
4872 * Get the first variable.
4873 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004874 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 return FAIL;
4876
4877 /*
4878 * Repeat computing, until no '*', '/' or '%' is following.
4879 */
4880 for (;;)
4881 {
4882 op = **arg;
4883 if (op != '*' && op != '/' && op != '%')
4884 break;
4885
4886 if (evaluate)
4887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888#ifdef FEAT_FLOAT
4889 if (rettv->v_type == VAR_FLOAT)
4890 {
4891 f1 = rettv->vval.v_float;
4892 use_float = TRUE;
4893 n1 = 0;
4894 }
4895 else
4896#endif
4897 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004899 if (error)
4900 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 else
4903 n1 = 0;
4904
4905 /*
4906 * Get the second variable.
4907 */
4908 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004909 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 return FAIL;
4911
4912 if (evaluate)
4913 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914#ifdef FEAT_FLOAT
4915 if (var2.v_type == VAR_FLOAT)
4916 {
4917 if (!use_float)
4918 {
4919 f1 = n1;
4920 use_float = TRUE;
4921 }
4922 f2 = var2.vval.v_float;
4923 n2 = 0;
4924 }
4925 else
4926#endif
4927 {
4928 n2 = get_tv_number_chk(&var2, &error);
4929 clear_tv(&var2);
4930 if (error)
4931 return FAIL;
4932#ifdef FEAT_FLOAT
4933 if (use_float)
4934 f2 = n2;
4935#endif
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 /*
4939 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004942#ifdef FEAT_FLOAT
4943 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945 if (op == '*')
4946 f1 = f1 * f2;
4947 else if (op == '/')
4948 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004949# ifdef VMS
4950 /* VMS crashes on divide by zero, work around it */
4951 if (f2 == 0.0)
4952 {
4953 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004954 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004955 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004956 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004957 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004958 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004959 }
4960 else
4961 f1 = f1 / f2;
4962# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004963 /* We rely on the floating point library to handle divide
4964 * by zero to result in "inf" and not a crash. */
4965 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004970 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 return FAIL;
4972 }
4973 rettv->v_type = VAR_FLOAT;
4974 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 if (op == '*')
4980 n1 = n1 * n2;
4981 else if (op == '/')
4982 {
4983 if (n2 == 0) /* give an error message? */
4984 {
4985 if (n1 == 0)
4986 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4987 else if (n1 < 0)
4988 n1 = -0x7fffffffL;
4989 else
4990 n1 = 0x7fffffffL;
4991 }
4992 else
4993 n1 = n1 / n2;
4994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 {
4997 if (n2 == 0) /* give an error message? */
4998 n1 = 0;
4999 else
5000 n1 = n1 % n2;
5001 }
5002 rettv->v_type = VAR_NUMBER;
5003 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 }
5007
5008 return OK;
5009}
5010
5011/*
5012 * Handle sixth level expression:
5013 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005014 * "string" string constant
5015 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 * &option-name option value
5017 * @r register contents
5018 * identifier variable value
5019 * function() function call
5020 * $VAR environment variable
5021 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005022 * [expr, expr] List
5023 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 *
5025 * Also handle:
5026 * ! in front logical NOT
5027 * - in front unary minus
5028 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 * trailing [] subscript in String or List
5030 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 *
5032 * "arg" must point to the first non-white of the expression.
5033 * "arg" is advanced to the next non-white after the recognized expression.
5034 *
5035 * Return OK or FAIL.
5036 */
5037 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005038eval7(
5039 char_u **arg,
5040 typval_T *rettv,
5041 int evaluate,
5042 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 long n;
5045 int len;
5046 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 char_u *start_leader, *end_leader;
5048 int ret = OK;
5049 char_u *alias;
5050
5051 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005053 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
5057 /*
5058 * Skip '!' and '-' characters. They are handled later.
5059 */
5060 start_leader = *arg;
5061 while (**arg == '!' || **arg == '-' || **arg == '+')
5062 *arg = skipwhite(*arg + 1);
5063 end_leader = *arg;
5064
5065 switch (**arg)
5066 {
5067 /*
5068 * Number constant.
5069 */
5070 case '0':
5071 case '1':
5072 case '2':
5073 case '3':
5074 case '4':
5075 case '5':
5076 case '6':
5077 case '7':
5078 case '8':
5079 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005080 {
5081#ifdef FEAT_FLOAT
5082 char_u *p = skipdigits(*arg + 1);
5083 int get_float = FALSE;
5084
5085 /* We accept a float when the format matches
5086 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005087 * strict to avoid backwards compatibility problems.
5088 * Don't look for a float after the "." operator, so that
5089 * ":let vers = 1.2.3" doesn't fail. */
5090 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005092 get_float = TRUE;
5093 p = skipdigits(p + 2);
5094 if (*p == 'e' || *p == 'E')
5095 {
5096 ++p;
5097 if (*p == '-' || *p == '+')
5098 ++p;
5099 if (!vim_isdigit(*p))
5100 get_float = FALSE;
5101 else
5102 p = skipdigits(p + 1);
5103 }
5104 if (ASCII_ISALPHA(*p) || *p == '.')
5105 get_float = FALSE;
5106 }
5107 if (get_float)
5108 {
5109 float_T f;
5110
5111 *arg += string2float(*arg, &f);
5112 if (evaluate)
5113 {
5114 rettv->v_type = VAR_FLOAT;
5115 rettv->vval.v_float = f;
5116 }
5117 }
5118 else
5119#endif
5120 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005121 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005122 *arg += len;
5123 if (evaluate)
5124 {
5125 rettv->v_type = VAR_NUMBER;
5126 rettv->vval.v_number = n;
5127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
5132 /*
5133 * String constant: "string".
5134 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005135 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 break;
5137
5138 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 break;
5143
5144 /*
5145 * List: [expr, expr]
5146 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005147 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 break;
5149
5150 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 * Dictionary: {key: val, key: val}
5152 */
5153 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5154 break;
5155
5156 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005157 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 break;
5161
5162 /*
5163 * Environment variable: $VAR.
5164 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 break;
5167
5168 /*
5169 * Register contents: @r.
5170 */
5171 case '@': ++*arg;
5172 if (evaluate)
5173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005175 rettv->vval.v_string = get_reg_contents(**arg,
5176 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178 if (**arg != NUL)
5179 ++*arg;
5180 break;
5181
5182 /*
5183 * nested expression: (expression).
5184 */
5185 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (**arg == ')')
5188 ++*arg;
5189 else if (ret == OK)
5190 {
5191 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005192 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 ret = FAIL;
5194 }
5195 break;
5196
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200
5201 if (ret == NOTDONE)
5202 {
5203 /*
5204 * Must be a variable or function name.
5205 * Can also be a curly-braces kind of name: {expr}.
5206 */
5207 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 if (alias != NULL)
5210 s = alias;
5211
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005212 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 ret = FAIL;
5214 else
5215 {
5216 if (**arg == '(') /* recursive! */
5217 {
5218 /* If "s" is the name of a variable of type VAR_FUNC
5219 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005220 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221
5222 /* Invoke the function. */
5223 ret = get_func_tv(s, len, rettv, arg,
5224 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005225 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005226
5227 /* If evaluate is FALSE rettv->v_type was not set in
5228 * get_func_tv, but it's needed in handle_subscript() to parse
5229 * what follows. So set it here. */
5230 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5231 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005232 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005233 rettv->v_type = VAR_FUNC;
5234 }
5235
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 /* Stop the expression evaluation when immediately
5237 * aborting on error, or when an interrupt occurred or
5238 * an exception was thrown but not caught. */
5239 if (aborting())
5240 {
5241 if (ret == OK)
5242 clear_tv(rettv);
5243 ret = FAIL;
5244 }
5245 }
5246 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005247 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005248 else
5249 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005251 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 }
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 *arg = skipwhite(*arg);
5255
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005256 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5257 * expr(expr). */
5258 if (ret == OK)
5259 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
5261 /*
5262 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5263 */
5264 if (ret == OK && evaluate && end_leader > start_leader)
5265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005267 int val = 0;
5268#ifdef FEAT_FLOAT
5269 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005271 if (rettv->v_type == VAR_FLOAT)
5272 f = rettv->vval.v_float;
5273 else
5274#endif
5275 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005276 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005278 clear_tv(rettv);
5279 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 else
5282 {
5283 while (end_leader > start_leader)
5284 {
5285 --end_leader;
5286 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 {
5288#ifdef FEAT_FLOAT
5289 if (rettv->v_type == VAR_FLOAT)
5290 f = !f;
5291 else
5292#endif
5293 val = !val;
5294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005296 {
5297#ifdef FEAT_FLOAT
5298 if (rettv->v_type == VAR_FLOAT)
5299 f = -f;
5300 else
5301#endif
5302 val = -val;
5303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005305#ifdef FEAT_FLOAT
5306 if (rettv->v_type == VAR_FLOAT)
5307 {
5308 clear_tv(rettv);
5309 rettv->vval.v_float = f;
5310 }
5311 else
5312#endif
5313 {
5314 clear_tv(rettv);
5315 rettv->v_type = VAR_NUMBER;
5316 rettv->vval.v_number = val;
5317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 }
5320
5321 return ret;
5322}
5323
5324/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005325 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5326 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5328 */
5329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005330eval_index(
5331 char_u **arg,
5332 typval_T *rettv,
5333 int evaluate,
5334 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335{
5336 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005337 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 long len = -1;
5340 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343
Bram Moolenaara03f2332016-02-06 18:09:59 +01005344 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005346 case VAR_FUNC:
5347 if (verbose)
5348 EMSG(_("E695: Cannot index a Funcref"));
5349 return FAIL;
5350 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005351#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005352 if (verbose)
5353 EMSG(_(e_float_as_string));
5354 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005355#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005356 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005357 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005358 if (verbose)
5359 EMSG(_("E909: Cannot index a special variable"));
5360 return FAIL;
5361 case VAR_UNKNOWN:
5362 if (evaluate)
5363 return FAIL;
5364 /* FALLTHROUGH */
5365
5366 case VAR_STRING:
5367 case VAR_NUMBER:
5368 case VAR_LIST:
5369 case VAR_DICT:
5370 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005371 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005373 init_tv(&var1);
5374 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005375 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377 /*
5378 * dict.name
5379 */
5380 key = *arg + 1;
5381 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5382 ;
5383 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 }
5387 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 /*
5390 * something[idx]
5391 *
5392 * Get the (first) variable from inside the [].
5393 */
5394 *arg = skipwhite(*arg + 1);
5395 if (**arg == ':')
5396 empty1 = TRUE;
5397 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5398 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005399 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5400 {
5401 /* not a number or string */
5402 clear_tv(&var1);
5403 return FAIL;
5404 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405
5406 /*
5407 * Get the second variable from inside the [:].
5408 */
5409 if (**arg == ':')
5410 {
5411 range = TRUE;
5412 *arg = skipwhite(*arg + 1);
5413 if (**arg == ']')
5414 empty2 = TRUE;
5415 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 if (!empty1)
5418 clear_tv(&var1);
5419 return FAIL;
5420 }
5421 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5422 {
5423 /* not a number or string */
5424 if (!empty1)
5425 clear_tv(&var1);
5426 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 return FAIL;
5428 }
5429 }
5430
5431 /* Check for the ']'. */
5432 if (**arg != ']')
5433 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005434 if (verbose)
5435 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436 clear_tv(&var1);
5437 if (range)
5438 clear_tv(&var2);
5439 return FAIL;
5440 }
5441 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 }
5443
5444 if (evaluate)
5445 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 n1 = 0;
5447 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 n1 = get_tv_number(&var1);
5450 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 }
5452 if (range)
5453 {
5454 if (empty2)
5455 n2 = -1;
5456 else
5457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 n2 = get_tv_number(&var2);
5459 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461 }
5462
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005465 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005466 case VAR_FUNC:
5467 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005468 case VAR_SPECIAL:
5469 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005470 break; /* not evaluating, skipping over subscript */
5471
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 case VAR_NUMBER:
5473 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 len = (long)STRLEN(s);
5476 if (range)
5477 {
5478 /* The resulting variable is a substring. If the indexes
5479 * are out of range the result is empty. */
5480 if (n1 < 0)
5481 {
5482 n1 = len + n1;
5483 if (n1 < 0)
5484 n1 = 0;
5485 }
5486 if (n2 < 0)
5487 n2 = len + n2;
5488 else if (n2 >= len)
5489 n2 = len;
5490 if (n1 >= len || n2 < 0 || n1 > n2)
5491 s = NULL;
5492 else
5493 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5494 }
5495 else
5496 {
5497 /* The resulting variable is a string of a single
5498 * character. If the index is too big or negative the
5499 * result is empty. */
5500 if (n1 >= len || n1 < 0)
5501 s = NULL;
5502 else
5503 s = vim_strnsave(s + n1, 1);
5504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 clear_tv(rettv);
5506 rettv->v_type = VAR_STRING;
5507 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 break;
5509
5510 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 if (n1 < 0)
5513 n1 = len + n1;
5514 if (!empty1 && (n1 < 0 || n1 >= len))
5515 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005516 /* For a range we allow invalid values and return an empty
5517 * list. A list index out of range is an error. */
5518 if (!range)
5519 {
5520 if (verbose)
5521 EMSGN(_(e_listidx), n1);
5522 return FAIL;
5523 }
5524 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 }
5526 if (range)
5527 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 list_T *l;
5529 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530
5531 if (n2 < 0)
5532 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005533 else if (n2 >= len)
5534 n2 = len - 1;
5535 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005536 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 l = list_alloc();
5538 if (l == NULL)
5539 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 n1 <= n2; ++n1)
5542 {
5543 if (list_append_tv(l, &item->li_tv) == FAIL)
5544 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005545 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 return FAIL;
5547 }
5548 item = item->li_next;
5549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 clear_tv(rettv);
5551 rettv->v_type = VAR_LIST;
5552 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005553 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 }
5555 else
5556 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005557 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 clear_tv(rettv);
5559 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 }
5561 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562
5563 case VAR_DICT:
5564 if (range)
5565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005566 if (verbose)
5567 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 if (len == -1)
5569 clear_tv(&var1);
5570 return FAIL;
5571 }
5572 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574
5575 if (len == -1)
5576 {
5577 key = get_tv_string(&var1);
5578 if (*key == NUL)
5579 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005580 if (verbose)
5581 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582 clear_tv(&var1);
5583 return FAIL;
5584 }
5585 }
5586
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005587 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005589 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005590 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 if (len == -1)
5592 clear_tv(&var1);
5593 if (item == NULL)
5594 return FAIL;
5595
5596 copy_tv(&item->di_tv, &var1);
5597 clear_tv(rettv);
5598 *rettv = var1;
5599 }
5600 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 }
5602 }
5603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 return OK;
5605}
5606
5607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * Get an option value.
5609 * "arg" points to the '&' or '+' before the option name.
5610 * "arg" is advanced to character after the option name.
5611 * Return OK or FAIL.
5612 */
5613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005614get_option_tv(
5615 char_u **arg,
5616 typval_T *rettv, /* when NULL, only check if option exists */
5617 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 char_u *option_end;
5620 long numval;
5621 char_u *stringval;
5622 int opt_type;
5623 int c;
5624 int working = (**arg == '+'); /* has("+option") */
5625 int ret = OK;
5626 int opt_flags;
5627
5628 /*
5629 * Isolate the option name and find its value.
5630 */
5631 option_end = find_option_end(arg, &opt_flags);
5632 if (option_end == NULL)
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 EMSG2(_("E112: Option name missing: %s"), *arg);
5636 return FAIL;
5637 }
5638
5639 if (!evaluate)
5640 {
5641 *arg = option_end;
5642 return OK;
5643 }
5644
5645 c = *option_end;
5646 *option_end = NUL;
5647 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650 if (opt_type == -3) /* invalid name */
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 EMSG2(_("E113: Unknown option: %s"), *arg);
5654 ret = FAIL;
5655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
5658 if (opt_type == -2) /* hidden string option */
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->v_type = VAR_STRING;
5661 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else if (opt_type == -1) /* hidden number option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_NUMBER;
5666 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 else if (opt_type == 1) /* number option */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv->v_type = VAR_NUMBER;
5671 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 else /* string option */
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 rettv->v_type = VAR_STRING;
5676 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 }
5679 else if (working && (opt_type == -2 || opt_type == -1))
5680 ret = FAIL;
5681
5682 *option_end = c; /* put back for error messages */
5683 *arg = option_end;
5684
5685 return ret;
5686}
5687
5688/*
5689 * Allocate a variable for a string constant.
5690 * Return OK or FAIL.
5691 */
5692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005693get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 char_u *p;
5696 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 int extra = 0;
5698
5699 /*
5700 * Find the end of the string, skipping backslashed characters.
5701 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005702 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 {
5704 if (*p == '\\' && p[1] != NUL)
5705 {
5706 ++p;
5707 /* A "\<x>" form occupies at least 4 characters, and produces up
5708 * to 6 characters: reserve space for 2 extra */
5709 if (*p == '<')
5710 extra += 2;
5711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713
5714 if (*p != '"')
5715 {
5716 EMSG2(_("E114: Missing quote: %s"), *arg);
5717 return FAIL;
5718 }
5719
5720 /* If only parsing, set *arg and return here */
5721 if (!evaluate)
5722 {
5723 *arg = p + 1;
5724 return OK;
5725 }
5726
5727 /*
5728 * Copy the string into allocated memory, handling backslashed
5729 * characters.
5730 */
5731 name = alloc((unsigned)(p - *arg + extra));
5732 if (name == NULL)
5733 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 rettv->v_type = VAR_STRING;
5735 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
5739 if (*p == '\\')
5740 {
5741 switch (*++p)
5742 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 case 'b': *name++ = BS; ++p; break;
5744 case 'e': *name++ = ESC; ++p; break;
5745 case 'f': *name++ = FF; ++p; break;
5746 case 'n': *name++ = NL; ++p; break;
5747 case 'r': *name++ = CAR; ++p; break;
5748 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
5750 case 'X': /* hex: "\x1", "\x12" */
5751 case 'x':
5752 case 'u': /* Unicode: "\u0023" */
5753 case 'U':
5754 if (vim_isxdigit(p[1]))
5755 {
5756 int n, nr;
5757 int c = toupper(*p);
5758
5759 if (c == 'X')
5760 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005761 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005763 else
5764 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 nr = 0;
5766 while (--n >= 0 && vim_isxdigit(p[1]))
5767 {
5768 ++p;
5769 nr = (nr << 4) + hex2nr(*p);
5770 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#ifdef FEAT_MBYTE
5773 /* For "\u" store the number according to
5774 * 'encoding'. */
5775 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 else
5778#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 break;
5782
5783 /* octal: "\1", "\12", "\123" */
5784 case '0':
5785 case '1':
5786 case '2':
5787 case '3':
5788 case '4':
5789 case '5':
5790 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 case '7': *name = *p++ - '0';
5792 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 *name = (*name << 3) + *p++ - '0';
5795 if (*p >= '0' && *p <= '7')
5796 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800
5801 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (extra != 0)
5804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807 }
5808 /* FALLTHROUGH */
5809
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 break;
5812 }
5813 }
5814 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 *arg = p + 1;
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 return OK;
5822}
5823
5824/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 * Return OK or FAIL.
5827 */
5828 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005829get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
5831 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 int reduce = 0;
5834
5835 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837 */
5838 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 break;
5844 ++reduce;
5845 ++p;
5846 }
5847 }
5848
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 return FAIL;
5853 }
5854
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 if (!evaluate)
5857 {
5858 *arg = p + 1;
5859 return OK;
5860 }
5861
5862 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 */
5865 str = alloc((unsigned)((p - *arg) - reduce));
5866 if (str == NULL)
5867 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 rettv->v_type = VAR_STRING;
5869 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 break;
5877 ++p;
5878 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 *arg = p + 1;
5883
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 return OK;
5885}
5886
5887/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 * Allocate a variable for a List and fill it from "*arg".
5889 * Return OK or FAIL.
5890 */
5891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005892get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893{
Bram Moolenaar33570922005-01-25 22:26:29 +00005894 list_T *l = NULL;
5895 typval_T tv;
5896 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897
5898 if (evaluate)
5899 {
5900 l = list_alloc();
5901 if (l == NULL)
5902 return FAIL;
5903 }
5904
5905 *arg = skipwhite(*arg + 1);
5906 while (**arg != ']' && **arg != NUL)
5907 {
5908 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5909 goto failret;
5910 if (evaluate)
5911 {
5912 item = listitem_alloc();
5913 if (item != NULL)
5914 {
5915 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005916 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 list_append(l, item);
5918 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005919 else
5920 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 }
5922
5923 if (**arg == ']')
5924 break;
5925 if (**arg != ',')
5926 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 goto failret;
5929 }
5930 *arg = skipwhite(*arg + 1);
5931 }
5932
5933 if (**arg != ']')
5934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936failret:
5937 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005938 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 return FAIL;
5940 }
5941
5942 *arg = skipwhite(*arg + 1);
5943 if (evaluate)
5944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005945 rettv->v_type = VAR_LIST;
5946 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 ++l->lv_refcount;
5948 }
5949
5950 return OK;
5951}
5952
5953/*
5954 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005955 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005957 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005958list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005960 list_T *l;
5961
5962 l = (list_T *)alloc_clear(sizeof(list_T));
5963 if (l != NULL)
5964 {
5965 /* Prepend the list to the list of lists for garbage collection. */
5966 if (first_list != NULL)
5967 first_list->lv_used_prev = l;
5968 l->lv_used_prev = NULL;
5969 l->lv_used_next = first_list;
5970 first_list = l;
5971 }
5972 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973}
5974
5975/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005976 * Allocate an empty list for a return value.
5977 * Returns OK or FAIL.
5978 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005979 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005980rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005981{
5982 list_T *l = list_alloc();
5983
5984 if (l == NULL)
5985 return FAIL;
5986
5987 rettv->vval.v_list = l;
5988 rettv->v_type = VAR_LIST;
5989 ++l->lv_refcount;
5990 return OK;
5991}
5992
5993/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 * Unreference a list: decrement the reference count and free it when it
5995 * becomes zero.
5996 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006000 if (l != NULL && --l->lv_refcount <= 0)
6001 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002}
6003
6004/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006005 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Ignores the reference count.
6007 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006009list_free(
6010 list_T *l,
6011 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012{
Bram Moolenaar33570922005-01-25 22:26:29 +00006013 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006015 /* Remove the list from the list of lists for garbage collection. */
6016 if (l->lv_used_prev == NULL)
6017 first_list = l->lv_used_next;
6018 else
6019 l->lv_used_prev->lv_used_next = l->lv_used_next;
6020 if (l->lv_used_next != NULL)
6021 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6022
Bram Moolenaard9fba312005-06-26 22:34:35 +00006023 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006025 /* Remove the item before deleting it. */
6026 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006027 if (recurse || (item->li_tv.v_type != VAR_LIST
6028 && item->li_tv.v_type != VAR_DICT))
6029 clear_tv(&item->li_tv);
6030 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031 }
6032 vim_free(l);
6033}
6034
6035/*
6036 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006037 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006039 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006040listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041{
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043}
6044
6045/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006046 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006049listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006051 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 vim_free(item);
6053}
6054
6055/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006056 * Remove a list item from a List and free it. Also clears the value.
6057 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006059listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006060{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006061 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006062 listitem_free(item);
6063}
6064
6065/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 * Get the number of items in a list.
6067 */
6068 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006069list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 if (l == NULL)
6072 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006073 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074}
6075
6076/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006077 * Return TRUE when two lists have exactly the same values.
6078 */
6079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080list_equal(
6081 list_T *l1,
6082 list_T *l2,
6083 int ic, /* ignore case for strings */
6084 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085{
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006088 if (l1 == NULL || l2 == NULL)
6089 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006090 if (l1 == l2)
6091 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006092 if (list_len(l1) != list_len(l2))
6093 return FALSE;
6094
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095 for (item1 = l1->lv_first, item2 = l2->lv_first;
6096 item1 != NULL && item2 != NULL;
6097 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006098 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 return FALSE;
6100 return item1 == NULL && item2 == NULL;
6101}
6102
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006103/*
6104 * Return the dictitem that an entry in a hashtable points to.
6105 */
6106 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006108{
6109 return HI2DI(hi);
6110}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006111
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006113 * Return TRUE when two dictionaries have exactly the same key/values.
6114 */
6115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116dict_equal(
6117 dict_T *d1,
6118 dict_T *d2,
6119 int ic, /* ignore case for strings */
6120 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006121{
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 hashitem_T *hi;
6123 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006124 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006125
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006126 if (d1 == NULL || d2 == NULL)
6127 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006128 if (d1 == d2)
6129 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130 if (dict_len(d1) != dict_len(d2))
6131 return FALSE;
6132
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006133 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006135 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006136 if (!HASHITEM_EMPTY(hi))
6137 {
6138 item2 = dict_find(d2, hi->hi_key, -1);
6139 if (item2 == NULL)
6140 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006141 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006142 return FALSE;
6143 --todo;
6144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145 }
6146 return TRUE;
6147}
6148
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006149static int tv_equal_recurse_limit;
6150
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006152 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006153 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006154 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006155 */
6156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157tv_equal(
6158 typval_T *tv1,
6159 typval_T *tv2,
6160 int ic, /* ignore case */
6161 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006162{
6163 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006164 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006165 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006166 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006168 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006169 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006170
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006171 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006172 * recursiveness to a limit. We guess they are equal then.
6173 * A fixed limit has the problem of still taking an awful long time.
6174 * Reduce the limit every time running into it. That should work fine for
6175 * deeply linked structures that are not recursively linked and catch
6176 * recursiveness quickly. */
6177 if (!recursive)
6178 tv_equal_recurse_limit = 1000;
6179 if (recursive_cnt >= tv_equal_recurse_limit)
6180 {
6181 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006182 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184
6185 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006187 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188 ++recursive_cnt;
6189 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6190 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006191 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006192
6193 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194 ++recursive_cnt;
6195 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6196 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006197 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198
6199 case VAR_FUNC:
6200 return (tv1->vval.v_string != NULL
6201 && tv2->vval.v_string != NULL
6202 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6203
6204 case VAR_NUMBER:
6205 return tv1->vval.v_number == tv2->vval.v_number;
6206
6207 case VAR_STRING:
6208 s1 = get_tv_string_buf(tv1, buf1);
6209 s2 = get_tv_string_buf(tv2, buf2);
6210 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006211
6212 case VAR_SPECIAL:
6213 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006214
6215 case VAR_FLOAT:
6216#ifdef FEAT_FLOAT
6217 return tv1->vval.v_float == tv2->vval.v_float;
6218#endif
6219 case VAR_JOB:
6220#ifdef FEAT_JOB
6221 return tv1->vval.v_job == tv2->vval.v_job;
6222#endif
6223 case VAR_UNKNOWN:
6224 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006226
Bram Moolenaara03f2332016-02-06 18:09:59 +01006227 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6228 * does not equal anything, not even itself. */
6229 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230}
6231
6232/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006233 * Locate item with index "n" in list "l" and return it.
6234 * A negative index is counted from the end; -1 is the last item.
6235 * Returns NULL when "n" is out of range.
6236 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006237 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006238list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239{
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006241 long idx;
6242
6243 if (l == NULL)
6244 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006245
6246 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006248 n = l->lv_len + n;
6249
6250 /* Check for index out of range. */
6251 if (n < 0 || n >= l->lv_len)
6252 return NULL;
6253
6254 /* When there is a cached index may start search from there. */
6255 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006257 if (n < l->lv_idx / 2)
6258 {
6259 /* closest to the start of the list */
6260 item = l->lv_first;
6261 idx = 0;
6262 }
6263 else if (n > (l->lv_idx + l->lv_len) / 2)
6264 {
6265 /* closest to the end of the list */
6266 item = l->lv_last;
6267 idx = l->lv_len - 1;
6268 }
6269 else
6270 {
6271 /* closest to the cached index */
6272 item = l->lv_idx_item;
6273 idx = l->lv_idx;
6274 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 }
6276 else
6277 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006278 if (n < l->lv_len / 2)
6279 {
6280 /* closest to the start of the list */
6281 item = l->lv_first;
6282 idx = 0;
6283 }
6284 else
6285 {
6286 /* closest to the end of the list */
6287 item = l->lv_last;
6288 idx = l->lv_len - 1;
6289 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006291
6292 while (n > idx)
6293 {
6294 /* search forward */
6295 item = item->li_next;
6296 ++idx;
6297 }
6298 while (n < idx)
6299 {
6300 /* search backward */
6301 item = item->li_prev;
6302 --idx;
6303 }
6304
6305 /* cache the used index */
6306 l->lv_idx = idx;
6307 l->lv_idx_item = item;
6308
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 return item;
6310}
6311
6312/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006313 * Get list item "l[idx]" as a number.
6314 */
6315 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006316list_find_nr(
6317 list_T *l,
6318 long idx,
6319 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006320{
6321 listitem_T *li;
6322
6323 li = list_find(l, idx);
6324 if (li == NULL)
6325 {
6326 if (errorp != NULL)
6327 *errorp = TRUE;
6328 return -1L;
6329 }
6330 return get_tv_number_chk(&li->li_tv, errorp);
6331}
6332
6333/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006334 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6335 */
6336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006337list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006338{
6339 listitem_T *li;
6340
6341 li = list_find(l, idx - 1);
6342 if (li == NULL)
6343 {
6344 EMSGN(_(e_listidx), idx);
6345 return NULL;
6346 }
6347 return get_tv_string(&li->li_tv);
6348}
6349
6350/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006351 * Locate "item" list "l" and return its index.
6352 * Returns -1 when "item" is not in the list.
6353 */
6354 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006355list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006356{
6357 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006359
6360 if (l == NULL)
6361 return -1;
6362 idx = 0;
6363 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6364 ++idx;
6365 if (li == NULL)
6366 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006367 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006368}
6369
6370/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 * Append item "item" to the end of list "l".
6372 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006374list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375{
6376 if (l->lv_last == NULL)
6377 {
6378 /* empty list */
6379 l->lv_first = item;
6380 l->lv_last = item;
6381 item->li_prev = NULL;
6382 }
6383 else
6384 {
6385 l->lv_last->li_next = item;
6386 item->li_prev = l->lv_last;
6387 l->lv_last = item;
6388 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006389 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 item->li_next = NULL;
6391}
6392
6393/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006398list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006400 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401
Bram Moolenaar05159a02005-02-26 23:04:13 +00006402 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006404 copy_tv(tv, &li->li_tv);
6405 list_append(l, li);
6406 return OK;
6407}
6408
6409/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006410 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006411 * Return FAIL when out of memory.
6412 */
6413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415{
6416 listitem_T *li = listitem_alloc();
6417
6418 if (li == NULL)
6419 return FAIL;
6420 li->li_tv.v_type = VAR_DICT;
6421 li->li_tv.v_lock = 0;
6422 li->li_tv.vval.v_dict = dict;
6423 list_append(list, li);
6424 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 return OK;
6426}
6427
6428/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006429 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006430 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006431 * Returns FAIL when out of memory.
6432 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006435{
6436 listitem_T *li = listitem_alloc();
6437
6438 if (li == NULL)
6439 return FAIL;
6440 list_append(l, li);
6441 li->li_tv.v_type = VAR_STRING;
6442 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006443 if (str == NULL)
6444 li->li_tv.vval.v_string = NULL;
6445 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006446 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006447 return FAIL;
6448 return OK;
6449}
6450
6451/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006452 * Append "n" to list "l".
6453 * Returns FAIL when out of memory.
6454 */
6455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006457{
6458 listitem_T *li;
6459
6460 li = listitem_alloc();
6461 if (li == NULL)
6462 return FAIL;
6463 li->li_tv.v_type = VAR_NUMBER;
6464 li->li_tv.v_lock = 0;
6465 li->li_tv.vval.v_number = n;
6466 list_append(l, li);
6467 return OK;
6468}
6469
6470/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472 * If "item" is NULL append at the end.
6473 * Return FAIL when out of memory.
6474 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477{
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479
6480 if (ni == NULL)
6481 return FAIL;
6482 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006483 list_insert(l, ni, item);
6484 return OK;
6485}
6486
6487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006488list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006489{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490 if (item == NULL)
6491 /* Append new item at end of list. */
6492 list_append(l, ni);
6493 else
6494 {
6495 /* Insert new item before existing item. */
6496 ni->li_prev = item->li_prev;
6497 ni->li_next = item;
6498 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006499 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501 ++l->lv_idx;
6502 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006504 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006506 l->lv_idx_item = NULL;
6507 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006509 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511}
6512
6513/*
6514 * Extend "l1" with "l2".
6515 * If "bef" is NULL append at the end, otherwise insert before this item.
6516 * Returns FAIL when out of memory.
6517 */
6518 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520{
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006522 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006524 /* We also quit the loop when we have inserted the original item count of
6525 * the list, avoid a hang when we extend a list with itself. */
6526 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6528 return FAIL;
6529 return OK;
6530}
6531
6532/*
6533 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6534 * Return FAIL when out of memory.
6535 */
6536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006537list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538{
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006541 if (l1 == NULL || l2 == NULL)
6542 return FAIL;
6543
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 if (l == NULL)
6547 return FAIL;
6548 tv->v_type = VAR_LIST;
6549 tv->vval.v_list = l;
6550
6551 /* append all items from the second list */
6552 return list_extend(l, l2, NULL);
6553}
6554
6555/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 * Returns NULL when out of memory.
6560 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006562list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563{
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 list_T *copy;
6565 listitem_T *item;
6566 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567
6568 if (orig == NULL)
6569 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570
6571 copy = list_alloc();
6572 if (copy != NULL)
6573 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006574 if (copyID != 0)
6575 {
6576 /* Do this before adding the items, because one of the items may
6577 * refer back to this list. */
6578 orig->lv_copyID = copyID;
6579 orig->lv_copylist = copy;
6580 }
6581 for (item = orig->lv_first; item != NULL && !got_int;
6582 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583 {
6584 ni = listitem_alloc();
6585 if (ni == NULL)
6586 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006587 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006588 {
6589 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6590 {
6591 vim_free(ni);
6592 break;
6593 }
6594 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006596 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 list_append(copy, ni);
6598 }
6599 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600 if (item != NULL)
6601 {
6602 list_unref(copy);
6603 copy = NULL;
6604 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 }
6606
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 return copy;
6608}
6609
6610/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006611 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006612 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006613 * This used to be called list_remove, but that conflicts with a Sun header
6614 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006617vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006618{
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006621 /* notify watchers */
6622 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006624 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006625 list_fix_watch(l, ip);
6626 if (ip == item2)
6627 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629
6630 if (item2->li_next == NULL)
6631 l->lv_last = item->li_prev;
6632 else
6633 item2->li_next->li_prev = item->li_prev;
6634 if (item->li_prev == NULL)
6635 l->lv_first = item2->li_next;
6636 else
6637 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006638 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639}
6640
6641/*
6642 * Return an allocated string with the string representation of a list.
6643 * May return NULL.
6644 */
6645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006646list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647{
6648 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649
6650 if (tv->vval.v_list == NULL)
6651 return NULL;
6652 ga_init2(&ga, (int)sizeof(char), 80);
6653 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006654 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006655 {
6656 vim_free(ga.ga_data);
6657 return NULL;
6658 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 ga_append(&ga, ']');
6660 ga_append(&ga, NUL);
6661 return (char_u *)ga.ga_data;
6662}
6663
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006664typedef struct join_S {
6665 char_u *s;
6666 char_u *tofree;
6667} join_T;
6668
6669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006670list_join_inner(
6671 garray_T *gap, /* to store the result in */
6672 list_T *l,
6673 char_u *sep,
6674 int echo_style,
6675 int copyID,
6676 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006677{
6678 int i;
6679 join_T *p;
6680 int len;
6681 int sumlen = 0;
6682 int first = TRUE;
6683 char_u *tofree;
6684 char_u numbuf[NUMBUFLEN];
6685 listitem_T *item;
6686 char_u *s;
6687
6688 /* Stringify each item in the list. */
6689 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6690 {
6691 if (echo_style)
6692 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6693 else
6694 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6695 if (s == NULL)
6696 return FAIL;
6697
6698 len = (int)STRLEN(s);
6699 sumlen += len;
6700
Bram Moolenaarcde88542015-08-11 19:14:00 +02006701 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006702 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6703 if (tofree != NULL || s != numbuf)
6704 {
6705 p->s = s;
6706 p->tofree = tofree;
6707 }
6708 else
6709 {
6710 p->s = vim_strnsave(s, len);
6711 p->tofree = p->s;
6712 }
6713
6714 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006715 if (did_echo_string_emsg) /* recursion error, bail out */
6716 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717 }
6718
6719 /* Allocate result buffer with its total size, avoid re-allocation and
6720 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6721 if (join_gap->ga_len >= 2)
6722 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6723 if (ga_grow(gap, sumlen + 2) == FAIL)
6724 return FAIL;
6725
6726 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6727 {
6728 if (first)
6729 first = FALSE;
6730 else
6731 ga_concat(gap, sep);
6732 p = ((join_T *)join_gap->ga_data) + i;
6733
6734 if (p->s != NULL)
6735 ga_concat(gap, p->s);
6736 line_breakcheck();
6737 }
6738
6739 return OK;
6740}
6741
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006742/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006744 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006745 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006746 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006747 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006748list_join(
6749 garray_T *gap,
6750 list_T *l,
6751 char_u *sep,
6752 int echo_style,
6753 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006754{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006755 garray_T join_ga;
6756 int retval;
6757 join_T *p;
6758 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759
Bram Moolenaard39a7512015-04-16 22:51:22 +02006760 if (l->lv_len < 1)
6761 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006762 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6763 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6764
6765 /* Dispose each item in join_ga. */
6766 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006767 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006768 p = (join_T *)join_ga.ga_data;
6769 for (i = 0; i < join_ga.ga_len; ++i)
6770 {
6771 vim_free(p->tofree);
6772 ++p;
6773 }
6774 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006775 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006776
6777 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778}
6779
6780/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006781 * Return the next (unique) copy ID.
6782 * Used for serializing nested structures.
6783 */
6784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006785get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006786{
6787 current_copyID += COPYID_INC;
6788 return current_copyID;
6789}
6790
6791/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792 * Garbage collection for lists and dictionaries.
6793 *
6794 * We use reference counts to be able to free most items right away when they
6795 * are no longer used. But for composite items it's possible that it becomes
6796 * unused while the reference count is > 0: When there is a recursive
6797 * reference. Example:
6798 * :let l = [1, 2, 3]
6799 * :let d = {9: l}
6800 * :let l[1] = d
6801 *
6802 * Since this is quite unusual we handle this with garbage collection: every
6803 * once in a while find out which lists and dicts are not referenced from any
6804 * variable.
6805 *
6806 * Here is a good reference text about garbage collection (refers to Python
6807 * but it applies to all reference-counting mechanisms):
6808 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006809 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006810
6811/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006812 * Do garbage collection for lists and dicts.
6813 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006814 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006816garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006817{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006818 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006819 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 buf_T *buf;
6821 win_T *wp;
6822 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006823 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006824 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006825 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826#ifdef FEAT_WINDOWS
6827 tabpage_T *tp;
6828#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006830 /* Only do this once. */
6831 want_garbage_collect = FALSE;
6832 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006833 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006834
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 /* We advance by two because we add one for items referenced through
6836 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006837 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006838
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 /*
6840 * 1. Go through all accessible variables and mark all lists and dicts
6841 * with copyID.
6842 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006843
6844 /* Don't free variables in the previous_funccal list unless they are only
6845 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006846 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6848 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006849 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6850 NULL);
6851 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6852 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 }
6854
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 /* script-local variables */
6856 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006857 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858
6859 /* buffer-local variables */
6860 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006861 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6862 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863
6864 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006865 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006866 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6867 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006868#ifdef FEAT_AUTOCMD
6869 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006870 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6871 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006872#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006874#ifdef FEAT_WINDOWS
6875 /* tabpage-local variables */
6876 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6878 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006879#endif
6880
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006882 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883
6884 /* function-local variables */
6885 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6886 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6888 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 }
6890
Bram Moolenaard812df62008-11-09 12:46:09 +00006891 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006893
Bram Moolenaar1dced572012-04-05 16:54:08 +02006894#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006896#endif
6897
Bram Moolenaardb913952012-06-29 12:54:53 +02006898#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006900#endif
6901
6902#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006904#endif
6905
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006906#ifdef FEAT_CHANNEL
6907 abort = abort || set_ref_in_channel(copyID);
6908#endif
6909
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006911 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 /*
6913 * 2. Free lists and dictionaries that are not referenced.
6914 */
6915 did_free = free_unref_items(copyID);
6916
6917 /*
6918 * 3. Check if any funccal can be freed now.
6919 */
6920 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006921 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 if (can_free_funccal(*pfc, copyID))
6923 {
6924 fc = *pfc;
6925 *pfc = fc->caller;
6926 free_funccal(fc, TRUE);
6927 did_free = TRUE;
6928 did_free_funccal = TRUE;
6929 }
6930 else
6931 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006932 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 if (did_free_funccal)
6934 /* When a funccal was freed some more items might be garbage
6935 * collected, so run again. */
6936 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006937 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 else if (p_verbose > 0)
6939 {
6940 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6941 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006942
6943 return did_free;
6944}
6945
6946/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006947 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 */
6949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006950free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006952 dict_T *dd, *dd_next;
6953 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 int did_free = FALSE;
6955
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 */
6959 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006960 {
6961 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006962 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006964 /* Free the Dictionary and ordinary items it contains, but don't
6965 * recurse into Lists and Dictionaries, they will be in the list
6966 * of dicts or list of lists. */
6967 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006970 dd = dd_next;
6971 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972
6973 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 * Go through the list of lists and free items without the copyID.
6975 * But don't free a list that has a watcher (used in a for loop), these
6976 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 */
6978 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006979 {
6980 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6982 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006984 /* Free the List and ordinary items it contains, but don't recurse
6985 * into Lists and Dictionaries, they will be in the list of dicts
6986 * or list of lists. */
6987 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006990 ll = ll_next;
6991 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006992
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 return did_free;
6994}
6995
6996/*
6997 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006998 * "list_stack" is used to add lists to be marked. Can be NULL.
6999 *
7000 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007003set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004{
7005 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007006 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007008 hashtab_T *cur_ht;
7009 ht_stack_T *ht_stack = NULL;
7010 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007012 cur_ht = ht;
7013 for (;;)
7014 {
7015 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 /* Mark each item in the hashtab. If the item contains a hashtab
7018 * it is added to ht_stack, if it contains a list it is added to
7019 * list_stack. */
7020 todo = (int)cur_ht->ht_used;
7021 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7022 if (!HASHITEM_EMPTY(hi))
7023 {
7024 --todo;
7025 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7026 &ht_stack, list_stack);
7027 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029
7030 if (ht_stack == NULL)
7031 break;
7032
7033 /* take an item from the stack */
7034 cur_ht = ht_stack->ht;
7035 tempitem = ht_stack;
7036 ht_stack = ht_stack->prev;
7037 free(tempitem);
7038 }
7039
7040 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041}
7042
7043/*
7044 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7046 *
7047 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007050set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 listitem_T *li;
7053 int abort = FALSE;
7054 list_T *cur_l;
7055 list_stack_T *list_stack = NULL;
7056 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 cur_l = l;
7059 for (;;)
7060 {
7061 if (!abort)
7062 /* Mark each item in the list. If the item contains a hashtab
7063 * it is added to ht_stack, if it contains a list it is added to
7064 * list_stack. */
7065 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7066 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7067 ht_stack, &list_stack);
7068 if (list_stack == NULL)
7069 break;
7070
7071 /* take an item from the stack */
7072 cur_l = list_stack->list;
7073 tempitem = list_stack;
7074 list_stack = list_stack->prev;
7075 free(tempitem);
7076 }
7077
7078 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079}
7080
7081/*
7082 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 * "list_stack" is used to add lists to be marked. Can be NULL.
7084 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7085 *
7086 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007089set_ref_in_item(
7090 typval_T *tv,
7091 int copyID,
7092 ht_stack_T **ht_stack,
7093 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094{
7095 dict_T *dd;
7096 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007098
Bram Moolenaara03f2332016-02-06 18:09:59 +01007099 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007100 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007101 dd = tv->vval.v_dict;
7102 if (dd != NULL && dd->dv_copyID != copyID)
7103 {
7104 /* Didn't see this dict yet. */
7105 dd->dv_copyID = copyID;
7106 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007107 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007108 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7109 }
7110 else
7111 {
7112 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7113 if (newitem == NULL)
7114 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007115 else
7116 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007117 newitem->ht = &dd->dv_hashtab;
7118 newitem->prev = *ht_stack;
7119 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007120 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007121 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007122 }
7123 }
7124 else if (tv->v_type == VAR_LIST)
7125 {
7126 ll = tv->vval.v_list;
7127 if (ll != NULL && ll->lv_copyID != copyID)
7128 {
7129 /* Didn't see this list yet. */
7130 ll->lv_copyID = copyID;
7131 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007132 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007133 abort = set_ref_in_list(ll, copyID, ht_stack);
7134 }
7135 else
7136 {
7137 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007138 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 if (newitem == NULL)
7140 abort = TRUE;
7141 else
7142 {
7143 newitem->list = ll;
7144 newitem->prev = *list_stack;
7145 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007146 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007148 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151}
7152
7153/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 * Allocate an empty header for a dictionary.
7155 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007156 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007157dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158{
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007160
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007163 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007164 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007165 if (first_dict != NULL)
7166 first_dict->dv_used_prev = d;
7167 d->dv_used_next = first_dict;
7168 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007169 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007172 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007173 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007174 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007175 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007176 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178}
7179
7180/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007181 * Allocate an empty dict for a return value.
7182 * Returns OK or FAIL.
7183 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007185rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007186{
7187 dict_T *d = dict_alloc();
7188
7189 if (d == NULL)
7190 return FAIL;
7191
7192 rettv->vval.v_dict = d;
7193 rettv->v_type = VAR_DICT;
7194 ++d->dv_refcount;
7195 return OK;
7196}
7197
7198
7199/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200 * Unreference a Dictionary: decrement the reference count and free it when it
7201 * becomes zero.
7202 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007204dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007206 if (d != NULL && --d->dv_refcount <= 0)
7207 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208}
7209
7210/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007211 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 * Ignores the reference count.
7213 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007215dict_free(
7216 dict_T *d,
7217 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007219 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007220 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007221 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223 /* Remove the dict from the list of dicts for garbage collection. */
7224 if (d->dv_used_prev == NULL)
7225 first_dict = d->dv_used_next;
7226 else
7227 d->dv_used_prev->dv_used_next = d->dv_used_next;
7228 if (d->dv_used_next != NULL)
7229 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7230
7231 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007232 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007233 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007236 if (!HASHITEM_EMPTY(hi))
7237 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007238 /* Remove the item before deleting it, just in case there is
7239 * something recursive causing trouble. */
7240 di = HI2DI(hi);
7241 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007242 if (recurse || (di->di_tv.v_type != VAR_LIST
7243 && di->di_tv.v_type != VAR_DICT))
7244 clear_tv(&di->di_tv);
7245 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246 --todo;
7247 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 vim_free(d);
7251}
7252
7253/*
7254 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 * The "key" is copied to the new item.
7256 * Note that the value of the item "di_tv" still needs to be initialized!
7257 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007259 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007260dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261{
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007264 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007265 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007266 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007267 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007268 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007269 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271}
7272
7273/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 * Make a copy of a Dictionary item.
7275 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007277dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278{
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007281 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7282 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283 if (di != NULL)
7284 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007286 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 copy_tv(&org->di_tv, &di->di_tv);
7288 }
7289 return di;
7290}
7291
7292/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 * Remove item "item" from Dictionary "dict" and free it.
7294 */
7295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007296dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297{
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 if (HASHITEM_EMPTY(hi))
7302 EMSG2(_(e_intern2), "dictitem_remove()");
7303 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 dictitem_free(item);
7306}
7307
7308/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 * Free a dict item. Also clears the value.
7310 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007313{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007315 if (item->di_flags & DI_FLAGS_ALLOC)
7316 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007317}
7318
7319/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7321 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007322 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 * Returns NULL when out of memory.
7324 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007326dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327{
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 dict_T *copy;
7329 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332
7333 if (orig == NULL)
7334 return NULL;
7335
7336 copy = dict_alloc();
7337 if (copy != NULL)
7338 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007339 if (copyID != 0)
7340 {
7341 orig->dv_copyID = copyID;
7342 orig->dv_copydict = copy;
7343 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007344 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007345 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 --todo;
7350
7351 di = dictitem_alloc(hi->hi_key);
7352 if (di == NULL)
7353 break;
7354 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007355 {
7356 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7357 copyID) == FAIL)
7358 {
7359 vim_free(di);
7360 break;
7361 }
7362 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 else
7364 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7365 if (dict_add(copy, di) == FAIL)
7366 {
7367 dictitem_free(di);
7368 break;
7369 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 if (todo > 0)
7375 {
7376 dict_unref(copy);
7377 copy = NULL;
7378 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 }
7380
7381 return copy;
7382}
7383
7384/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007386 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390{
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392}
7393
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007395 * Add a number or string entry to dictionary "d".
7396 * When "str" is NULL use number "nr", otherwise use "str".
7397 * Returns FAIL when out of memory and when key already exists.
7398 */
7399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007400dict_add_nr_str(
7401 dict_T *d,
7402 char *key,
7403 long nr,
7404 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007405{
7406 dictitem_T *item;
7407
7408 item = dictitem_alloc((char_u *)key);
7409 if (item == NULL)
7410 return FAIL;
7411 item->di_tv.v_lock = 0;
7412 if (str == NULL)
7413 {
7414 item->di_tv.v_type = VAR_NUMBER;
7415 item->di_tv.vval.v_number = nr;
7416 }
7417 else
7418 {
7419 item->di_tv.v_type = VAR_STRING;
7420 item->di_tv.vval.v_string = vim_strsave(str);
7421 }
7422 if (dict_add(d, item) == FAIL)
7423 {
7424 dictitem_free(item);
7425 return FAIL;
7426 }
7427 return OK;
7428}
7429
7430/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007431 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007432 * Returns FAIL when out of memory and when key already exists.
7433 */
7434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007435dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007436{
7437 dictitem_T *item;
7438
7439 item = dictitem_alloc((char_u *)key);
7440 if (item == NULL)
7441 return FAIL;
7442 item->di_tv.v_lock = 0;
7443 item->di_tv.v_type = VAR_LIST;
7444 item->di_tv.vval.v_list = list;
7445 if (dict_add(d, item) == FAIL)
7446 {
7447 dictitem_free(item);
7448 return FAIL;
7449 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007450 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007451 return OK;
7452}
7453
7454/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 * Get the number of items in a Dictionary.
7456 */
7457 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007458dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007460 if (d == NULL)
7461 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007462 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007463}
7464
7465/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007466 * Find item "key[len]" in Dictionary "d".
7467 * If "len" is negative use strlen(key).
7468 * Returns NULL when not found.
7469 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007470 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007471dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007473#define AKEYLEN 200
7474 char_u buf[AKEYLEN];
7475 char_u *akey;
7476 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479 if (len < 0)
7480 akey = key;
7481 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007483 tofree = akey = vim_strnsave(key, len);
7484 if (akey == NULL)
7485 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007486 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007487 else
7488 {
7489 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007490 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491 akey = buf;
7492 }
7493
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495 vim_free(tofree);
7496 if (HASHITEM_EMPTY(hi))
7497 return NULL;
7498 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499}
7500
7501/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007502 * Get a string item from a dictionary.
7503 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007504 * Returns NULL if the entry doesn't exist or out of memory.
7505 */
7506 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007507get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007508{
7509 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007510 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007511
7512 di = dict_find(d, key, -1);
7513 if (di == NULL)
7514 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007515 s = get_tv_string(&di->di_tv);
7516 if (save && s != NULL)
7517 s = vim_strsave(s);
7518 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007519}
7520
7521/*
7522 * Get a number item from a dictionary.
7523 * Returns 0 if the entry doesn't exist or out of memory.
7524 */
7525 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007526get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527{
7528 dictitem_T *di;
7529
7530 di = dict_find(d, key, -1);
7531 if (di == NULL)
7532 return 0;
7533 return get_tv_number(&di->di_tv);
7534}
7535
7536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 * Return an allocated string with the string representation of a Dictionary.
7538 * May return NULL.
7539 */
7540 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542{
7543 garray_T ga;
7544 int first = TRUE;
7545 char_u *tofree;
7546 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553 return NULL;
7554 ga_init2(&ga, (int)sizeof(char), 80);
7555 ga_append(&ga, '{');
7556
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007557 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007558 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 --todo;
7563
7564 if (first)
7565 first = FALSE;
7566 else
7567 ga_concat(&ga, (char_u *)", ");
7568
7569 tofree = string_quote(hi->hi_key, FALSE);
7570 if (tofree != NULL)
7571 {
7572 ga_concat(&ga, tofree);
7573 vim_free(tofree);
7574 }
7575 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007576 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 if (s != NULL)
7578 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007580 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007581 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007582 line_breakcheck();
7583
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007586 if (todo > 0)
7587 {
7588 vim_free(ga.ga_data);
7589 return NULL;
7590 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591
7592 ga_append(&ga, '}');
7593 ga_append(&ga, NUL);
7594 return (char_u *)ga.ga_data;
7595}
7596
7597/*
7598 * Allocate a variable for a Dictionary and fill it from "*arg".
7599 * Return OK or FAIL. Returns NOTDONE for {expr}.
7600 */
7601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007602get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603{
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 dict_T *d = NULL;
7605 typval_T tvkey;
7606 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007607 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007610 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611
7612 /*
7613 * First check if it's not a curly-braces thing: {expr}.
7614 * Must do this without evaluating, otherwise a function may be called
7615 * twice. Unfortunately this means we need to call eval1() twice for the
7616 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 if (*start != '}')
7620 {
7621 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7622 return FAIL;
7623 if (*start == '}')
7624 return NOTDONE;
7625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626
7627 if (evaluate)
7628 {
7629 d = dict_alloc();
7630 if (d == NULL)
7631 return FAIL;
7632 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 tvkey.v_type = VAR_UNKNOWN;
7634 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635
7636 *arg = skipwhite(*arg + 1);
7637 while (**arg != '}' && **arg != NUL)
7638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 goto failret;
7641 if (**arg != ':')
7642 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007643 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007644 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645 goto failret;
7646 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007647 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007649 key = get_tv_string_buf_chk(&tvkey, buf);
7650 if (key == NULL || *key == NUL)
7651 {
7652 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7653 if (key != NULL)
7654 EMSG(_(e_emptykey));
7655 clear_tv(&tvkey);
7656 goto failret;
7657 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659
7660 *arg = skipwhite(*arg + 1);
7661 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7662 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007663 if (evaluate)
7664 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 goto failret;
7666 }
7667 if (evaluate)
7668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007669 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 if (item != NULL)
7671 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007672 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007673 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 clear_tv(&tv);
7675 goto failret;
7676 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007677 item = dictitem_alloc(key);
7678 clear_tv(&tvkey);
7679 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007682 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007683 if (dict_add(d, item) == FAIL)
7684 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 }
7686 }
7687
7688 if (**arg == '}')
7689 break;
7690 if (**arg != ',')
7691 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007692 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 goto failret;
7694 }
7695 *arg = skipwhite(*arg + 1);
7696 }
7697
7698 if (**arg != '}')
7699 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007700 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701failret:
7702 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007703 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 return FAIL;
7705 }
7706
7707 *arg = skipwhite(*arg + 1);
7708 if (evaluate)
7709 {
7710 rettv->v_type = VAR_DICT;
7711 rettv->vval.v_dict = d;
7712 ++d->dv_refcount;
7713 }
7714
7715 return OK;
7716}
7717
Bram Moolenaar835dc632016-02-07 14:27:38 +01007718#ifdef FEAT_JOB
7719 static void
7720job_free(job_T *job)
7721{
7722 /* TODO: free any handles */
7723
7724 vim_free(job);
7725}
7726
7727 static void
7728job_unref(job_T *job)
7729{
7730 if (job != NULL && --job->jv_refcount <= 0)
7731 job_free(job);
7732}
7733
7734/*
7735 * Allocate a job. Sets the refcount to one.
7736 */
7737 static job_T *
7738job_alloc(void)
7739{
7740 job_T *job;
7741
7742 job = (job_T *)alloc_clear(sizeof(job_T));
7743 if (job != NULL)
7744 {
7745 job->jv_refcount = 1;
7746 }
7747 return job;
7748}
7749
7750#endif
7751
Bram Moolenaar17a13432016-01-24 14:22:10 +01007752 static char *
7753get_var_special_name(int nr)
7754{
7755 switch (nr)
7756 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007757 case VVAL_FALSE: return "v:false";
7758 case VVAL_TRUE: return "v:true";
7759 case VVAL_NONE: return "v:none";
7760 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007761 }
7762 EMSG2(_(e_intern2), "get_var_special_name()");
7763 return "42";
7764}
7765
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007767 * Return a string with the string representation of a variable.
7768 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007769 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007770 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007771 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007772 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 */
7774 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007775echo_string(
7776 typval_T *tv,
7777 char_u **tofree,
7778 char_u *numbuf,
7779 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007780{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007781 static int recurse = 0;
7782 char_u *r = NULL;
7783
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007785 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007786 if (!did_echo_string_emsg)
7787 {
7788 /* Only give this message once for a recursive call to avoid
7789 * flooding the user with errors. And stop iterating over lists
7790 * and dicts. */
7791 did_echo_string_emsg = TRUE;
7792 EMSG(_("E724: variable nested too deep for displaying"));
7793 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007794 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007795 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007796 }
7797 ++recurse;
7798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 switch (tv->v_type)
7800 {
7801 case VAR_FUNC:
7802 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007803 r = tv->vval.v_string;
7804 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007805
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007807 if (tv->vval.v_list == NULL)
7808 {
7809 *tofree = NULL;
7810 r = NULL;
7811 }
7812 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7813 {
7814 *tofree = NULL;
7815 r = (char_u *)"[...]";
7816 }
7817 else
7818 {
7819 tv->vval.v_list->lv_copyID = copyID;
7820 *tofree = list2string(tv, copyID);
7821 r = *tofree;
7822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007824
Bram Moolenaar8c711452005-01-14 21:53:12 +00007825 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007826 if (tv->vval.v_dict == NULL)
7827 {
7828 *tofree = NULL;
7829 r = NULL;
7830 }
7831 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7832 {
7833 *tofree = NULL;
7834 r = (char_u *)"{...}";
7835 }
7836 else
7837 {
7838 tv->vval.v_dict->dv_copyID = copyID;
7839 *tofree = dict2string(tv, copyID);
7840 r = *tofree;
7841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 case VAR_STRING:
7845 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007846 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007847 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007848 *tofree = NULL;
7849 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007850 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007851
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007852 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007853#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007854 *tofree = NULL;
7855 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7856 r = numbuf;
7857 break;
7858#endif
7859
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007860 case VAR_SPECIAL:
7861 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007862 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007863 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007864 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007865
Bram Moolenaar8502c702014-06-17 12:51:16 +02007866 if (--recurse == 0)
7867 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007869}
7870
7871/*
7872 * Return a string with the string representation of a variable.
7873 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7874 * "numbuf" is used for a number.
7875 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007876 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007877 */
7878 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007879tv2string(
7880 typval_T *tv,
7881 char_u **tofree,
7882 char_u *numbuf,
7883 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884{
7885 switch (tv->v_type)
7886 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007887 case VAR_FUNC:
7888 *tofree = string_quote(tv->vval.v_string, TRUE);
7889 return *tofree;
7890 case VAR_STRING:
7891 *tofree = string_quote(tv->vval.v_string, FALSE);
7892 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007893#ifdef FEAT_FLOAT
7894 case VAR_FLOAT:
7895 *tofree = NULL;
7896 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7897 return numbuf;
7898#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007899 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007901 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007902 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007903 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007904 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007906 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007907 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908}
7909
7910/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 * Return string "str" in ' quotes, doubling ' characters.
7912 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 */
7915 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007916string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917{
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 char_u *p, *r, *s;
7920
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 len = (function ? 13 : 3);
7922 if (str != NULL)
7923 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007924 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 for (p = str; *p != NUL; mb_ptr_adv(p))
7926 if (*p == '\'')
7927 ++len;
7928 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007929 s = r = alloc(len);
7930 if (r != NULL)
7931 {
7932 if (function)
7933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007935 r += 10;
7936 }
7937 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 if (str != NULL)
7940 for (p = str; *p != NUL; )
7941 {
7942 if (*p == '\'')
7943 *r++ = '\'';
7944 MB_COPY_CHAR(p, r);
7945 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007947 if (function)
7948 *r++ = ')';
7949 *r++ = NUL;
7950 }
7951 return s;
7952}
7953
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007954#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007955/*
7956 * Convert the string "text" to a floating point number.
7957 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7958 * this always uses a decimal point.
7959 * Returns the length of the text that was consumed.
7960 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007962string2float(
7963 char_u *text,
7964 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965{
7966 char *s = (char *)text;
7967 float_T f;
7968
7969 f = strtod(s, &s);
7970 *value = f;
7971 return (int)((char_u *)s - text);
7972}
7973#endif
7974
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 * Get the value of an environment variable.
7977 * "arg" is pointing to the '$'. It is advanced to after the name.
7978 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007979 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 */
7981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007982get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
7984 char_u *string = NULL;
7985 int len;
7986 int cc;
7987 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007988 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 ++*arg;
7991 name = *arg;
7992 len = get_env_len(arg);
7993 if (evaluate)
7994 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007995 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007996 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007997
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007998 cc = name[len];
7999 name[len] = NUL;
8000 /* first try vim_getenv(), fast for normal environment vars */
8001 string = vim_getenv(name, &mustfree);
8002 if (string != NULL && *string != NUL)
8003 {
8004 if (!mustfree)
8005 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008007 else
8008 {
8009 if (mustfree)
8010 vim_free(string);
8011
8012 /* next try expanding things like $VIM and ${HOME} */
8013 string = expand_env_save(name - 1);
8014 if (string != NULL && *string == '$')
8015 {
8016 vim_free(string);
8017 string = NULL;
8018 }
8019 }
8020 name[len] = cc;
8021
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 rettv->v_type = VAR_STRING;
8023 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025
8026 return OK;
8027}
8028
8029/*
8030 * Array with names and number of arguments of all internal functions
8031 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8032 */
8033static struct fst
8034{
8035 char *f_name; /* function name */
8036 char f_min_argc; /* minimal number of arguments */
8037 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008038 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008039 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040} functions[] =
8041{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042#ifdef FEAT_FLOAT
8043 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008044 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008045#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008046 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008047 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008048 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"append", 2, 2, f_append},
8050 {"argc", 0, 0, f_argc},
8051 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008052 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008053 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008054#ifdef FEAT_FLOAT
8055 {"asin", 1, 1, f_asin}, /* WJMc */
8056#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008057 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008058 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008059 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008060 {"assert_false", 1, 2, f_assert_false},
8061 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008062#ifdef FEAT_FLOAT
8063 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008064 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008067 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"bufexists", 1, 1, f_bufexists},
8069 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8070 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8071 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8072 {"buflisted", 1, 1, f_buflisted},
8073 {"bufloaded", 1, 1, f_bufloaded},
8074 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008075 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"bufwinnr", 1, 1, f_bufwinnr},
8077 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008078 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008079 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008080 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081#ifdef FEAT_FLOAT
8082 {"ceil", 1, 1, f_ceil},
8083#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008084#ifdef FEAT_CHANNEL
8085 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008086 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008087 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8088 {"ch_sendraw", 2, 3, f_ch_sendraw},
8089#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008090 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008091 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008093 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008095#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008096 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008097 {"complete_add", 1, 1, f_complete_add},
8098 {"complete_check", 0, 0, f_complete_check},
8099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008101 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#ifdef FEAT_FLOAT
8103 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008104 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008106 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008108 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008109 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008110 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008112 {"diff_filler", 1, 1, f_diff_filler},
8113 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008114 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008116 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"eventhandler", 0, 0, f_eventhandler},
8118 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008119 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008121#ifdef FEAT_FLOAT
8122 {"exp", 1, 1, f_exp},
8123#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008124 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008125 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008126 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8128 {"filereadable", 1, 1, f_filereadable},
8129 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008130 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008131 {"finddir", 1, 3, f_finddir},
8132 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008133#ifdef FEAT_FLOAT
8134 {"float2nr", 1, 1, f_float2nr},
8135 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008136 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008137#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008138 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"fnamemodify", 2, 2, f_fnamemodify},
8140 {"foldclosed", 1, 1, f_foldclosed},
8141 {"foldclosedend", 1, 1, f_foldclosedend},
8142 {"foldlevel", 1, 1, f_foldlevel},
8143 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008144 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008147 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008148 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008149 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008150 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"getchar", 0, 1, f_getchar},
8152 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008153 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"getcmdline", 0, 0, f_getcmdline},
8155 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008156 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008157 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008158 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008159 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008160 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008161 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"getfsize", 1, 1, f_getfsize},
8163 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008164 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008165 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008166 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008167 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008168 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008169 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008170 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008171 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008173 {"gettabvar", 2, 3, f_gettabvar},
8174 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"getwinposx", 0, 0, f_getwinposx},
8176 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008177 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008178 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008179 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008180 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008183 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008184 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8186 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8187 {"histadd", 2, 2, f_histadd},
8188 {"histdel", 1, 2, f_histdel},
8189 {"histget", 1, 2, f_histget},
8190 {"histnr", 1, 1, f_histnr},
8191 {"hlID", 1, 1, f_hlID},
8192 {"hlexists", 1, 1, f_hlexists},
8193 {"hostname", 0, 0, f_hostname},
8194 {"iconv", 3, 3, f_iconv},
8195 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008196 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008197 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008199 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"inputrestore", 0, 0, f_inputrestore},
8201 {"inputsave", 0, 0, f_inputsave},
8202 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008203 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008204 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008206 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008207 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008208#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +01008209 {"job_start", 1, 2, f_job_start},
8210 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008211 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008212#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008213 {"join", 1, 2, f_join},
Bram Moolenaar595e64e2016-02-07 19:19:53 +01008214 {"jsdecode", 1, 1, f_jsdecode},
8215 {"jsencode", 1, 1, f_jsencode},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008216 {"jsondecode", 1, 1, f_jsondecode},
8217 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008218 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008220 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"libcall", 3, 3, f_libcall},
8222 {"libcallnr", 3, 3, f_libcallnr},
8223 {"line", 1, 1, f_line},
8224 {"line2byte", 1, 1, f_line2byte},
8225 {"lispindent", 1, 1, f_lispindent},
8226 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008227#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008228 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229 {"log10", 1, 1, f_log10},
8230#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008231#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008232 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008233#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008234 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008235 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008236 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008237 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008238 {"matchadd", 2, 5, f_matchadd},
8239 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008240 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008241 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008242 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008243 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008244 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008245 {"max", 1, 1, f_max},
8246 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008247#ifdef vim_mkdir
8248 {"mkdir", 1, 3, f_mkdir},
8249#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008250 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008251#ifdef FEAT_MZSCHEME
8252 {"mzeval", 1, 1, f_mzeval},
8253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008255 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008256 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008257 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008258#ifdef FEAT_PERL
8259 {"perleval", 1, 1, f_perleval},
8260#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008261#ifdef FEAT_FLOAT
8262 {"pow", 2, 2, f_pow},
8263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008265 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008266 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008267#ifdef FEAT_PYTHON3
8268 {"py3eval", 1, 1, f_py3eval},
8269#endif
8270#ifdef FEAT_PYTHON
8271 {"pyeval", 1, 1, f_pyeval},
8272#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008273 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008274 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008275 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008276 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008277 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {"remote_expr", 2, 3, f_remote_expr},
8279 {"remote_foreground", 1, 1, f_remote_foreground},
8280 {"remote_peek", 1, 2, f_remote_peek},
8281 {"remote_read", 1, 1, f_remote_read},
8282 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008283 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008285 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008287 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008288#ifdef FEAT_FLOAT
8289 {"round", 1, 1, f_round},
8290#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008291 {"screenattr", 2, 2, f_screenattr},
8292 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008293 {"screencol", 0, 0, f_screencol},
8294 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008295 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008296 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008297 {"searchpair", 3, 7, f_searchpair},
8298 {"searchpairpos", 3, 7, f_searchpairpos},
8299 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"server2client", 2, 2, f_server2client},
8301 {"serverlist", 0, 0, f_serverlist},
8302 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008303 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"setcmdpos", 1, 1, f_setcmdpos},
8305 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008306 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008307 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008308 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008309 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008311 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008312 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008314#ifdef FEAT_CRYPT
8315 {"sha256", 1, 1, f_sha256},
8316#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008317 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008318 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008320#ifdef FEAT_FLOAT
8321 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008322 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008323#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008324 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008325 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008326 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008327 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008328 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008329#ifdef FEAT_FLOAT
8330 {"sqrt", 1, 1, f_sqrt},
8331 {"str2float", 1, 1, f_str2float},
8332#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008333 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008334 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008335 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336#ifdef HAVE_STRFTIME
8337 {"strftime", 1, 2, f_strftime},
8338#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008340 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"strlen", 1, 1, f_strlen},
8342 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008343 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008345 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008346 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {"substitute", 4, 4, f_substitute},
8348 {"synID", 3, 3, f_synID},
8349 {"synIDattr", 2, 3, f_synIDattr},
8350 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008351 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008352 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008353 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008354 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008355 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008356 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008357 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008358 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008359 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008360#ifdef FEAT_FLOAT
8361 {"tan", 1, 1, f_tan},
8362 {"tanh", 1, 1, f_tanh},
8363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008365 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {"tolower", 1, 1, f_tolower},
8367 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008368 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008369#ifdef FEAT_FLOAT
8370 {"trunc", 1, 1, f_trunc},
8371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008373 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008374 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008375 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008376 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"virtcol", 1, 1, f_virtcol},
8378 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008379 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"winbufnr", 1, 1, f_winbufnr},
8381 {"wincol", 0, 0, f_wincol},
8382 {"winheight", 1, 1, f_winheight},
8383 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008384 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008386 {"winrestview", 1, 1, f_winrestview},
8387 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008389 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008390 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008391 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392};
8393
8394#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8395
8396/*
8397 * Function given to ExpandGeneric() to obtain the list of internal
8398 * or user defined function names.
8399 */
8400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008401get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 static int intidx = -1;
8404 char_u *name;
8405
8406 if (idx == 0)
8407 intidx = -1;
8408 if (intidx < 0)
8409 {
8410 name = get_user_func_name(xp, idx);
8411 if (name != NULL)
8412 return name;
8413 }
8414 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8415 {
8416 STRCPY(IObuff, functions[intidx].f_name);
8417 STRCAT(IObuff, "(");
8418 if (functions[intidx].f_max_argc == 0)
8419 STRCAT(IObuff, ")");
8420 return IObuff;
8421 }
8422
8423 return NULL;
8424}
8425
8426/*
8427 * Function given to ExpandGeneric() to obtain the list of internal or
8428 * user defined variable or function names.
8429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008431get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432{
8433 static int intidx = -1;
8434 char_u *name;
8435
8436 if (idx == 0)
8437 intidx = -1;
8438 if (intidx < 0)
8439 {
8440 name = get_function_name(xp, idx);
8441 if (name != NULL)
8442 return name;
8443 }
8444 return get_user_var_name(xp, ++intidx);
8445}
8446
8447#endif /* FEAT_CMDL_COMPL */
8448
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008449#if defined(EBCDIC) || defined(PROTO)
8450/*
8451 * Compare struct fst by function name.
8452 */
8453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008454compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008455{
8456 struct fst *p1 = (struct fst *)s1;
8457 struct fst *p2 = (struct fst *)s2;
8458
8459 return STRCMP(p1->f_name, p2->f_name);
8460}
8461
8462/*
8463 * Sort the function table by function name.
8464 * The sorting of the table above is ASCII dependant.
8465 * On machines using EBCDIC we have to sort it.
8466 */
8467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008468sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008469{
8470 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8471
8472 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8473}
8474#endif
8475
8476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477/*
8478 * Find internal function in table above.
8479 * Return index, or -1 if not found
8480 */
8481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008482find_internal_func(
8483 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 int first = 0;
8486 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8487 int cmp;
8488 int x;
8489
8490 /*
8491 * Find the function name in the table. Binary search.
8492 */
8493 while (first <= last)
8494 {
8495 x = first + ((unsigned)(last - first) >> 1);
8496 cmp = STRCMP(name, functions[x].f_name);
8497 if (cmp < 0)
8498 last = x - 1;
8499 else if (cmp > 0)
8500 first = x + 1;
8501 else
8502 return x;
8503 }
8504 return -1;
8505}
8506
8507/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008508 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8509 * name it contains, otherwise return "name".
8510 */
8511 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008512deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008513{
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008515 int cc;
8516
8517 cc = name[*lenp];
8518 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008519 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 {
8525 *lenp = 0;
8526 return (char_u *)""; /* just in case */
8527 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008528 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 }
8531
8532 return name;
8533}
8534
8535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 * Allocate a variable for the result of a function.
8537 * Return OK or FAIL.
8538 */
8539 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008540get_func_tv(
8541 char_u *name, /* name of the function */
8542 int len, /* length of "name" */
8543 typval_T *rettv,
8544 char_u **arg, /* argument, pointing to the '(' */
8545 linenr_T firstline, /* first line of range */
8546 linenr_T lastline, /* last line of range */
8547 int *doesrange, /* return: function handled range */
8548 int evaluate,
8549 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 char_u *argp;
8552 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008553 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 int argcount = 0; /* number of arguments found */
8555
8556 /*
8557 * Get the arguments.
8558 */
8559 argp = *arg;
8560 while (argcount < MAX_FUNC_ARGS)
8561 {
8562 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8563 if (*argp == ')' || *argp == ',' || *argp == NUL)
8564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8566 {
8567 ret = FAIL;
8568 break;
8569 }
8570 ++argcount;
8571 if (*argp != ',')
8572 break;
8573 }
8574 if (*argp == ')')
8575 ++argp;
8576 else
8577 ret = FAIL;
8578
8579 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008581 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008583 {
8584 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008585 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008587 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
8593 *arg = skipwhite(argp);
8594 return ret;
8595}
8596
8597
8598/*
8599 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008600 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008601 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008604call_func(
8605 char_u *funcname, /* name of the function */
8606 int len, /* length of "name" */
8607 typval_T *rettv, /* return value goes here */
8608 int argcount, /* number of "argvars" */
8609 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008610 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008611 linenr_T firstline, /* first line of range */
8612 linenr_T lastline, /* last line of range */
8613 int *doesrange, /* return: function handled range */
8614 int evaluate,
8615 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618#define ERROR_UNKNOWN 0
8619#define ERROR_TOOMANY 1
8620#define ERROR_TOOFEW 2
8621#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008622#define ERROR_DICT 4
8623#define ERROR_NONE 5
8624#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 int error = ERROR_NONE;
8626 int i;
8627 int llen;
8628 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629#define FLEN_FIXED 40
8630 char_u fname_buf[FLEN_FIXED + 1];
8631 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008632 char_u *name;
8633
8634 /* Make a copy of the name, if it comes from a funcref variable it could
8635 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008636 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008637 if (name == NULL)
8638 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639
8640 /*
8641 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8642 * Change <SNR>123_name() to K_SNR 123_name().
8643 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 llen = eval_fname_script(name);
8646 if (llen > 0)
8647 {
8648 fname_buf[0] = K_SPECIAL;
8649 fname_buf[1] = KS_EXTRA;
8650 fname_buf[2] = (int)KE_SNR;
8651 i = 3;
8652 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8653 {
8654 if (current_SID <= 0)
8655 error = ERROR_SCRIPT;
8656 else
8657 {
8658 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8659 i = (int)STRLEN(fname_buf);
8660 }
8661 }
8662 if (i + STRLEN(name + llen) < FLEN_FIXED)
8663 {
8664 STRCPY(fname_buf + i, name + llen);
8665 fname = fname_buf;
8666 }
8667 else
8668 {
8669 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8670 if (fname == NULL)
8671 error = ERROR_OTHER;
8672 else
8673 {
8674 mch_memmove(fname, fname_buf, (size_t)i);
8675 STRCPY(fname + i, name + llen);
8676 }
8677 }
8678 }
8679 else
8680 fname = name;
8681
8682 *doesrange = FALSE;
8683
8684
8685 /* execute the function if no errors detected and executing */
8686 if (evaluate && error == ERROR_NONE)
8687 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008688 char_u *rfname = fname;
8689
8690 /* Ignore "g:" before a function name. */
8691 if (fname[0] == 'g' && fname[1] == ':')
8692 rfname = fname + 2;
8693
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008694 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8695 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 error = ERROR_UNKNOWN;
8697
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008698 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {
8700 /*
8701 * User defined function.
8702 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008703 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008706 /* Trigger FuncUndefined event, may load the function. */
8707 if (fp == NULL
8708 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008709 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008710 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008712 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008713 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 }
8715#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008716 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008717 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008718 {
8719 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008720 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008721 }
8722
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 if (fp != NULL)
8724 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008725 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008727 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008729 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008731 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008732 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 else
8734 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008735 int did_save_redo = FALSE;
8736
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 /*
8738 * Call the user function.
8739 * Save and restore search patterns, script variables and
8740 * redo buffer.
8741 */
8742 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008743#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008744 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008745#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008746 {
8747 saveRedobuff();
8748 did_save_redo = TRUE;
8749 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008750 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008752 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008753 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8754 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8755 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008756 /* Function was unreferenced while being used, free it
8757 * now. */
8758 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008759 if (did_save_redo)
8760 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 restore_search_patterns();
8762 error = ERROR_NONE;
8763 }
8764 }
8765 }
8766 else
8767 {
8768 /*
8769 * Find the function name in the table, call its implementation.
8770 */
8771 i = find_internal_func(fname);
8772 if (i >= 0)
8773 {
8774 if (argcount < functions[i].f_min_argc)
8775 error = ERROR_TOOFEW;
8776 else if (argcount > functions[i].f_max_argc)
8777 error = ERROR_TOOMANY;
8778 else
8779 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008780 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 error = ERROR_NONE;
8783 }
8784 }
8785 }
8786 /*
8787 * The function call (or "FuncUndefined" autocommand sequence) might
8788 * have been aborted by an error, an interrupt, or an explicitly thrown
8789 * exception that has not been caught so far. This situation can be
8790 * tested for by calling aborting(). For an error in an internal
8791 * function or for the "E132" error in call_user_func(), however, the
8792 * throw point at which the "force_abort" flag (temporarily reset by
8793 * emsg()) is normally updated has not been reached yet. We need to
8794 * update that flag first to make aborting() reliable.
8795 */
8796 update_force_abort();
8797 }
8798 if (error == ERROR_NONE)
8799 ret = OK;
8800
8801 /*
8802 * Report an error unless the argument evaluation or function call has been
8803 * cancelled due to an aborting error, an interrupt, or an exception.
8804 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008805 if (!aborting())
8806 {
8807 switch (error)
8808 {
8809 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008810 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008811 break;
8812 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008813 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008814 break;
8815 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008816 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008817 name);
8818 break;
8819 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008820 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008821 name);
8822 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008823 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008824 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008825 name);
8826 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008827 }
8828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 if (fname != name && fname != fname_buf)
8831 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008832 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833
8834 return ret;
8835}
8836
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008837/*
8838 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008839 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008840 */
8841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008842emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008843{
8844 char_u *p;
8845
8846 if (*name == K_SPECIAL)
8847 p = concat_str((char_u *)"<SNR>", name + 3);
8848 else
8849 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008850 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008851 if (p != name)
8852 vim_free(p);
8853}
8854
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008855/*
8856 * Return TRUE for a non-zero Number and a non-empty String.
8857 */
8858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008859non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008860{
8861 return ((argvars[0].v_type == VAR_NUMBER
8862 && argvars[0].vval.v_number != 0)
8863 || (argvars[0].v_type == VAR_STRING
8864 && argvars[0].vval.v_string != NULL
8865 && *argvars[0].vval.v_string != NUL));
8866}
8867
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868/*********************************************
8869 * Implementation of the built-in functions
8870 */
8871
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008872#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008873static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008874
8875/*
8876 * Get the float value of "argvars[0]" into "f".
8877 * Returns FAIL when the argument is not a Number or Float.
8878 */
8879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008880get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008881{
8882 if (argvars[0].v_type == VAR_FLOAT)
8883 {
8884 *f = argvars[0].vval.v_float;
8885 return OK;
8886 }
8887 if (argvars[0].v_type == VAR_NUMBER)
8888 {
8889 *f = (float_T)argvars[0].vval.v_number;
8890 return OK;
8891 }
8892 EMSG(_("E808: Number or Float required"));
8893 return FAIL;
8894}
8895
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008896/*
8897 * "abs(expr)" function
8898 */
8899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008900f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008901{
8902 if (argvars[0].v_type == VAR_FLOAT)
8903 {
8904 rettv->v_type = VAR_FLOAT;
8905 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8906 }
8907 else
8908 {
8909 varnumber_T n;
8910 int error = FALSE;
8911
8912 n = get_tv_number_chk(&argvars[0], &error);
8913 if (error)
8914 rettv->vval.v_number = -1;
8915 else if (n > 0)
8916 rettv->vval.v_number = n;
8917 else
8918 rettv->vval.v_number = -n;
8919 }
8920}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008921
8922/*
8923 * "acos()" function
8924 */
8925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008926f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008927{
8928 float_T f;
8929
8930 rettv->v_type = VAR_FLOAT;
8931 if (get_float_arg(argvars, &f) == OK)
8932 rettv->vval.v_float = acos(f);
8933 else
8934 rettv->vval.v_float = 0.0;
8935}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008936#endif
8937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008939 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 */
8941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008942f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008947 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008949 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008950 && !tv_check_lock(l->lv_lock,
8951 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008952 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 }
8955 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008956 EMSG(_(e_listreq));
8957}
8958
8959/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008960 * "alloc_fail(id, countdown, repeat)" function
8961 */
8962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008963f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008964{
8965 if (argvars[0].v_type != VAR_NUMBER
8966 || argvars[0].vval.v_number <= 0
8967 || argvars[1].v_type != VAR_NUMBER
8968 || argvars[1].vval.v_number < 0
8969 || argvars[2].v_type != VAR_NUMBER)
8970 EMSG(_(e_invarg));
8971 else
8972 {
8973 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008974 if (alloc_fail_id >= aid_last)
8975 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008976 alloc_fail_countdown = argvars[1].vval.v_number;
8977 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008978 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008979 }
8980}
8981
8982/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008983 * "and(expr, expr)" function
8984 */
8985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008986f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008987{
8988 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8989 & get_tv_number_chk(&argvars[1], NULL);
8990}
8991
8992/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008993 * "append(lnum, string/list)" function
8994 */
8995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008996f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008997{
8998 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008999 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009000 list_T *l = NULL;
9001 listitem_T *li = NULL;
9002 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009003 long added = 0;
9004
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009005 /* When coming here from Insert mode, sync undo, so that this can be
9006 * undone separately from what was previously inserted. */
9007 if (u_sync_once == 2)
9008 {
9009 u_sync_once = 1; /* notify that u_sync() was called */
9010 u_sync(TRUE);
9011 }
9012
Bram Moolenaar0d660222005-01-07 21:51:51 +00009013 lnum = get_tv_lnum(argvars);
9014 if (lnum >= 0
9015 && lnum <= curbuf->b_ml.ml_line_count
9016 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009017 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009018 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009019 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009020 l = argvars[1].vval.v_list;
9021 if (l == NULL)
9022 return;
9023 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009025 for (;;)
9026 {
9027 if (l == NULL)
9028 tv = &argvars[1]; /* append a string */
9029 else if (li == NULL)
9030 break; /* end of list */
9031 else
9032 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 line = get_tv_string_chk(tv);
9034 if (line == NULL) /* type error */
9035 {
9036 rettv->vval.v_number = 1; /* Failed */
9037 break;
9038 }
9039 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009040 ++added;
9041 if (l == NULL)
9042 break;
9043 li = li->li_next;
9044 }
9045
9046 appended_lines_mark(lnum, added);
9047 if (curwin->w_cursor.lnum > lnum)
9048 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009050 else
9051 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052}
9053
9054/*
9055 * "argc()" function
9056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009058f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061}
9062
9063/*
9064 * "argidx()" function
9065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009067f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070}
9071
9072/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009073 * "arglistid()" function
9074 */
9075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009076f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009077{
9078 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009079
9080 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009081 wp = find_tabwin(&argvars[0], &argvars[1]);
9082 if (wp != NULL)
9083 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009084}
9085
9086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 * "argv(nr)" function
9088 */
9089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009090f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091{
9092 int idx;
9093
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009094 if (argvars[0].v_type != VAR_UNKNOWN)
9095 {
9096 idx = get_tv_number_chk(&argvars[0], NULL);
9097 if (idx >= 0 && idx < ARGCOUNT)
9098 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9099 else
9100 rettv->vval.v_string = NULL;
9101 rettv->v_type = VAR_STRING;
9102 }
9103 else if (rettv_list_alloc(rettv) == OK)
9104 for (idx = 0; idx < ARGCOUNT; ++idx)
9105 list_append_string(rettv->vval.v_list,
9106 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107}
9108
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009109static void prepare_assert_error(garray_T*gap);
9110static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9111static void assert_error(garray_T *gap);
9112static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009113
9114/*
9115 * Prepare "gap" for an assert error and add the sourcing position.
9116 */
9117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009118prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009119{
9120 char buf[NUMBUFLEN];
9121
9122 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009123 if (sourcing_name != NULL)
9124 {
9125 ga_concat(gap, sourcing_name);
9126 if (sourcing_lnum > 0)
9127 ga_concat(gap, (char_u *)" ");
9128 }
9129 if (sourcing_lnum > 0)
9130 {
9131 sprintf(buf, "line %ld", (long)sourcing_lnum);
9132 ga_concat(gap, (char_u *)buf);
9133 }
9134 if (sourcing_name != NULL || sourcing_lnum > 0)
9135 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009136}
9137
9138/*
9139 * Fill "gap" with information about an assert error.
9140 */
9141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009142fill_assert_error(
9143 garray_T *gap,
9144 typval_T *opt_msg_tv,
9145 char_u *exp_str,
9146 typval_T *exp_tv,
9147 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009148{
9149 char_u numbuf[NUMBUFLEN];
9150 char_u *tofree;
9151
9152 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9153 {
9154 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9155 vim_free(tofree);
9156 }
9157 else
9158 {
9159 ga_concat(gap, (char_u *)"Expected ");
9160 if (exp_str == NULL)
9161 {
9162 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9163 vim_free(tofree);
9164 }
9165 else
9166 ga_concat(gap, exp_str);
9167 ga_concat(gap, (char_u *)" but got ");
9168 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9169 vim_free(tofree);
9170 }
9171}
Bram Moolenaar43345542015-11-29 17:35:35 +01009172
9173/*
9174 * Add an assert error to v:errors.
9175 */
9176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009177assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009178{
9179 struct vimvar *vp = &vimvars[VV_ERRORS];
9180
9181 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9182 /* Make sure v:errors is a list. */
9183 set_vim_var_list(VV_ERRORS, list_alloc());
9184 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9185}
9186
Bram Moolenaar43345542015-11-29 17:35:35 +01009187/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009188 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009189 */
9190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009191f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009192{
9193 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009194
9195 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9196 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009197 prepare_assert_error(&ga);
9198 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9199 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009200 ga_clear(&ga);
9201 }
9202}
9203
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009204/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009205 * "assert_exception(string[, msg])" function
9206 */
9207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009208f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009209{
9210 garray_T ga;
9211 char *error;
9212
9213 error = (char *)get_tv_string_chk(&argvars[0]);
9214 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9215 {
9216 prepare_assert_error(&ga);
9217 ga_concat(&ga, (char_u *)"v:exception is not set");
9218 assert_error(&ga);
9219 ga_clear(&ga);
9220 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009221 else if (error != NULL
9222 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009223 {
9224 prepare_assert_error(&ga);
9225 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9226 &vimvars[VV_EXCEPTION].vv_tv);
9227 assert_error(&ga);
9228 ga_clear(&ga);
9229 }
9230}
9231
9232/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009233 * "assert_fails(cmd [, error])" function
9234 */
9235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009236f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009237{
9238 char_u *cmd = get_tv_string_chk(&argvars[0]);
9239 garray_T ga;
9240
9241 called_emsg = FALSE;
9242 suppress_errthrow = TRUE;
9243 emsg_silent = TRUE;
9244 do_cmdline_cmd(cmd);
9245 if (!called_emsg)
9246 {
9247 prepare_assert_error(&ga);
9248 ga_concat(&ga, (char_u *)"command did not fail: ");
9249 ga_concat(&ga, cmd);
9250 assert_error(&ga);
9251 ga_clear(&ga);
9252 }
9253 else if (argvars[1].v_type != VAR_UNKNOWN)
9254 {
9255 char_u buf[NUMBUFLEN];
9256 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9257
9258 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9259 {
9260 prepare_assert_error(&ga);
9261 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9262 &vimvars[VV_ERRMSG].vv_tv);
9263 assert_error(&ga);
9264 ga_clear(&ga);
9265 }
9266 }
9267
9268 called_emsg = FALSE;
9269 suppress_errthrow = FALSE;
9270 emsg_silent = FALSE;
9271 emsg_on_display = FALSE;
9272 set_vim_var_string(VV_ERRMSG, NULL, 0);
9273}
9274
9275/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009276 * Common for assert_true() and assert_false().
9277 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009279assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009280{
9281 int error = FALSE;
9282 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009283
Bram Moolenaar37127922016-02-06 20:29:28 +01009284 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009285 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009286 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009287 if (argvars[0].v_type != VAR_NUMBER
9288 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9289 || error)
9290 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009291 prepare_assert_error(&ga);
9292 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009293 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009294 NULL, &argvars[0]);
9295 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009296 ga_clear(&ga);
9297 }
9298}
9299
9300/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009301 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009302 */
9303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009305{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009306 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009307}
9308
9309/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009310 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009311 */
9312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009313f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009314{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009316}
9317
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009318#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009319/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009320 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009321 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009323f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009324{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009325 float_T f;
9326
9327 rettv->v_type = VAR_FLOAT;
9328 if (get_float_arg(argvars, &f) == OK)
9329 rettv->vval.v_float = asin(f);
9330 else
9331 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009332}
9333
9334/*
9335 * "atan()" function
9336 */
9337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009338f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009339{
9340 float_T f;
9341
9342 rettv->v_type = VAR_FLOAT;
9343 if (get_float_arg(argvars, &f) == OK)
9344 rettv->vval.v_float = atan(f);
9345 else
9346 rettv->vval.v_float = 0.0;
9347}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009348
9349/*
9350 * "atan2()" function
9351 */
9352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009353f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009354{
9355 float_T fx, fy;
9356
9357 rettv->v_type = VAR_FLOAT;
9358 if (get_float_arg(argvars, &fx) == OK
9359 && get_float_arg(&argvars[1], &fy) == OK)
9360 rettv->vval.v_float = atan2(fx, fy);
9361 else
9362 rettv->vval.v_float = 0.0;
9363}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009364#endif
9365
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366/*
9367 * "browse(save, title, initdir, default)" function
9368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009370f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
9372#ifdef FEAT_BROWSE
9373 int save;
9374 char_u *title;
9375 char_u *initdir;
9376 char_u *defname;
9377 char_u buf[NUMBUFLEN];
9378 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 save = get_tv_number_chk(&argvars[0], &error);
9382 title = get_tv_string_chk(&argvars[1]);
9383 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9384 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 if (error || title == NULL || initdir == NULL || defname == NULL)
9387 rettv->vval.v_string = NULL;
9388 else
9389 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009390 do_browse(save ? BROWSE_SAVE : 0,
9391 title, defname, NULL, initdir, NULL, curbuf);
9392#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009394#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009396}
9397
9398/*
9399 * "browsedir(title, initdir)" function
9400 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009402f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009403{
9404#ifdef FEAT_BROWSE
9405 char_u *title;
9406 char_u *initdir;
9407 char_u buf[NUMBUFLEN];
9408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009409 title = get_tv_string_chk(&argvars[0]);
9410 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 if (title == NULL || initdir == NULL)
9413 rettv->vval.v_string = NULL;
9414 else
9415 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009416 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421}
9422
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009423static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009424
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425/*
9426 * Find a buffer by number or exact name.
9427 */
9428 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009429find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009433 if (avar->v_type == VAR_NUMBER)
9434 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009435 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009437 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009438 if (buf == NULL)
9439 {
9440 /* No full path name match, try a match with a URL or a "nofile"
9441 * buffer, these don't use the full path. */
9442 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9443 if (buf->b_fname != NULL
9444 && (path_with_url(buf->b_fname)
9445#ifdef FEAT_QUICKFIX
9446 || bt_nofile(buf)
9447#endif
9448 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009449 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009450 break;
9451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 }
9453 return buf;
9454}
9455
9456/*
9457 * "bufexists(expr)" function
9458 */
9459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009460f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463}
9464
9465/*
9466 * "buflisted(expr)" function
9467 */
9468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009469f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 buf_T *buf;
9472
9473 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475}
9476
9477/*
9478 * "bufloaded(expr)" function
9479 */
9480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009481f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
9483 buf_T *buf;
9484
9485 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487}
9488
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009489static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009490
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491/*
9492 * Get buffer by number or pattern.
9493 */
9494 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009495get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 int save_magic;
9499 char_u *save_cpo;
9500 buf_T *buf;
9501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 if (tv->v_type == VAR_NUMBER)
9503 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009504 if (tv->v_type != VAR_STRING)
9505 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (name == NULL || *name == NUL)
9507 return curbuf;
9508 if (name[0] == '$' && name[1] == NUL)
9509 return lastbuf;
9510
9511 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9512 save_magic = p_magic;
9513 p_magic = TRUE;
9514 save_cpo = p_cpo;
9515 p_cpo = (char_u *)"";
9516
9517 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009518 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519
9520 p_magic = save_magic;
9521 p_cpo = save_cpo;
9522
9523 /* If not found, try expanding the name, like done for bufexists(). */
9524 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526
9527 return buf;
9528}
9529
9530/*
9531 * "bufname(expr)" function
9532 */
9533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536 buf_T *buf;
9537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009540 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 --emsg_off;
9547}
9548
9549/*
9550 * "bufnr(expr)" function
9551 */
9552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009553f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009556 int error = FALSE;
9557 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009561 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009562 --emsg_off;
9563
9564 /* If the buffer isn't found and the second argument is not zero create a
9565 * new buffer. */
9566 if (buf == NULL
9567 && argvars[1].v_type != VAR_UNKNOWN
9568 && get_tv_number_chk(&argvars[1], &error) != 0
9569 && !error
9570 && (name = get_tv_string_chk(&argvars[0])) != NULL
9571 && !error)
9572 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9573
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578}
9579
9580/*
9581 * "bufwinnr(nr)" function
9582 */
9583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009584f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585{
9586#ifdef FEAT_WINDOWS
9587 win_T *wp;
9588 int winnr = 0;
9589#endif
9590 buf_T *buf;
9591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009594 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#ifdef FEAT_WINDOWS
9596 for (wp = firstwin; wp; wp = wp->w_next)
9597 {
9598 ++winnr;
9599 if (wp->w_buffer == buf)
9600 break;
9601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605#endif
9606 --emsg_off;
9607}
9608
9609/*
9610 * "byte2line(byte)" function
9611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009613f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617#else
9618 long boff = 0;
9619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 (linenr_T)0, &boff);
9626#endif
9627}
9628
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009630byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009631{
9632#ifdef FEAT_MBYTE
9633 char_u *t;
9634#endif
9635 char_u *str;
9636 long idx;
9637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 str = get_tv_string_chk(&argvars[0]);
9639 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009641 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009642 return;
9643
9644#ifdef FEAT_MBYTE
9645 t = str;
9646 for ( ; idx > 0; idx--)
9647 {
9648 if (*t == NUL) /* EOL reached */
9649 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009650 if (enc_utf8 && comp)
9651 t += utf_ptr2len(t);
9652 else
9653 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009654 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009655 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009656#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009657 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009659#endif
9660}
9661
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009662/*
9663 * "byteidx()" function
9664 */
9665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009666f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009667{
9668 byteidx(argvars, rettv, FALSE);
9669}
9670
9671/*
9672 * "byteidxcomp()" function
9673 */
9674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009675f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009676{
9677 byteidx(argvars, rettv, TRUE);
9678}
9679
Bram Moolenaardb913952012-06-29 12:54:53 +02009680 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009681func_call(
9682 char_u *name,
9683 typval_T *args,
9684 dict_T *selfdict,
9685 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009686{
9687 listitem_T *item;
9688 typval_T argv[MAX_FUNC_ARGS + 1];
9689 int argc = 0;
9690 int dummy;
9691 int r = 0;
9692
9693 for (item = args->vval.v_list->lv_first; item != NULL;
9694 item = item->li_next)
9695 {
9696 if (argc == MAX_FUNC_ARGS)
9697 {
9698 EMSG(_("E699: Too many arguments"));
9699 break;
9700 }
9701 /* Make a copy of each argument. This is needed to be able to set
9702 * v_lock to VAR_FIXED in the copy without changing the original list.
9703 */
9704 copy_tv(&item->li_tv, &argv[argc++]);
9705 }
9706
9707 if (item == NULL)
9708 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9709 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9710 &dummy, TRUE, selfdict);
9711
9712 /* Free the arguments. */
9713 while (argc > 0)
9714 clear_tv(&argv[--argc]);
9715
9716 return r;
9717}
9718
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009719/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009720 * "call(func, arglist)" function
9721 */
9722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009723f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009724{
9725 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009726 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009727
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009728 if (argvars[1].v_type != VAR_LIST)
9729 {
9730 EMSG(_(e_listreq));
9731 return;
9732 }
9733 if (argvars[1].vval.v_list == NULL)
9734 return;
9735
9736 if (argvars[0].v_type == VAR_FUNC)
9737 func = argvars[0].vval.v_string;
9738 else
9739 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009740 if (*func == NUL)
9741 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009742
Bram Moolenaare9a41262005-01-15 22:18:47 +00009743 if (argvars[2].v_type != VAR_UNKNOWN)
9744 {
9745 if (argvars[2].v_type != VAR_DICT)
9746 {
9747 EMSG(_(e_dictreq));
9748 return;
9749 }
9750 selfdict = argvars[2].vval.v_dict;
9751 }
9752
Bram Moolenaardb913952012-06-29 12:54:53 +02009753 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009754}
9755
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009756#ifdef FEAT_FLOAT
9757/*
9758 * "ceil({float})" function
9759 */
9760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009761f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009762{
9763 float_T f;
9764
9765 rettv->v_type = VAR_FLOAT;
9766 if (get_float_arg(argvars, &f) == OK)
9767 rettv->vval.v_float = ceil(f);
9768 else
9769 rettv->vval.v_float = 0.0;
9770}
9771#endif
9772
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009773#ifdef FEAT_CHANNEL
9774/*
9775 * Get the channel index from the handle argument.
9776 * Returns -1 if the handle is invalid or the channel is closed.
9777 */
9778 static int
9779get_channel_arg(typval_T *tv)
9780{
9781 int ch_idx;
9782
9783 if (tv->v_type != VAR_NUMBER)
9784 {
9785 EMSG2(_(e_invarg2), get_tv_string(tv));
9786 return -1;
9787 }
9788 ch_idx = tv->vval.v_number;
9789
9790 if (!channel_is_open(ch_idx))
9791 {
9792 EMSGN(_("E906: not an open channel"), ch_idx);
9793 return -1;
9794 }
9795 return ch_idx;
9796}
9797
9798/*
9799 * "ch_close()" function
9800 */
9801 static void
9802f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9803{
9804 int ch_idx = get_channel_arg(&argvars[0]);
9805
9806 if (ch_idx >= 0)
9807 channel_close(ch_idx);
9808}
9809
9810/*
9811 * Get a callback from "arg". It can be a Funcref or a function name.
9812 * When "arg" is zero return an empty string.
9813 * Return NULL for an invalid argument.
9814 */
9815 static char_u *
9816get_callback(typval_T *arg)
9817{
9818 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9819 return arg->vval.v_string;
9820 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9821 return (char_u *)"";
9822 EMSG(_("E999: Invalid callback argument"));
9823 return NULL;
9824}
9825
9826/*
9827 * "ch_open()" function
9828 */
9829 static void
9830f_ch_open(typval_T *argvars, typval_T *rettv)
9831{
9832 char_u *address;
9833 char_u *mode;
9834 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009835 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009836 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009837 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009838 int waittime = 0;
9839 int timeout = 2000;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009840 ch_mode_T ch_mode = MODE_JSON;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009841 int ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009842
9843 /* default: fail */
9844 rettv->vval.v_number = -1;
9845
9846 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009847 if (argvars[1].v_type != VAR_UNKNOWN
9848 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009849 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009850 EMSG(_(e_invarg));
9851 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009852 }
9853
9854 /* parse address */
9855 p = vim_strchr(address, ':');
9856 if (p == NULL)
9857 {
9858 EMSG2(_(e_invarg2), address);
9859 return;
9860 }
9861 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009862 port = strtol((char *)p, &rest, 10);
9863 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009864 {
9865 p[-1] = ':';
9866 EMSG2(_(e_invarg2), address);
9867 return;
9868 }
9869
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009870 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009871 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009872 /* parse argdict */
9873 dict_T *dict = argvars[1].vval.v_dict;
9874
9875 if (dict_find(dict, (char_u *)"mode", -1) != NULL)
9876 {
9877 mode = get_dict_string(dict, (char_u *)"mode", FALSE);
9878 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009879 ch_mode = MODE_RAW;
9880 else if (STRCMP(mode, "js") == 0)
9881 ch_mode = MODE_JS;
9882 else if (STRCMP(mode, "json") == 0)
9883 ch_mode = MODE_JSON;
9884 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009885 {
9886 EMSG2(_(e_invarg2), mode);
9887 return;
9888 }
9889 }
9890 if (dict_find(dict, (char_u *)"waittime", -1) != NULL)
9891 waittime = get_dict_number(dict, (char_u *)"waittime");
9892 if (dict_find(dict, (char_u *)"timeout", -1) != NULL)
9893 timeout = get_dict_number(dict, (char_u *)"timeout");
9894 if (dict_find(dict, (char_u *)"callback", -1) != NULL)
9895 callback = get_dict_string(dict, (char_u *)"callback", FALSE);
9896 }
9897 if (waittime < 0 || timeout < 0)
9898 {
9899 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009900 return;
9901 }
9902
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009903 ch_idx = channel_open((char *)address, port, waittime, NULL);
9904 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009905 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009906 channel_set_json_mode(ch_idx, ch_mode);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009907 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009908 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009909 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009910 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009911 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009912}
9913
9914/*
9915 * common for "sendexpr()" and "sendraw()"
9916 * Returns the channel index if the caller should read the response.
9917 * Otherwise returns -1.
9918 */
9919 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009920send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009921{
9922 int ch_idx;
9923 char_u *callback = NULL;
9924
9925 ch_idx = get_channel_arg(&argvars[0]);
9926 if (ch_idx < 0)
9927 return -1;
9928
9929 if (argvars[2].v_type != VAR_UNKNOWN)
9930 {
9931 callback = get_callback(&argvars[2]);
9932 if (callback == NULL)
9933 return -1;
9934 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009935 /* Set the callback. An empty callback means no callback and not reading
9936 * the response. */
9937 if (callback != NULL && *callback != NUL)
9938 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009939
9940 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9941 return ch_idx;
9942 return -1;
9943}
9944
9945/*
9946 * "ch_sendexpr()" function
9947 */
9948 static void
9949f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9950{
9951 char_u *text;
9952 typval_T *listtv;
9953 int ch_idx;
9954 int id;
9955
9956 /* return an empty string by default */
9957 rettv->v_type = VAR_STRING;
9958 rettv->vval.v_string = NULL;
9959
9960 id = channel_get_id();
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009961 text = json_encode_nr_expr(id, &argvars[1], 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009962 if (text == NULL)
9963 return;
9964
Bram Moolenaara07fec92016-02-05 21:04:08 +01009965 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009966 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009967 if (ch_idx >= 0)
9968 {
9969 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9970 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009971 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009972
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009973 /* Move the item from the list and then change the type to
9974 * avoid the value being freed. */
9975 *rettv = list->lv_last->li_tv;
9976 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009977 clear_tv(listtv);
9978 }
9979 }
9980}
9981
9982/*
9983 * "ch_sendraw()" function
9984 */
9985 static void
9986f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9987{
9988 char_u buf[NUMBUFLEN];
9989 char_u *text;
9990 int ch_idx;
9991
9992 /* return an empty string by default */
9993 rettv->v_type = VAR_STRING;
9994 rettv->vval.v_string = NULL;
9995
9996 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +01009997 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009998 if (ch_idx >= 0)
9999 rettv->vval.v_string = channel_read_block(ch_idx);
10000}
10001#endif
10002
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010003/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010004 * "changenr()" function
10005 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010007f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010008{
10009 rettv->vval.v_number = curbuf->b_u_seq_cur;
10010}
10011
10012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 * "char2nr(string)" function
10014 */
10015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010016f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017{
10018#ifdef FEAT_MBYTE
10019 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010020 {
10021 int utf8 = 0;
10022
10023 if (argvars[1].v_type != VAR_UNKNOWN)
10024 utf8 = get_tv_number_chk(&argvars[1], NULL);
10025
10026 if (utf8)
10027 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10028 else
10029 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 else
10032#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034}
10035
10036/*
10037 * "cindent(lnum)" function
10038 */
10039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010040f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041{
10042#ifdef FEAT_CINDENT
10043 pos_T pos;
10044 linenr_T lnum;
10045
10046 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10049 {
10050 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 curwin->w_cursor = pos;
10053 }
10054 else
10055#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057}
10058
10059/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010060 * "clearmatches()" function
10061 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010063f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010064{
10065#ifdef FEAT_SEARCH_EXTRA
10066 clear_matches(curwin);
10067#endif
10068}
10069
10070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 * "col(string)" function
10072 */
10073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010074f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075{
10076 colnr_T col = 0;
10077 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010078 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010080 fp = var2fpos(&argvars[0], FALSE, &fnum);
10081 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 {
10083 if (fp->col == MAXCOL)
10084 {
10085 /* '> can be MAXCOL, get the length of the line then */
10086 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010087 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 else
10089 col = MAXCOL;
10090 }
10091 else
10092 {
10093 col = fp->col + 1;
10094#ifdef FEAT_VIRTUALEDIT
10095 /* col(".") when the cursor is on the NUL at the end of the line
10096 * because of "coladd" can be seen as an extra column. */
10097 if (virtual_active() && fp == &curwin->w_cursor)
10098 {
10099 char_u *p = ml_get_cursor();
10100
10101 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10102 curwin->w_virtcol - curwin->w_cursor.coladd))
10103 {
10104# ifdef FEAT_MBYTE
10105 int l;
10106
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010107 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 col += l;
10109# else
10110 if (*p != NUL && p[1] == NUL)
10111 ++col;
10112# endif
10113 }
10114 }
10115#endif
10116 }
10117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010118 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119}
10120
Bram Moolenaar572cb562005-08-05 21:35:02 +000010121#if defined(FEAT_INS_EXPAND)
10122/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010123 * "complete()" function
10124 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010126f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010127{
10128 int startcol;
10129
10130 if ((State & INSERT) == 0)
10131 {
10132 EMSG(_("E785: complete() can only be used in Insert mode"));
10133 return;
10134 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010135
10136 /* Check for undo allowed here, because if something was already inserted
10137 * the line was already saved for undo and this check isn't done. */
10138 if (!undo_allowed())
10139 return;
10140
Bram Moolenaarade00832006-03-10 21:46:58 +000010141 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10142 {
10143 EMSG(_(e_invarg));
10144 return;
10145 }
10146
10147 startcol = get_tv_number_chk(&argvars[0], NULL);
10148 if (startcol <= 0)
10149 return;
10150
10151 set_completion(startcol - 1, argvars[1].vval.v_list);
10152}
10153
10154/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010155 * "complete_add()" function
10156 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010158f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010159{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010160 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010161}
10162
10163/*
10164 * "complete_check()" function
10165 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010167f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010168{
10169 int saved = RedrawingDisabled;
10170
10171 RedrawingDisabled = 0;
10172 ins_compl_check_keys(0);
10173 rettv->vval.v_number = compl_interrupted;
10174 RedrawingDisabled = saved;
10175}
10176#endif
10177
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178/*
10179 * "confirm(message, buttons[, default [, type]])" function
10180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010182f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183{
10184#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10185 char_u *message;
10186 char_u *buttons = NULL;
10187 char_u buf[NUMBUFLEN];
10188 char_u buf2[NUMBUFLEN];
10189 int def = 1;
10190 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 char_u *typestr;
10192 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 message = get_tv_string_chk(&argvars[0]);
10195 if (message == NULL)
10196 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010197 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010199 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10200 if (buttons == NULL)
10201 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010202 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010204 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010205 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10208 if (typestr == NULL)
10209 error = TRUE;
10210 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 switch (TOUPPER_ASC(*typestr))
10213 {
10214 case 'E': type = VIM_ERROR; break;
10215 case 'Q': type = VIM_QUESTION; break;
10216 case 'I': type = VIM_INFO; break;
10217 case 'W': type = VIM_WARNING; break;
10218 case 'G': type = VIM_GENERIC; break;
10219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 }
10221 }
10222 }
10223 }
10224
10225 if (buttons == NULL || *buttons == NUL)
10226 buttons = (char_u *)_("&Ok");
10227
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010228 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010230 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231#endif
10232}
10233
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010234/*
10235 * "copy()" function
10236 */
10237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010238f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010239{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010240 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010241}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010243#ifdef FEAT_FLOAT
10244/*
10245 * "cos()" function
10246 */
10247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010248f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010249{
10250 float_T f;
10251
10252 rettv->v_type = VAR_FLOAT;
10253 if (get_float_arg(argvars, &f) == OK)
10254 rettv->vval.v_float = cos(f);
10255 else
10256 rettv->vval.v_float = 0.0;
10257}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010258
10259/*
10260 * "cosh()" function
10261 */
10262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010263f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010264{
10265 float_T f;
10266
10267 rettv->v_type = VAR_FLOAT;
10268 if (get_float_arg(argvars, &f) == OK)
10269 rettv->vval.v_float = cosh(f);
10270 else
10271 rettv->vval.v_float = 0.0;
10272}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010273#endif
10274
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276 * "count()" function
10277 */
10278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010279f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010280{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010281 long n = 0;
10282 int ic = FALSE;
10283
Bram Moolenaare9a41262005-01-15 22:18:47 +000010284 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010285 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010286 listitem_T *li;
10287 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010288 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010289
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290 if ((l = argvars[0].vval.v_list) != NULL)
10291 {
10292 li = l->lv_first;
10293 if (argvars[2].v_type != VAR_UNKNOWN)
10294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010295 int error = FALSE;
10296
10297 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010298 if (argvars[3].v_type != VAR_UNKNOWN)
10299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 idx = get_tv_number_chk(&argvars[3], &error);
10301 if (!error)
10302 {
10303 li = list_find(l, idx);
10304 if (li == NULL)
10305 EMSGN(_(e_listidx), idx);
10306 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 if (error)
10309 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010310 }
10311
10312 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010313 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 ++n;
10315 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010317 else if (argvars[0].v_type == VAR_DICT)
10318 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010319 int todo;
10320 dict_T *d;
10321 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010322
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010323 if ((d = argvars[0].vval.v_dict) != NULL)
10324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010325 int error = FALSE;
10326
Bram Moolenaare9a41262005-01-15 22:18:47 +000010327 if (argvars[2].v_type != VAR_UNKNOWN)
10328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 if (argvars[3].v_type != VAR_UNKNOWN)
10331 EMSG(_(e_invarg));
10332 }
10333
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010334 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010335 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010336 {
10337 if (!HASHITEM_EMPTY(hi))
10338 {
10339 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010340 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010341 ++n;
10342 }
10343 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010344 }
10345 }
10346 else
10347 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010348 rettv->vval.v_number = n;
10349}
10350
10351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10353 *
10354 * Checks the existence of a cscope connection.
10355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010357f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
10359#ifdef FEAT_CSCOPE
10360 int num = 0;
10361 char_u *dbpath = NULL;
10362 char_u *prepend = NULL;
10363 char_u buf[NUMBUFLEN];
10364
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010365 if (argvars[0].v_type != VAR_UNKNOWN
10366 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368 num = (int)get_tv_number(&argvars[0]);
10369 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010370 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010371 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 }
10373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010374 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375#endif
10376}
10377
10378/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010379 * "cursor(lnum, col)" function, or
10380 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010382 * Moves the cursor to the specified line and column.
10383 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010386f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387{
10388 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010389#ifdef FEAT_VIRTUALEDIT
10390 long coladd = 0;
10391#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010392 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010394 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010395 if (argvars[1].v_type == VAR_UNKNOWN)
10396 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010397 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010398 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010399
Bram Moolenaar493c1782014-05-28 14:34:46 +020010400 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010401 {
10402 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010403 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010404 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010405 line = pos.lnum;
10406 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010407#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010408 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010409#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010410 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010411 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010412 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010413 set_curswant = FALSE;
10414 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010415 }
10416 else
10417 {
10418 line = get_tv_lnum(argvars);
10419 col = get_tv_number_chk(&argvars[1], NULL);
10420#ifdef FEAT_VIRTUALEDIT
10421 if (argvars[2].v_type != VAR_UNKNOWN)
10422 coladd = get_tv_number_chk(&argvars[2], NULL);
10423#endif
10424 }
10425 if (line < 0 || col < 0
10426#ifdef FEAT_VIRTUALEDIT
10427 || coladd < 0
10428#endif
10429 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 if (line > 0)
10432 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 if (col > 0)
10434 curwin->w_cursor.col = col - 1;
10435#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010436 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437#endif
10438
10439 /* Make sure the cursor is in a valid position. */
10440 check_cursor();
10441#ifdef FEAT_MBYTE
10442 /* Correct cursor for multi-byte character. */
10443 if (has_mbyte)
10444 mb_adjust_cursor();
10445#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010446
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010447 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010448 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449}
10450
10451/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010452 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 */
10454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010455f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010457 int noref = 0;
10458
10459 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010461 if (noref < 0 || noref > 1)
10462 EMSG(_(e_invarg));
10463 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010464 {
10465 current_copyID += COPYID_INC;
10466 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468}
10469
10470/*
10471 * "delete()" function
10472 */
10473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010474f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010476 char_u nbuf[NUMBUFLEN];
10477 char_u *name;
10478 char_u *flags;
10479
10480 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010482 return;
10483
10484 name = get_tv_string(&argvars[0]);
10485 if (name == NULL || *name == NUL)
10486 {
10487 EMSG(_(e_invarg));
10488 return;
10489 }
10490
10491 if (argvars[1].v_type != VAR_UNKNOWN)
10492 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010494 flags = (char_u *)"";
10495
10496 if (*flags == NUL)
10497 /* delete a file */
10498 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10499 else if (STRCMP(flags, "d") == 0)
10500 /* delete an empty directory */
10501 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10502 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010503 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010504 rettv->vval.v_number = delete_recursive(name);
10505 else
10506 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507}
10508
10509/*
10510 * "did_filetype()" function
10511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010513f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
10515#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517#endif
10518}
10519
10520/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010521 * "diff_filler()" function
10522 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010524f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010525{
10526#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010527 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010528#endif
10529}
10530
10531/*
10532 * "diff_hlID()" function
10533 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010535f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010536{
10537#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010539 static linenr_T prev_lnum = 0;
10540 static int changedtick = 0;
10541 static int fnum = 0;
10542 static int change_start = 0;
10543 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010544 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010545 int filler_lines;
10546 int col;
10547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010548 if (lnum < 0) /* ignore type error in {lnum} arg */
10549 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010550 if (lnum != prev_lnum
10551 || changedtick != curbuf->b_changedtick
10552 || fnum != curbuf->b_fnum)
10553 {
10554 /* New line, buffer, change: need to get the values. */
10555 filler_lines = diff_check(curwin, lnum);
10556 if (filler_lines < 0)
10557 {
10558 if (filler_lines == -1)
10559 {
10560 change_start = MAXCOL;
10561 change_end = -1;
10562 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10563 hlID = HLF_ADD; /* added line */
10564 else
10565 hlID = HLF_CHD; /* changed line */
10566 }
10567 else
10568 hlID = HLF_ADD; /* added line */
10569 }
10570 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010571 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010572 prev_lnum = lnum;
10573 changedtick = curbuf->b_changedtick;
10574 fnum = curbuf->b_fnum;
10575 }
10576
10577 if (hlID == HLF_CHD || hlID == HLF_TXD)
10578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010580 if (col >= change_start && col <= change_end)
10581 hlID = HLF_TXD; /* changed text */
10582 else
10583 hlID = HLF_CHD; /* changed line */
10584 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010585 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010586#endif
10587}
10588
10589/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010590 * "empty({expr})" function
10591 */
10592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010593f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010594{
10595 int n;
10596
10597 switch (argvars[0].v_type)
10598 {
10599 case VAR_STRING:
10600 case VAR_FUNC:
10601 n = argvars[0].vval.v_string == NULL
10602 || *argvars[0].vval.v_string == NUL;
10603 break;
10604 case VAR_NUMBER:
10605 n = argvars[0].vval.v_number == 0;
10606 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010607 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010608#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010609 n = argvars[0].vval.v_float == 0.0;
10610 break;
10611#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010612 case VAR_LIST:
10613 n = argvars[0].vval.v_list == NULL
10614 || argvars[0].vval.v_list->lv_first == NULL;
10615 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 case VAR_DICT:
10617 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010618 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010619 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010620 case VAR_SPECIAL:
10621 n = argvars[0].vval.v_number != VVAL_TRUE;
10622 break;
10623
Bram Moolenaar835dc632016-02-07 14:27:38 +010010624 case VAR_JOB:
10625#ifdef FEAT_JOB
10626 n = argvars[0].vval.v_job->jv_status != JOB_STARTED;
10627 break;
10628#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010629 case VAR_UNKNOWN:
10630 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10631 n = TRUE;
10632 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010633 }
10634
10635 rettv->vval.v_number = n;
10636}
10637
10638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 * "escape({string}, {chars})" function
10640 */
10641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010642f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643{
10644 char_u buf[NUMBUFLEN];
10645
Bram Moolenaar758711c2005-02-02 23:11:38 +000010646 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10647 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649}
10650
10651/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010652 * "eval()" function
10653 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010655f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010656{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010657 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010659 s = get_tv_string_chk(&argvars[0]);
10660 if (s != NULL)
10661 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010662
Bram Moolenaar615b9972015-01-14 17:15:05 +010010663 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010664 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10665 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010666 if (p != NULL && !aborting())
10667 EMSG2(_(e_invexpr2), p);
10668 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010670 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010671 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010672 else if (*s != NUL)
10673 EMSG(_(e_trailing));
10674}
10675
10676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 * "eventhandler()" function
10678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010680f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010682 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683}
10684
10685/*
10686 * "executable()" function
10687 */
10688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010689f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010691 char_u *name = get_tv_string(&argvars[0]);
10692
10693 /* Check in $PATH and also check directly if there is a directory name. */
10694 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10695 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010696}
10697
10698/*
10699 * "exepath()" function
10700 */
10701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010702f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010703{
10704 char_u *p = NULL;
10705
Bram Moolenaarb5971142015-03-21 17:32:19 +010010706 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010707 rettv->v_type = VAR_STRING;
10708 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709}
10710
10711/*
10712 * "exists()" function
10713 */
10714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010715f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716{
10717 char_u *p;
10718 char_u *name;
10719 int n = FALSE;
10720 int len = 0;
10721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 if (*p == '$') /* environment variable */
10724 {
10725 /* first try "normal" environment variables (fast) */
10726 if (mch_getenv(p + 1) != NULL)
10727 n = TRUE;
10728 else
10729 {
10730 /* try expanding things like $VIM and ${HOME} */
10731 p = expand_env_save(p);
10732 if (p != NULL && *p != '$')
10733 n = TRUE;
10734 vim_free(p);
10735 }
10736 }
10737 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010740 if (*skipwhite(p) != NUL)
10741 n = FALSE; /* trailing garbage */
10742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 else if (*p == '*') /* internal or user defined function */
10744 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010745 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 }
10747 else if (*p == ':')
10748 {
10749 n = cmd_exists(p + 1);
10750 }
10751 else if (*p == '#')
10752 {
10753#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010754 if (p[1] == '#')
10755 n = autocmd_supported(p + 2);
10756 else
10757 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758#endif
10759 }
10760 else /* internal variable */
10761 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010762 char_u *tofree;
10763 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010765 /* get_name_len() takes care of expanding curly braces */
10766 name = p;
10767 len = get_name_len(&p, &tofree, TRUE, FALSE);
10768 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010770 if (tofree != NULL)
10771 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010772 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010773 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010775 /* handle d.key, l[idx], f(expr) */
10776 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10777 if (n)
10778 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 }
10780 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010781 if (*p != NUL)
10782 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010784 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 }
10786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788}
10789
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010790#ifdef FEAT_FLOAT
10791/*
10792 * "exp()" function
10793 */
10794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010795f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010796{
10797 float_T f;
10798
10799 rettv->v_type = VAR_FLOAT;
10800 if (get_float_arg(argvars, &f) == OK)
10801 rettv->vval.v_float = exp(f);
10802 else
10803 rettv->vval.v_float = 0.0;
10804}
10805#endif
10806
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807/*
10808 * "expand()" function
10809 */
10810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010811f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812{
10813 char_u *s;
10814 int len;
10815 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010816 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010818 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010819 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010822 if (argvars[1].v_type != VAR_UNKNOWN
10823 && argvars[2].v_type != VAR_UNKNOWN
10824 && get_tv_number_chk(&argvars[2], &error)
10825 && !error)
10826 {
10827 rettv->v_type = VAR_LIST;
10828 rettv->vval.v_list = NULL;
10829 }
10830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 if (*s == '%' || *s == '#' || *s == '<')
10833 {
10834 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010835 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010837 if (rettv->v_type == VAR_LIST)
10838 {
10839 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10840 list_append_string(rettv->vval.v_list, result, -1);
10841 else
10842 vim_free(result);
10843 }
10844 else
10845 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 }
10847 else
10848 {
10849 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010850 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 if (argvars[1].v_type != VAR_UNKNOWN
10852 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010853 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010854 if (!error)
10855 {
10856 ExpandInit(&xpc);
10857 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010858 if (p_wic)
10859 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010860 if (rettv->v_type == VAR_STRING)
10861 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10862 options, WILD_ALL);
10863 else if (rettv_list_alloc(rettv) != FAIL)
10864 {
10865 int i;
10866
10867 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10868 for (i = 0; i < xpc.xp_numfiles; i++)
10869 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10870 ExpandCleanup(&xpc);
10871 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 }
10873 else
10874 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 }
10876}
10877
10878/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010879 * Go over all entries in "d2" and add them to "d1".
10880 * When "action" is "error" then a duplicate key is an error.
10881 * When "action" is "force" then a duplicate key is overwritten.
10882 * Otherwise duplicate keys are ignored ("action" is "keep").
10883 */
10884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010885dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010886{
10887 dictitem_T *di1;
10888 hashitem_T *hi2;
10889 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010890 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010891
10892 todo = (int)d2->dv_hashtab.ht_used;
10893 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10894 {
10895 if (!HASHITEM_EMPTY(hi2))
10896 {
10897 --todo;
10898 di1 = dict_find(d1, hi2->hi_key, -1);
10899 if (d1->dv_scope != 0)
10900 {
10901 /* Disallow replacing a builtin function in l: and g:.
10902 * Check the key to be valid when adding to any
10903 * scope. */
10904 if (d1->dv_scope == VAR_DEF_SCOPE
10905 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10906 && var_check_func_name(hi2->hi_key,
10907 di1 == NULL))
10908 break;
10909 if (!valid_varname(hi2->hi_key))
10910 break;
10911 }
10912 if (di1 == NULL)
10913 {
10914 di1 = dictitem_copy(HI2DI(hi2));
10915 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10916 dictitem_free(di1);
10917 }
10918 else if (*action == 'e')
10919 {
10920 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10921 break;
10922 }
10923 else if (*action == 'f' && HI2DI(hi2) != di1)
10924 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010925 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10926 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010927 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010928 clear_tv(&di1->di_tv);
10929 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10930 }
10931 }
10932 }
10933}
10934
10935/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010936 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010938 */
10939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010940f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010941{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010942 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010943
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010945 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010946 list_T *l1, *l2;
10947 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010948 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010949 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010950
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951 l1 = argvars[0].vval.v_list;
10952 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010953 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010954 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010955 {
10956 if (argvars[2].v_type != VAR_UNKNOWN)
10957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 before = get_tv_number_chk(&argvars[2], &error);
10959 if (error)
10960 return; /* type error; errmsg already given */
10961
Bram Moolenaar758711c2005-02-02 23:11:38 +000010962 if (before == l1->lv_len)
10963 item = NULL;
10964 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010965 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010966 item = list_find(l1, before);
10967 if (item == NULL)
10968 {
10969 EMSGN(_(e_listidx), before);
10970 return;
10971 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010972 }
10973 }
10974 else
10975 item = NULL;
10976 list_extend(l1, l2, item);
10977
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 copy_tv(&argvars[0], rettv);
10979 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010980 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010981 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10982 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010983 dict_T *d1, *d2;
10984 char_u *action;
10985 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010986
10987 d1 = argvars[0].vval.v_dict;
10988 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010989 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010990 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010991 {
10992 /* Check the third argument. */
10993 if (argvars[2].v_type != VAR_UNKNOWN)
10994 {
10995 static char *(av[]) = {"keep", "force", "error"};
10996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010997 action = get_tv_string_chk(&argvars[2]);
10998 if (action == NULL)
10999 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 for (i = 0; i < 3; ++i)
11001 if (STRCMP(action, av[i]) == 0)
11002 break;
11003 if (i == 3)
11004 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011005 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 return;
11007 }
11008 }
11009 else
11010 action = (char_u *)"force";
11011
Bram Moolenaara9922d62013-05-30 13:01:18 +020011012 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011013
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 copy_tv(&argvars[0], rettv);
11015 }
11016 }
11017 else
11018 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011019}
11020
11021/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011022 * "feedkeys()" function
11023 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011025f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011026{
11027 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011028 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011029 char_u *keys, *flags;
11030 char_u nbuf[NUMBUFLEN];
11031 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011032 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011033 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011034
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011035 /* This is not allowed in the sandbox. If the commands would still be
11036 * executed in the sandbox it would be OK, but it probably happens later,
11037 * when "sandbox" is no longer set. */
11038 if (check_secure())
11039 return;
11040
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011041 keys = get_tv_string(&argvars[0]);
11042 if (*keys != NUL)
11043 {
11044 if (argvars[1].v_type != VAR_UNKNOWN)
11045 {
11046 flags = get_tv_string_buf(&argvars[1], nbuf);
11047 for ( ; *flags != NUL; ++flags)
11048 {
11049 switch (*flags)
11050 {
11051 case 'n': remap = FALSE; break;
11052 case 'm': remap = TRUE; break;
11053 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011054 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011055 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011056 }
11057 }
11058 }
11059
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011060 /* Need to escape K_SPECIAL and CSI before putting the string in the
11061 * typeahead buffer. */
11062 keys_esc = vim_strsave_escape_csi(keys);
11063 if (keys_esc != NULL)
11064 {
11065 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011066 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011067 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011068 if (vgetc_busy)
11069 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011070 if (execute)
11071 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011072 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011073 }
11074}
11075
11076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 * "filereadable()" function
11078 */
11079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011080f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011082 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 char_u *p;
11084 int n;
11085
Bram Moolenaarc236c162008-07-13 17:41:49 +000011086#ifndef O_NONBLOCK
11087# define O_NONBLOCK 0
11088#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011090 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11091 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 {
11093 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011094 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 }
11096 else
11097 n = FALSE;
11098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100}
11101
11102/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011103 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 * rights to write into.
11105 */
11106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011107f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011109 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110}
11111
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011112 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011113findfilendir(
11114 typval_T *argvars UNUSED,
11115 typval_T *rettv,
11116 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011117{
11118#ifdef FEAT_SEARCHPATH
11119 char_u *fname;
11120 char_u *fresult = NULL;
11121 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11122 char_u *p;
11123 char_u pathbuf[NUMBUFLEN];
11124 int count = 1;
11125 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011126 int error = FALSE;
11127#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011128
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011129 rettv->vval.v_string = NULL;
11130 rettv->v_type = VAR_STRING;
11131
11132#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011134
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011135 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011137 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11138 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011139 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 else
11141 {
11142 if (*p != NUL)
11143 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011146 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011147 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011148 }
11149
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011150 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11151 error = TRUE;
11152
11153 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 do
11156 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011157 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011158 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011159 fresult = find_file_in_path_option(first ? fname : NULL,
11160 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011161 0, first, path,
11162 find_what,
11163 curbuf->b_ffname,
11164 find_what == FINDFILE_DIR
11165 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011167
11168 if (fresult != NULL && rettv->v_type == VAR_LIST)
11169 list_append_string(rettv->vval.v_list, fresult, -1);
11170
11171 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011173
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011174 if (rettv->v_type == VAR_STRING)
11175 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011176#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011177}
11178
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011179static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11180static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011181
11182/*
11183 * Implementation of map() and filter().
11184 */
11185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011186filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011187{
11188 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011189 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 listitem_T *li, *nli;
11191 list_T *l = NULL;
11192 dictitem_T *di;
11193 hashtab_T *ht;
11194 hashitem_T *hi;
11195 dict_T *d = NULL;
11196 typval_T save_val;
11197 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011198 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011199 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011200 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011201 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011202 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011203 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011204 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011205
Bram Moolenaare9a41262005-01-15 22:18:47 +000011206 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011207 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011208 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011209 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011210 return;
11211 }
11212 else if (argvars[0].v_type == VAR_DICT)
11213 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011214 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011215 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011216 return;
11217 }
11218 else
11219 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011220 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011221 return;
11222 }
11223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 expr = get_tv_string_buf_chk(&argvars[1], buf);
11225 /* On type errors, the preceding call has already displayed an error
11226 * message. Avoid a misleading error message for an empty string that
11227 * was not passed as argument. */
11228 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 prepare_vimvar(VV_VAL, &save_val);
11231 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011232
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011233 /* We reset "did_emsg" to be able to detect whether an error
11234 * occurred during evaluation of the expression. */
11235 save_did_emsg = did_emsg;
11236 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011237
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011238 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011239 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 vimvars[VV_KEY].vv_type = VAR_STRING;
11242
11243 ht = &d->dv_hashtab;
11244 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011245 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011246 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011248 if (!HASHITEM_EMPTY(hi))
11249 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011250 int r;
11251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011252 --todo;
11253 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011254 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011255 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11256 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 break;
11258 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011259 r = filter_map_one(&di->di_tv, expr, map, &rem);
11260 clear_tv(&vimvars[VV_KEY].vv_tv);
11261 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011262 break;
11263 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011264 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011265 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11266 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011267 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011268 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011269 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 }
11271 }
11272 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011273 }
11274 else
11275 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011276 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011278 for (li = l->lv_first; li != NULL; li = nli)
11279 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011280 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011281 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011282 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011283 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011284 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011285 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011286 break;
11287 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011288 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011289 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011290 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011291 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011292
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011293 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011294 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011295
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011296 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011297 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011298
11299 copy_tv(&argvars[0], rettv);
11300}
11301
11302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011303filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011304{
Bram Moolenaar33570922005-01-25 22:26:29 +000011305 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011306 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011307 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011308
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011310 s = expr;
11311 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011312 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011313 if (*s != NUL) /* check for trailing chars after expr */
11314 {
11315 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011316 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011317 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011318 }
11319 if (map)
11320 {
11321 /* map(): replace the list item value */
11322 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011323 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011324 *tv = rettv;
11325 }
11326 else
11327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328 int error = FALSE;
11329
Bram Moolenaare9a41262005-01-15 22:18:47 +000011330 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011332 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011333 /* On type error, nothing has been removed; return FAIL to stop the
11334 * loop. The error message was given by get_tv_number_chk(). */
11335 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011336 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011337 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011338 retval = OK;
11339theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011340 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011341 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011342}
11343
11344/*
11345 * "filter()" function
11346 */
11347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011348f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011349{
11350 filter_map(argvars, rettv, FALSE);
11351}
11352
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011354 * "finddir({fname}[, {path}[, {count}]])" function
11355 */
11356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011357f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011358{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011359 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011360}
11361
11362/*
11363 * "findfile({fname}[, {path}[, {count}]])" function
11364 */
11365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011366f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011367{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011368 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011369}
11370
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011371#ifdef FEAT_FLOAT
11372/*
11373 * "float2nr({float})" function
11374 */
11375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011376f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011377{
11378 float_T f;
11379
11380 if (get_float_arg(argvars, &f) == OK)
11381 {
11382 if (f < -0x7fffffff)
11383 rettv->vval.v_number = -0x7fffffff;
11384 else if (f > 0x7fffffff)
11385 rettv->vval.v_number = 0x7fffffff;
11386 else
11387 rettv->vval.v_number = (varnumber_T)f;
11388 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011389}
11390
11391/*
11392 * "floor({float})" function
11393 */
11394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011395f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011396{
11397 float_T f;
11398
11399 rettv->v_type = VAR_FLOAT;
11400 if (get_float_arg(argvars, &f) == OK)
11401 rettv->vval.v_float = floor(f);
11402 else
11403 rettv->vval.v_float = 0.0;
11404}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011405
11406/*
11407 * "fmod()" function
11408 */
11409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011410f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011411{
11412 float_T fx, fy;
11413
11414 rettv->v_type = VAR_FLOAT;
11415 if (get_float_arg(argvars, &fx) == OK
11416 && get_float_arg(&argvars[1], &fy) == OK)
11417 rettv->vval.v_float = fmod(fx, fy);
11418 else
11419 rettv->vval.v_float = 0.0;
11420}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011421#endif
11422
Bram Moolenaar0d660222005-01-07 21:51:51 +000011423/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011424 * "fnameescape({string})" function
11425 */
11426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011427f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011428{
11429 rettv->vval.v_string = vim_strsave_fnameescape(
11430 get_tv_string(&argvars[0]), FALSE);
11431 rettv->v_type = VAR_STRING;
11432}
11433
11434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 * "fnamemodify({fname}, {mods})" function
11436 */
11437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011438f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
11440 char_u *fname;
11441 char_u *mods;
11442 int usedlen = 0;
11443 int len;
11444 char_u *fbuf = NULL;
11445 char_u buf[NUMBUFLEN];
11446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 fname = get_tv_string_chk(&argvars[0]);
11448 mods = get_tv_string_buf_chk(&argvars[1], buf);
11449 if (fname == NULL || mods == NULL)
11450 fname = NULL;
11451 else
11452 {
11453 len = (int)STRLEN(fname);
11454 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 vim_free(fbuf);
11463}
11464
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011465static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466
11467/*
11468 * "foldclosed()" function
11469 */
11470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011471foldclosed_both(
11472 typval_T *argvars UNUSED,
11473 typval_T *rettv,
11474 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475{
11476#ifdef FEAT_FOLDING
11477 linenr_T lnum;
11478 linenr_T first, last;
11479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11482 {
11483 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11484 {
11485 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 return;
11490 }
11491 }
11492#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494}
11495
11496/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497 * "foldclosed()" function
11498 */
11499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011500f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501{
11502 foldclosed_both(argvars, rettv, FALSE);
11503}
11504
11505/*
11506 * "foldclosedend()" function
11507 */
11508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011509f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510{
11511 foldclosed_both(argvars, rettv, TRUE);
11512}
11513
11514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 * "foldlevel()" function
11516 */
11517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011518f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519{
11520#ifdef FEAT_FOLDING
11521 linenr_T lnum;
11522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527}
11528
11529/*
11530 * "foldtext()" function
11531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011533f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534{
11535#ifdef FEAT_FOLDING
11536 linenr_T lnum;
11537 char_u *s;
11538 char_u *r;
11539 int len;
11540 char *txt;
11541#endif
11542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011543 rettv->v_type = VAR_STRING;
11544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011546 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11547 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11548 <= curbuf->b_ml.ml_line_count
11549 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 {
11551 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011552 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11553 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 {
11555 if (!linewhite(lnum))
11556 break;
11557 ++lnum;
11558 }
11559
11560 /* Find interesting text in this line. */
11561 s = skipwhite(ml_get(lnum));
11562 /* skip C comment-start */
11563 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011566 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011567 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011568 {
11569 s = skipwhite(ml_get(lnum + 1));
11570 if (*s == '*')
11571 s = skipwhite(s + 1);
11572 }
11573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 txt = _("+-%s%3ld lines: ");
11575 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011576 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 + 20 /* for %3ld */
11578 + STRLEN(s))); /* concatenated */
11579 if (r != NULL)
11580 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011581 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11582 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11583 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 len = (int)STRLEN(r);
11585 STRCAT(r, s);
11586 /* remove 'foldmarker' and 'commentstring' */
11587 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 }
11590 }
11591#endif
11592}
11593
11594/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011595 * "foldtextresult(lnum)" function
11596 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011598f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011599{
11600#ifdef FEAT_FOLDING
11601 linenr_T lnum;
11602 char_u *text;
11603 char_u buf[51];
11604 foldinfo_T foldinfo;
11605 int fold_count;
11606#endif
11607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011608 rettv->v_type = VAR_STRING;
11609 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011610#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 /* treat illegal types and illegal string values for {lnum} the same */
11613 if (lnum < 0)
11614 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011615 fold_count = foldedCount(curwin, lnum, &foldinfo);
11616 if (fold_count > 0)
11617 {
11618 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11619 &foldinfo, buf);
11620 if (text == buf)
11621 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011623 }
11624#endif
11625}
11626
11627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 * "foreground()" function
11629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011631f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633#ifdef FEAT_GUI
11634 if (gui.in_use)
11635 gui_mch_set_foreground();
11636#else
11637# ifdef WIN32
11638 win32_set_foreground();
11639# endif
11640#endif
11641}
11642
11643/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011644 * "function()" function
11645 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011647f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011648{
11649 char_u *s;
11650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011652 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011653 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011654 /* Don't check an autoload name for existence here. */
11655 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011656 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011657 else
11658 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011659 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011660 {
11661 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011662 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011663
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011664 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11665 * also be called from another script. Using trans_function_name()
11666 * would also work, but some plugins depend on the name being
11667 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011668 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011669 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011670 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011671 if (rettv->vval.v_string != NULL)
11672 {
11673 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011674 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011675 }
11676 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011677 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011678 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011680 }
11681}
11682
11683/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011684 * "garbagecollect()" function
11685 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011687f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011688{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011689 /* This is postponed until we are back at the toplevel, because we may be
11690 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11691 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011692
11693 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11694 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011695}
11696
11697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011698 * "get()" function
11699 */
11700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011701f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011702{
Bram Moolenaar33570922005-01-25 22:26:29 +000011703 listitem_T *li;
11704 list_T *l;
11705 dictitem_T *di;
11706 dict_T *d;
11707 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011708
Bram Moolenaare9a41262005-01-15 22:18:47 +000011709 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011710 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011711 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011713 int error = FALSE;
11714
11715 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11716 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011717 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011718 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011719 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011720 else if (argvars[0].v_type == VAR_DICT)
11721 {
11722 if ((d = argvars[0].vval.v_dict) != NULL)
11723 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011724 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011725 if (di != NULL)
11726 tv = &di->di_tv;
11727 }
11728 }
11729 else
11730 EMSG2(_(e_listdictarg), "get()");
11731
11732 if (tv == NULL)
11733 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011734 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011735 copy_tv(&argvars[2], rettv);
11736 }
11737 else
11738 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011739}
11740
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011741static 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 +000011742
11743/*
11744 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011745 * Return a range (from start to end) of lines in rettv from the specified
11746 * buffer.
11747 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011748 */
11749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011750get_buffer_lines(
11751 buf_T *buf,
11752 linenr_T start,
11753 linenr_T end,
11754 int retlist,
11755 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011756{
11757 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011758
Bram Moolenaar959a1432013-12-14 12:17:38 +010011759 rettv->v_type = VAR_STRING;
11760 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011761 if (retlist && rettv_list_alloc(rettv) == FAIL)
11762 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011763
11764 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11765 return;
11766
11767 if (!retlist)
11768 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011769 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11770 p = ml_get_buf(buf, start, FALSE);
11771 else
11772 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011773 rettv->vval.v_string = vim_strsave(p);
11774 }
11775 else
11776 {
11777 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011778 return;
11779
11780 if (start < 1)
11781 start = 1;
11782 if (end > buf->b_ml.ml_line_count)
11783 end = buf->b_ml.ml_line_count;
11784 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011785 if (list_append_string(rettv->vval.v_list,
11786 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011787 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011788 }
11789}
11790
11791/*
11792 * "getbufline()" function
11793 */
11794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011795f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011796{
11797 linenr_T lnum;
11798 linenr_T end;
11799 buf_T *buf;
11800
11801 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11802 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011803 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011804 --emsg_off;
11805
Bram Moolenaar661b1822005-07-28 22:36:45 +000011806 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011807 if (argvars[2].v_type == VAR_UNKNOWN)
11808 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011809 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011810 end = get_tv_lnum_buf(&argvars[2], buf);
11811
Bram Moolenaar342337a2005-07-21 21:11:17 +000011812 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011813}
11814
Bram Moolenaar0d660222005-01-07 21:51:51 +000011815/*
11816 * "getbufvar()" function
11817 */
11818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011819f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011820{
11821 buf_T *buf;
11822 buf_T *save_curbuf;
11823 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011824 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011825 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011827 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11828 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011829 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011830 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011831
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011832 rettv->v_type = VAR_STRING;
11833 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011834
11835 if (buf != NULL && varname != NULL)
11836 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011837 /* set curbuf to be our buf, temporarily */
11838 save_curbuf = curbuf;
11839 curbuf = buf;
11840
Bram Moolenaar0d660222005-01-07 21:51:51 +000011841 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011842 {
11843 if (get_option_tv(&varname, rettv, TRUE) == OK)
11844 done = TRUE;
11845 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011846 else if (STRCMP(varname, "changedtick") == 0)
11847 {
11848 rettv->v_type = VAR_NUMBER;
11849 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011850 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011851 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011852 else
11853 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011854 /* Look up the variable. */
11855 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11856 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11857 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011858 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011859 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011860 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011861 done = TRUE;
11862 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011863 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011864
11865 /* restore previous notion of curbuf */
11866 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011867 }
11868
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011869 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11870 /* use the default value */
11871 copy_tv(&argvars[2], rettv);
11872
Bram Moolenaar0d660222005-01-07 21:51:51 +000011873 --emsg_off;
11874}
11875
11876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 * "getchar()" function
11878 */
11879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011880f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881{
11882 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011883 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011885 /* Position the cursor. Needed after a message that ends in a space. */
11886 windgoto(msg_row, msg_col);
11887
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 ++no_mapping;
11889 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011890 for (;;)
11891 {
11892 if (argvars[0].v_type == VAR_UNKNOWN)
11893 /* getchar(): blocking wait. */
11894 n = safe_vgetc();
11895 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11896 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011897 n = vpeekc_any();
11898 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011899 /* illegal argument or getchar(0) and no char avail: return zero */
11900 n = 0;
11901 else
11902 /* getchar(0) and char avail: return char */
11903 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011904
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011905 if (n == K_IGNORE)
11906 continue;
11907 break;
11908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 --no_mapping;
11910 --allow_keys;
11911
Bram Moolenaar219b8702006-11-01 14:32:36 +000011912 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11913 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11914 vimvars[VV_MOUSE_COL].vv_nr = 0;
11915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011916 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 if (IS_SPECIAL(n) || mod_mask != 0)
11918 {
11919 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11920 int i = 0;
11921
11922 /* Turn a special key into three bytes, plus modifier. */
11923 if (mod_mask != 0)
11924 {
11925 temp[i++] = K_SPECIAL;
11926 temp[i++] = KS_MODIFIER;
11927 temp[i++] = mod_mask;
11928 }
11929 if (IS_SPECIAL(n))
11930 {
11931 temp[i++] = K_SPECIAL;
11932 temp[i++] = K_SECOND(n);
11933 temp[i++] = K_THIRD(n);
11934 }
11935#ifdef FEAT_MBYTE
11936 else if (has_mbyte)
11937 i += (*mb_char2bytes)(n, temp + i);
11938#endif
11939 else
11940 temp[i++] = n;
11941 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011942 rettv->v_type = VAR_STRING;
11943 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011944
11945#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011946 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011947 {
11948 int row = mouse_row;
11949 int col = mouse_col;
11950 win_T *win;
11951 linenr_T lnum;
11952# ifdef FEAT_WINDOWS
11953 win_T *wp;
11954# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011955 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011956
11957 if (row >= 0 && col >= 0)
11958 {
11959 /* Find the window at the mouse coordinates and compute the
11960 * text position. */
11961 win = mouse_find_win(&row, &col);
11962 (void)mouse_comp_pos(win, &row, &col, &lnum);
11963# ifdef FEAT_WINDOWS
11964 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011965 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011966# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011967 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011968 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11969 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11970 }
11971 }
11972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 }
11974}
11975
11976/*
11977 * "getcharmod()" function
11978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011980f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983}
11984
11985/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011986 * "getcharsearch()" function
11987 */
11988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011989f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011990{
11991 if (rettv_dict_alloc(rettv) != FAIL)
11992 {
11993 dict_T *dict = rettv->vval.v_dict;
11994
11995 dict_add_nr_str(dict, "char", 0L, last_csearch());
11996 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11997 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11998 }
11999}
12000
12001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 * "getcmdline()" function
12003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012005f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 rettv->v_type = VAR_STRING;
12008 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009}
12010
12011/*
12012 * "getcmdpos()" function
12013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012015f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012017 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018}
12019
12020/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012021 * "getcmdtype()" function
12022 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012024f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012025{
12026 rettv->v_type = VAR_STRING;
12027 rettv->vval.v_string = alloc(2);
12028 if (rettv->vval.v_string != NULL)
12029 {
12030 rettv->vval.v_string[0] = get_cmdline_type();
12031 rettv->vval.v_string[1] = NUL;
12032 }
12033}
12034
12035/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012036 * "getcmdwintype()" function
12037 */
12038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012039f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012040{
12041 rettv->v_type = VAR_STRING;
12042 rettv->vval.v_string = NULL;
12043#ifdef FEAT_CMDWIN
12044 rettv->vval.v_string = alloc(2);
12045 if (rettv->vval.v_string != NULL)
12046 {
12047 rettv->vval.v_string[0] = cmdwin_type;
12048 rettv->vval.v_string[1] = NUL;
12049 }
12050#endif
12051}
12052
12053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 * "getcwd()" function
12055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012057f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012059 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012060 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012063 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012064
12065 wp = find_tabwin(&argvars[0], &argvars[1]);
12066 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012068 if (wp->w_localdir != NULL)
12069 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12070 else if(globaldir != NULL)
12071 rettv->vval.v_string = vim_strsave(globaldir);
12072 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012073 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012074 cwd = alloc(MAXPATHL);
12075 if (cwd != NULL)
12076 {
12077 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12078 rettv->vval.v_string = vim_strsave(cwd);
12079 vim_free(cwd);
12080 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012081 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012082#ifdef BACKSLASH_IN_FILENAME
12083 if (rettv->vval.v_string != NULL)
12084 slash_adjust(rettv->vval.v_string);
12085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 }
12087}
12088
12089/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012090 * "getfontname()" function
12091 */
12092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012093f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012094{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 rettv->v_type = VAR_STRING;
12096 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012097#ifdef FEAT_GUI
12098 if (gui.in_use)
12099 {
12100 GuiFont font;
12101 char_u *name = NULL;
12102
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012103 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012104 {
12105 /* Get the "Normal" font. Either the name saved by
12106 * hl_set_font_name() or from the font ID. */
12107 font = gui.norm_font;
12108 name = hl_get_font_name();
12109 }
12110 else
12111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012112 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012113 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12114 return;
12115 font = gui_mch_get_font(name, FALSE);
12116 if (font == NOFONT)
12117 return; /* Invalid font name, return empty string. */
12118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012120 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012121 gui_mch_free_font(font);
12122 }
12123#endif
12124}
12125
12126/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012127 * "getfperm({fname})" function
12128 */
12129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012130f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012131{
12132 char_u *fname;
12133 struct stat st;
12134 char_u *perm = NULL;
12135 char_u flags[] = "rwx";
12136 int i;
12137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012141 if (mch_stat((char *)fname, &st) >= 0)
12142 {
12143 perm = vim_strsave((char_u *)"---------");
12144 if (perm != NULL)
12145 {
12146 for (i = 0; i < 9; i++)
12147 {
12148 if (st.st_mode & (1 << (8 - i)))
12149 perm[i] = flags[i % 3];
12150 }
12151 }
12152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012154}
12155
12156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 * "getfsize({fname})" function
12158 */
12159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012160f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161{
12162 char_u *fname;
12163 struct stat st;
12164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012165 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168
12169 if (mch_stat((char *)fname, &st) >= 0)
12170 {
12171 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012175 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012176
12177 /* non-perfect check for overflow */
12178 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12179 rettv->vval.v_number = -2;
12180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 }
12182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184}
12185
12186/*
12187 * "getftime({fname})" function
12188 */
12189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012190f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191{
12192 char_u *fname;
12193 struct stat st;
12194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012195 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196
12197 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201}
12202
12203/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012204 * "getftype({fname})" function
12205 */
12206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012207f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012208{
12209 char_u *fname;
12210 struct stat st;
12211 char_u *type = NULL;
12212 char *t;
12213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012217 if (mch_lstat((char *)fname, &st) >= 0)
12218 {
12219#ifdef S_ISREG
12220 if (S_ISREG(st.st_mode))
12221 t = "file";
12222 else if (S_ISDIR(st.st_mode))
12223 t = "dir";
12224# ifdef S_ISLNK
12225 else if (S_ISLNK(st.st_mode))
12226 t = "link";
12227# endif
12228# ifdef S_ISBLK
12229 else if (S_ISBLK(st.st_mode))
12230 t = "bdev";
12231# endif
12232# ifdef S_ISCHR
12233 else if (S_ISCHR(st.st_mode))
12234 t = "cdev";
12235# endif
12236# ifdef S_ISFIFO
12237 else if (S_ISFIFO(st.st_mode))
12238 t = "fifo";
12239# endif
12240# ifdef S_ISSOCK
12241 else if (S_ISSOCK(st.st_mode))
12242 t = "fifo";
12243# endif
12244 else
12245 t = "other";
12246#else
12247# ifdef S_IFMT
12248 switch (st.st_mode & S_IFMT)
12249 {
12250 case S_IFREG: t = "file"; break;
12251 case S_IFDIR: t = "dir"; break;
12252# ifdef S_IFLNK
12253 case S_IFLNK: t = "link"; break;
12254# endif
12255# ifdef S_IFBLK
12256 case S_IFBLK: t = "bdev"; break;
12257# endif
12258# ifdef S_IFCHR
12259 case S_IFCHR: t = "cdev"; break;
12260# endif
12261# ifdef S_IFIFO
12262 case S_IFIFO: t = "fifo"; break;
12263# endif
12264# ifdef S_IFSOCK
12265 case S_IFSOCK: t = "socket"; break;
12266# endif
12267 default: t = "other";
12268 }
12269# else
12270 if (mch_isdir(fname))
12271 t = "dir";
12272 else
12273 t = "file";
12274# endif
12275#endif
12276 type = vim_strsave((char_u *)t);
12277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012279}
12280
12281/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012282 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012283 */
12284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012285f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012286{
12287 linenr_T lnum;
12288 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012289 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012290
12291 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012292 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012293 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012294 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012295 retlist = FALSE;
12296 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012297 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012298 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012299 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012300 retlist = TRUE;
12301 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012302
Bram Moolenaar342337a2005-07-21 21:11:17 +000012303 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012304}
12305
12306/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012307 * "getmatches()" function
12308 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012310f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012311{
12312#ifdef FEAT_SEARCH_EXTRA
12313 dict_T *dict;
12314 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012315 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012316
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012317 if (rettv_list_alloc(rettv) == OK)
12318 {
12319 while (cur != NULL)
12320 {
12321 dict = dict_alloc();
12322 if (dict == NULL)
12323 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012324 if (cur->match.regprog == NULL)
12325 {
12326 /* match added with matchaddpos() */
12327 for (i = 0; i < MAXPOSMATCH; ++i)
12328 {
12329 llpos_T *llpos;
12330 char buf[6];
12331 list_T *l;
12332
12333 llpos = &cur->pos.pos[i];
12334 if (llpos->lnum == 0)
12335 break;
12336 l = list_alloc();
12337 if (l == NULL)
12338 break;
12339 list_append_number(l, (varnumber_T)llpos->lnum);
12340 if (llpos->col > 0)
12341 {
12342 list_append_number(l, (varnumber_T)llpos->col);
12343 list_append_number(l, (varnumber_T)llpos->len);
12344 }
12345 sprintf(buf, "pos%d", i + 1);
12346 dict_add_list(dict, buf, l);
12347 }
12348 }
12349 else
12350 {
12351 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12352 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012353 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012354 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12355 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012356# ifdef FEAT_CONCEAL
12357 if (cur->conceal_char)
12358 {
12359 char_u buf[MB_MAXBYTES + 1];
12360
12361 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12362 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12363 }
12364# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012365 list_append_dict(rettv->vval.v_list, dict);
12366 cur = cur->next;
12367 }
12368 }
12369#endif
12370}
12371
12372/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012373 * "getpid()" function
12374 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012376f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012377{
12378 rettv->vval.v_number = mch_get_pid();
12379}
12380
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012381static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012382
12383/*
12384 * "getcurpos()" function
12385 */
12386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012387f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012388{
12389 getpos_both(argvars, rettv, TRUE);
12390}
12391
Bram Moolenaar18081e32008-02-20 19:11:07 +000012392/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012393 * "getpos(string)" function
12394 */
12395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012396f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012397{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012398 getpos_both(argvars, rettv, FALSE);
12399}
12400
12401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012402getpos_both(
12403 typval_T *argvars,
12404 typval_T *rettv,
12405 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012406{
Bram Moolenaara5525202006-03-02 22:52:09 +000012407 pos_T *fp;
12408 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012409 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012410
12411 if (rettv_list_alloc(rettv) == OK)
12412 {
12413 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012414 if (getcurpos)
12415 fp = &curwin->w_cursor;
12416 else
12417 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012418 if (fnum != -1)
12419 list_append_number(l, (varnumber_T)fnum);
12420 else
12421 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012422 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12423 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012424 list_append_number(l, (fp != NULL)
12425 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012426 : (varnumber_T)0);
12427 list_append_number(l,
12428#ifdef FEAT_VIRTUALEDIT
12429 (fp != NULL) ? (varnumber_T)fp->coladd :
12430#endif
12431 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012432 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012433 list_append_number(l, curwin->w_curswant == MAXCOL ?
12434 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012435 }
12436 else
12437 rettv->vval.v_number = FALSE;
12438}
12439
12440/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012441 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012442 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012444f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012445{
12446#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012447 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012448#endif
12449
Bram Moolenaar2641f772005-03-25 21:58:17 +000012450#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012451 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012452 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012453 wp = NULL;
12454 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12455 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012456 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012457 if (wp == NULL)
12458 return;
12459 }
12460
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012461 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012462 }
12463#endif
12464}
12465
12466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 * "getreg()" function
12468 */
12469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012470f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471{
12472 char_u *strregname;
12473 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012474 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012475 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012476 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012478 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012480 strregname = get_tv_string_chk(&argvars[0]);
12481 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012482 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012485 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12486 return_list = get_tv_number_chk(&argvars[2], &error);
12487 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012490 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012491
12492 if (error)
12493 return;
12494
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495 regname = (strregname == NULL ? '"' : *strregname);
12496 if (regname == 0)
12497 regname = '"';
12498
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012499 if (return_list)
12500 {
12501 rettv->v_type = VAR_LIST;
12502 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12503 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012504 if (rettv->vval.v_list != NULL)
12505 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012506 }
12507 else
12508 {
12509 rettv->v_type = VAR_STRING;
12510 rettv->vval.v_string = get_reg_contents(regname,
12511 arg2 ? GREG_EXPR_SRC : 0);
12512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513}
12514
12515/*
12516 * "getregtype()" function
12517 */
12518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012519f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520{
12521 char_u *strregname;
12522 int regname;
12523 char_u buf[NUMBUFLEN + 2];
12524 long reglen = 0;
12525
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012526 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012527 {
12528 strregname = get_tv_string_chk(&argvars[0]);
12529 if (strregname == NULL) /* type error; errmsg already given */
12530 {
12531 rettv->v_type = VAR_STRING;
12532 rettv->vval.v_string = NULL;
12533 return;
12534 }
12535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536 else
12537 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012538 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539
12540 regname = (strregname == NULL ? '"' : *strregname);
12541 if (regname == 0)
12542 regname = '"';
12543
12544 buf[0] = NUL;
12545 buf[1] = NUL;
12546 switch (get_reg_type(regname, &reglen))
12547 {
12548 case MLINE: buf[0] = 'V'; break;
12549 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 case MBLOCK:
12551 buf[0] = Ctrl_V;
12552 sprintf((char *)buf + 1, "%ld", reglen + 1);
12553 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555 rettv->v_type = VAR_STRING;
12556 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557}
12558
12559/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012560 * "gettabvar()" function
12561 */
12562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012563f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012564{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012565 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012566 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012567 dictitem_T *v;
12568 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012569 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012570
12571 rettv->v_type = VAR_STRING;
12572 rettv->vval.v_string = NULL;
12573
12574 varname = get_tv_string_chk(&argvars[1]);
12575 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12576 if (tp != NULL && varname != NULL)
12577 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012578 /* Set tp to be our tabpage, temporarily. Also set the window to the
12579 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012580 if (switch_win(&oldcurwin, &oldtabpage,
12581 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012582 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012583 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012584 /* look up the variable */
12585 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12586 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12587 if (v != NULL)
12588 {
12589 copy_tv(&v->di_tv, rettv);
12590 done = TRUE;
12591 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012592 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012593
12594 /* restore previous notion of curwin */
12595 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012596 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012597
12598 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012599 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012600}
12601
12602/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012603 * "gettabwinvar()" function
12604 */
12605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012606f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012607{
12608 getwinvar(argvars, rettv, 1);
12609}
12610
12611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 * "getwinposx()" function
12613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012615f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012617 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618#ifdef FEAT_GUI
12619 if (gui.in_use)
12620 {
12621 int x, y;
12622
12623 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012624 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625 }
12626#endif
12627}
12628
12629/*
12630 * "getwinposy()" function
12631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012633f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012635 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636#ifdef FEAT_GUI
12637 if (gui.in_use)
12638 {
12639 int x, y;
12640
12641 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012642 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 }
12644#endif
12645}
12646
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012647/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012648 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012649 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012650 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012651find_win_by_nr(
12652 typval_T *vp,
12653 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012654{
12655#ifdef FEAT_WINDOWS
12656 win_T *wp;
12657#endif
12658 int nr;
12659
12660 nr = get_tv_number_chk(vp, NULL);
12661
12662#ifdef FEAT_WINDOWS
12663 if (nr < 0)
12664 return NULL;
12665 if (nr == 0)
12666 return curwin;
12667
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012668 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12669 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012670 if (--nr <= 0)
12671 break;
12672 return wp;
12673#else
12674 if (nr == 0 || nr == 1)
12675 return curwin;
12676 return NULL;
12677#endif
12678}
12679
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012681 * Find window specified by "wvp" in tabpage "tvp".
12682 */
12683 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012684find_tabwin(
12685 typval_T *wvp, /* VAR_UNKNOWN for current window */
12686 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012687{
12688 win_T *wp = NULL;
12689 tabpage_T *tp = NULL;
12690 long n;
12691
12692 if (wvp->v_type != VAR_UNKNOWN)
12693 {
12694 if (tvp->v_type != VAR_UNKNOWN)
12695 {
12696 n = get_tv_number(tvp);
12697 if (n >= 0)
12698 tp = find_tabpage(n);
12699 }
12700 else
12701 tp = curtab;
12702
12703 if (tp != NULL)
12704 wp = find_win_by_nr(wvp, tp);
12705 }
12706 else
12707 wp = curwin;
12708
12709 return wp;
12710}
12711
12712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 * "getwinvar()" function
12714 */
12715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012716f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012718 getwinvar(argvars, rettv, 0);
12719}
12720
12721/*
12722 * getwinvar() and gettabwinvar()
12723 */
12724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012725getwinvar(
12726 typval_T *argvars,
12727 typval_T *rettv,
12728 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012729{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012730 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012732 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012733 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012734 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012735#ifdef FEAT_WINDOWS
12736 win_T *oldcurwin;
12737 tabpage_T *oldtabpage;
12738 int need_switch_win;
12739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012741#ifdef FEAT_WINDOWS
12742 if (off == 1)
12743 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12744 else
12745 tp = curtab;
12746#endif
12747 win = find_win_by_nr(&argvars[off], tp);
12748 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012749 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012751 rettv->v_type = VAR_STRING;
12752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753
12754 if (win != NULL && varname != NULL)
12755 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012756#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012757 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012758 * otherwise the window is not valid. Only do this when needed,
12759 * autocommands get blocked. */
12760 need_switch_win = !(tp == curtab && win == curwin);
12761 if (!need_switch_win
12762 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12763#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012764 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012765 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012766 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012767 if (get_option_tv(&varname, rettv, 1) == OK)
12768 done = TRUE;
12769 }
12770 else
12771 {
12772 /* Look up the variable. */
12773 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12774 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12775 varname, FALSE);
12776 if (v != NULL)
12777 {
12778 copy_tv(&v->di_tv, rettv);
12779 done = TRUE;
12780 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012783
Bram Moolenaarba117c22015-09-29 16:53:22 +020012784#ifdef FEAT_WINDOWS
12785 if (need_switch_win)
12786 /* restore previous notion of curwin */
12787 restore_win(oldcurwin, oldtabpage, TRUE);
12788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 }
12790
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012791 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12792 /* use the default return value */
12793 copy_tv(&argvars[off + 2], rettv);
12794
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 --emsg_off;
12796}
12797
12798/*
12799 * "glob()" function
12800 */
12801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012802f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012804 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012806 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012808 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012809 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012811 if (argvars[1].v_type != VAR_UNKNOWN)
12812 {
12813 if (get_tv_number_chk(&argvars[1], &error))
12814 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012815 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012816 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012817 if (get_tv_number_chk(&argvars[2], &error))
12818 {
12819 rettv->v_type = VAR_LIST;
12820 rettv->vval.v_list = NULL;
12821 }
12822 if (argvars[3].v_type != VAR_UNKNOWN
12823 && get_tv_number_chk(&argvars[3], &error))
12824 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012825 }
12826 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012827 if (!error)
12828 {
12829 ExpandInit(&xpc);
12830 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012831 if (p_wic)
12832 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012833 if (rettv->v_type == VAR_STRING)
12834 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012835 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012836 else if (rettv_list_alloc(rettv) != FAIL)
12837 {
12838 int i;
12839
12840 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12841 NULL, options, WILD_ALL_KEEP);
12842 for (i = 0; i < xpc.xp_numfiles; i++)
12843 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12844
12845 ExpandCleanup(&xpc);
12846 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012847 }
12848 else
12849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850}
12851
12852/*
12853 * "globpath()" function
12854 */
12855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012856f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012858 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012860 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012861 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012862 garray_T ga;
12863 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012865 /* When the optional second argument is non-zero, don't remove matches
12866 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012868 if (argvars[2].v_type != VAR_UNKNOWN)
12869 {
12870 if (get_tv_number_chk(&argvars[2], &error))
12871 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012872 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012873 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012874 if (get_tv_number_chk(&argvars[3], &error))
12875 {
12876 rettv->v_type = VAR_LIST;
12877 rettv->vval.v_list = NULL;
12878 }
12879 if (argvars[4].v_type != VAR_UNKNOWN
12880 && get_tv_number_chk(&argvars[4], &error))
12881 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012882 }
12883 }
12884 if (file != NULL && !error)
12885 {
12886 ga_init2(&ga, (int)sizeof(char_u *), 10);
12887 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12888 if (rettv->v_type == VAR_STRING)
12889 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12890 else if (rettv_list_alloc(rettv) != FAIL)
12891 for (i = 0; i < ga.ga_len; ++i)
12892 list_append_string(rettv->vval.v_list,
12893 ((char_u **)(ga.ga_data))[i], -1);
12894 ga_clear_strings(&ga);
12895 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012896 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012897 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898}
12899
12900/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012901 * "glob2regpat()" function
12902 */
12903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012904f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012905{
12906 char_u *pat = get_tv_string_chk(&argvars[0]);
12907
12908 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012909 rettv->vval.v_string = (pat == NULL)
12910 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012911}
12912
12913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 * "has()" function
12915 */
12916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012917f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918{
12919 int i;
12920 char_u *name;
12921 int n = FALSE;
12922 static char *(has_list[]) =
12923 {
12924#ifdef AMIGA
12925 "amiga",
12926# ifdef FEAT_ARP
12927 "arp",
12928# endif
12929#endif
12930#ifdef __BEOS__
12931 "beos",
12932#endif
12933#ifdef MSDOS
12934# ifdef DJGPP
12935 "dos32",
12936# else
12937 "dos16",
12938# endif
12939#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012940#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 "mac",
12942#endif
12943#if defined(MACOS_X_UNIX)
12944 "macunix",
12945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946#ifdef __QNX__
12947 "qnx",
12948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949#ifdef UNIX
12950 "unix",
12951#endif
12952#ifdef VMS
12953 "vms",
12954#endif
12955#ifdef WIN16
12956 "win16",
12957#endif
12958#ifdef WIN32
12959 "win32",
12960#endif
12961#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12962 "win32unix",
12963#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012964#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 "win64",
12966#endif
12967#ifdef EBCDIC
12968 "ebcdic",
12969#endif
12970#ifndef CASE_INSENSITIVE_FILENAME
12971 "fname_case",
12972#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012973#ifdef HAVE_ACL
12974 "acl",
12975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976#ifdef FEAT_ARABIC
12977 "arabic",
12978#endif
12979#ifdef FEAT_AUTOCMD
12980 "autocmd",
12981#endif
12982#ifdef FEAT_BEVAL
12983 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012984# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12985 "balloon_multiline",
12986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987#endif
12988#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12989 "builtin_terms",
12990# ifdef ALL_BUILTIN_TCAPS
12991 "all_builtin_terms",
12992# endif
12993#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012994#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12995 || defined(FEAT_GUI_W32) \
12996 || defined(FEAT_GUI_MOTIF))
12997 "browsefilter",
12998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999#ifdef FEAT_BYTEOFF
13000 "byte_offset",
13001#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013002#ifdef FEAT_CHANNEL
13003 "channel",
13004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005#ifdef FEAT_CINDENT
13006 "cindent",
13007#endif
13008#ifdef FEAT_CLIENTSERVER
13009 "clientserver",
13010#endif
13011#ifdef FEAT_CLIPBOARD
13012 "clipboard",
13013#endif
13014#ifdef FEAT_CMDL_COMPL
13015 "cmdline_compl",
13016#endif
13017#ifdef FEAT_CMDHIST
13018 "cmdline_hist",
13019#endif
13020#ifdef FEAT_COMMENTS
13021 "comments",
13022#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013023#ifdef FEAT_CONCEAL
13024 "conceal",
13025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026#ifdef FEAT_CRYPT
13027 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013028 "crypt-blowfish",
13029 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030#endif
13031#ifdef FEAT_CSCOPE
13032 "cscope",
13033#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013034#ifdef FEAT_CURSORBIND
13035 "cursorbind",
13036#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013037#ifdef CURSOR_SHAPE
13038 "cursorshape",
13039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040#ifdef DEBUG
13041 "debug",
13042#endif
13043#ifdef FEAT_CON_DIALOG
13044 "dialog_con",
13045#endif
13046#ifdef FEAT_GUI_DIALOG
13047 "dialog_gui",
13048#endif
13049#ifdef FEAT_DIFF
13050 "diff",
13051#endif
13052#ifdef FEAT_DIGRAPHS
13053 "digraphs",
13054#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013055#ifdef FEAT_DIRECTX
13056 "directx",
13057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058#ifdef FEAT_DND
13059 "dnd",
13060#endif
13061#ifdef FEAT_EMACS_TAGS
13062 "emacs_tags",
13063#endif
13064 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013065 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066#ifdef FEAT_SEARCH_EXTRA
13067 "extra_search",
13068#endif
13069#ifdef FEAT_FKMAP
13070 "farsi",
13071#endif
13072#ifdef FEAT_SEARCHPATH
13073 "file_in_path",
13074#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013075#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013076 "filterpipe",
13077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078#ifdef FEAT_FIND_ID
13079 "find_in_path",
13080#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013081#ifdef FEAT_FLOAT
13082 "float",
13083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084#ifdef FEAT_FOLDING
13085 "folding",
13086#endif
13087#ifdef FEAT_FOOTER
13088 "footer",
13089#endif
13090#if !defined(USE_SYSTEM) && defined(UNIX)
13091 "fork",
13092#endif
13093#ifdef FEAT_GETTEXT
13094 "gettext",
13095#endif
13096#ifdef FEAT_GUI
13097 "gui",
13098#endif
13099#ifdef FEAT_GUI_ATHENA
13100# ifdef FEAT_GUI_NEXTAW
13101 "gui_neXtaw",
13102# else
13103 "gui_athena",
13104# endif
13105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106#ifdef FEAT_GUI_GTK
13107 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013110#ifdef FEAT_GUI_GNOME
13111 "gui_gnome",
13112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113#ifdef FEAT_GUI_MAC
13114 "gui_mac",
13115#endif
13116#ifdef FEAT_GUI_MOTIF
13117 "gui_motif",
13118#endif
13119#ifdef FEAT_GUI_PHOTON
13120 "gui_photon",
13121#endif
13122#ifdef FEAT_GUI_W16
13123 "gui_win16",
13124#endif
13125#ifdef FEAT_GUI_W32
13126 "gui_win32",
13127#endif
13128#ifdef FEAT_HANGULIN
13129 "hangul_input",
13130#endif
13131#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13132 "iconv",
13133#endif
13134#ifdef FEAT_INS_EXPAND
13135 "insert_expand",
13136#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013137#ifdef FEAT_JOB
13138 "job",
13139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140#ifdef FEAT_JUMPLIST
13141 "jumplist",
13142#endif
13143#ifdef FEAT_KEYMAP
13144 "keymap",
13145#endif
13146#ifdef FEAT_LANGMAP
13147 "langmap",
13148#endif
13149#ifdef FEAT_LIBCALL
13150 "libcall",
13151#endif
13152#ifdef FEAT_LINEBREAK
13153 "linebreak",
13154#endif
13155#ifdef FEAT_LISP
13156 "lispindent",
13157#endif
13158#ifdef FEAT_LISTCMDS
13159 "listcmds",
13160#endif
13161#ifdef FEAT_LOCALMAP
13162 "localmap",
13163#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013164#ifdef FEAT_LUA
13165# ifndef DYNAMIC_LUA
13166 "lua",
13167# endif
13168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169#ifdef FEAT_MENU
13170 "menu",
13171#endif
13172#ifdef FEAT_SESSION
13173 "mksession",
13174#endif
13175#ifdef FEAT_MODIFY_FNAME
13176 "modify_fname",
13177#endif
13178#ifdef FEAT_MOUSE
13179 "mouse",
13180#endif
13181#ifdef FEAT_MOUSESHAPE
13182 "mouseshape",
13183#endif
13184#if defined(UNIX) || defined(VMS)
13185# ifdef FEAT_MOUSE_DEC
13186 "mouse_dec",
13187# endif
13188# ifdef FEAT_MOUSE_GPM
13189 "mouse_gpm",
13190# endif
13191# ifdef FEAT_MOUSE_JSB
13192 "mouse_jsbterm",
13193# endif
13194# ifdef FEAT_MOUSE_NET
13195 "mouse_netterm",
13196# endif
13197# ifdef FEAT_MOUSE_PTERM
13198 "mouse_pterm",
13199# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013200# ifdef FEAT_MOUSE_SGR
13201 "mouse_sgr",
13202# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013203# ifdef FEAT_SYSMOUSE
13204 "mouse_sysmouse",
13205# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013206# ifdef FEAT_MOUSE_URXVT
13207 "mouse_urxvt",
13208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209# ifdef FEAT_MOUSE_XTERM
13210 "mouse_xterm",
13211# endif
13212#endif
13213#ifdef FEAT_MBYTE
13214 "multi_byte",
13215#endif
13216#ifdef FEAT_MBYTE_IME
13217 "multi_byte_ime",
13218#endif
13219#ifdef FEAT_MULTI_LANG
13220 "multi_lang",
13221#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013222#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013223#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013224 "mzscheme",
13225#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227#ifdef FEAT_OLE
13228 "ole",
13229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230#ifdef FEAT_PATH_EXTRA
13231 "path_extra",
13232#endif
13233#ifdef FEAT_PERL
13234#ifndef DYNAMIC_PERL
13235 "perl",
13236#endif
13237#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013238#ifdef FEAT_PERSISTENT_UNDO
13239 "persistent_undo",
13240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241#ifdef FEAT_PYTHON
13242#ifndef DYNAMIC_PYTHON
13243 "python",
13244#endif
13245#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013246#ifdef FEAT_PYTHON3
13247#ifndef DYNAMIC_PYTHON3
13248 "python3",
13249#endif
13250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251#ifdef FEAT_POSTSCRIPT
13252 "postscript",
13253#endif
13254#ifdef FEAT_PRINTER
13255 "printer",
13256#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013257#ifdef FEAT_PROFILE
13258 "profile",
13259#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013260#ifdef FEAT_RELTIME
13261 "reltime",
13262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263#ifdef FEAT_QUICKFIX
13264 "quickfix",
13265#endif
13266#ifdef FEAT_RIGHTLEFT
13267 "rightleft",
13268#endif
13269#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13270 "ruby",
13271#endif
13272#ifdef FEAT_SCROLLBIND
13273 "scrollbind",
13274#endif
13275#ifdef FEAT_CMDL_INFO
13276 "showcmd",
13277 "cmdline_info",
13278#endif
13279#ifdef FEAT_SIGNS
13280 "signs",
13281#endif
13282#ifdef FEAT_SMARTINDENT
13283 "smartindent",
13284#endif
13285#ifdef FEAT_SNIFF
13286 "sniff",
13287#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013288#ifdef STARTUPTIME
13289 "startuptime",
13290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291#ifdef FEAT_STL_OPT
13292 "statusline",
13293#endif
13294#ifdef FEAT_SUN_WORKSHOP
13295 "sun_workshop",
13296#endif
13297#ifdef FEAT_NETBEANS_INTG
13298 "netbeans_intg",
13299#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013300#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013301 "spell",
13302#endif
13303#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 "syntax",
13305#endif
13306#if defined(USE_SYSTEM) || !defined(UNIX)
13307 "system",
13308#endif
13309#ifdef FEAT_TAG_BINS
13310 "tag_binary",
13311#endif
13312#ifdef FEAT_TAG_OLDSTATIC
13313 "tag_old_static",
13314#endif
13315#ifdef FEAT_TAG_ANYWHITE
13316 "tag_any_white",
13317#endif
13318#ifdef FEAT_TCL
13319# ifndef DYNAMIC_TCL
13320 "tcl",
13321# endif
13322#endif
13323#ifdef TERMINFO
13324 "terminfo",
13325#endif
13326#ifdef FEAT_TERMRESPONSE
13327 "termresponse",
13328#endif
13329#ifdef FEAT_TEXTOBJ
13330 "textobjects",
13331#endif
13332#ifdef HAVE_TGETENT
13333 "tgetent",
13334#endif
13335#ifdef FEAT_TITLE
13336 "title",
13337#endif
13338#ifdef FEAT_TOOLBAR
13339 "toolbar",
13340#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013341#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13342 "unnamedplus",
13343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344#ifdef FEAT_USR_CMDS
13345 "user-commands", /* was accidentally included in 5.4 */
13346 "user_commands",
13347#endif
13348#ifdef FEAT_VIMINFO
13349 "viminfo",
13350#endif
13351#ifdef FEAT_VERTSPLIT
13352 "vertsplit",
13353#endif
13354#ifdef FEAT_VIRTUALEDIT
13355 "virtualedit",
13356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358#ifdef FEAT_VISUALEXTRA
13359 "visualextra",
13360#endif
13361#ifdef FEAT_VREPLACE
13362 "vreplace",
13363#endif
13364#ifdef FEAT_WILDIGN
13365 "wildignore",
13366#endif
13367#ifdef FEAT_WILDMENU
13368 "wildmenu",
13369#endif
13370#ifdef FEAT_WINDOWS
13371 "windows",
13372#endif
13373#ifdef FEAT_WAK
13374 "winaltkeys",
13375#endif
13376#ifdef FEAT_WRITEBACKUP
13377 "writebackup",
13378#endif
13379#ifdef FEAT_XIM
13380 "xim",
13381#endif
13382#ifdef FEAT_XFONTSET
13383 "xfontset",
13384#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013385#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013386 "xpm",
13387 "xpm_w32", /* for backward compatibility */
13388#else
13389# if defined(HAVE_XPM)
13390 "xpm",
13391# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393#ifdef USE_XSMP
13394 "xsmp",
13395#endif
13396#ifdef USE_XSMP_INTERACT
13397 "xsmp_interact",
13398#endif
13399#ifdef FEAT_XCLIPBOARD
13400 "xterm_clipboard",
13401#endif
13402#ifdef FEAT_XTERM_SAVE
13403 "xterm_save",
13404#endif
13405#if defined(UNIX) && defined(FEAT_X11)
13406 "X11",
13407#endif
13408 NULL
13409 };
13410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 for (i = 0; has_list[i] != NULL; ++i)
13413 if (STRICMP(name, has_list[i]) == 0)
13414 {
13415 n = TRUE;
13416 break;
13417 }
13418
13419 if (n == FALSE)
13420 {
13421 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013422 {
13423 if (name[5] == '-'
13424 && STRLEN(name) > 11
13425 && vim_isdigit(name[6])
13426 && vim_isdigit(name[8])
13427 && vim_isdigit(name[10]))
13428 {
13429 int major = atoi((char *)name + 6);
13430 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013431
13432 /* Expect "patch-9.9.01234". */
13433 n = (major < VIM_VERSION_MAJOR
13434 || (major == VIM_VERSION_MAJOR
13435 && (minor < VIM_VERSION_MINOR
13436 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013437 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013438 }
13439 else
13440 n = has_patch(atoi((char *)name + 5));
13441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 else if (STRICMP(name, "vim_starting") == 0)
13443 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013444#ifdef FEAT_MBYTE
13445 else if (STRICMP(name, "multi_byte_encoding") == 0)
13446 n = has_mbyte;
13447#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013448#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13449 else if (STRICMP(name, "balloon_multiline") == 0)
13450 n = multiline_balloon_available();
13451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452#ifdef DYNAMIC_TCL
13453 else if (STRICMP(name, "tcl") == 0)
13454 n = tcl_enabled(FALSE);
13455#endif
13456#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13457 else if (STRICMP(name, "iconv") == 0)
13458 n = iconv_enabled(FALSE);
13459#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013460#ifdef DYNAMIC_LUA
13461 else if (STRICMP(name, "lua") == 0)
13462 n = lua_enabled(FALSE);
13463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013464#ifdef DYNAMIC_MZSCHEME
13465 else if (STRICMP(name, "mzscheme") == 0)
13466 n = mzscheme_enabled(FALSE);
13467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468#ifdef DYNAMIC_RUBY
13469 else if (STRICMP(name, "ruby") == 0)
13470 n = ruby_enabled(FALSE);
13471#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013472#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473#ifdef DYNAMIC_PYTHON
13474 else if (STRICMP(name, "python") == 0)
13475 n = python_enabled(FALSE);
13476#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013477#endif
13478#ifdef FEAT_PYTHON3
13479#ifdef DYNAMIC_PYTHON3
13480 else if (STRICMP(name, "python3") == 0)
13481 n = python3_enabled(FALSE);
13482#endif
13483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484#ifdef DYNAMIC_PERL
13485 else if (STRICMP(name, "perl") == 0)
13486 n = perl_enabled(FALSE);
13487#endif
13488#ifdef FEAT_GUI
13489 else if (STRICMP(name, "gui_running") == 0)
13490 n = (gui.in_use || gui.starting);
13491# ifdef FEAT_GUI_W32
13492 else if (STRICMP(name, "gui_win32s") == 0)
13493 n = gui_is_win32s();
13494# endif
13495# ifdef FEAT_BROWSE
13496 else if (STRICMP(name, "browse") == 0)
13497 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13498# endif
13499#endif
13500#ifdef FEAT_SYN_HL
13501 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013502 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503#endif
13504#if defined(WIN3264)
13505 else if (STRICMP(name, "win95") == 0)
13506 n = mch_windows95();
13507#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013508#ifdef FEAT_NETBEANS_INTG
13509 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013510 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 }
13513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515}
13516
13517/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013518 * "has_key()" function
13519 */
13520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013521f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013522{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013523 if (argvars[0].v_type != VAR_DICT)
13524 {
13525 EMSG(_(e_dictreq));
13526 return;
13527 }
13528 if (argvars[0].vval.v_dict == NULL)
13529 return;
13530
13531 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013532 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013533}
13534
13535/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013536 * "haslocaldir()" function
13537 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013539f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013540{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013541 win_T *wp = NULL;
13542
13543 wp = find_tabwin(&argvars[0], &argvars[1]);
13544 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013545}
13546
13547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 * "hasmapto()" function
13549 */
13550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013551f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552{
13553 char_u *name;
13554 char_u *mode;
13555 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013556 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013559 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 mode = (char_u *)"nvo";
13561 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013563 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013564 if (argvars[2].v_type != VAR_UNKNOWN)
13565 abbr = get_tv_number(&argvars[2]);
13566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567
Bram Moolenaar2c932302006-03-18 21:42:09 +000013568 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572}
13573
13574/*
13575 * "histadd()" function
13576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013578f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579{
13580#ifdef FEAT_CMDHIST
13581 int histype;
13582 char_u *str;
13583 char_u buf[NUMBUFLEN];
13584#endif
13585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 if (check_restricted() || check_secure())
13588 return;
13589#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13591 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 if (histype >= 0)
13593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 if (*str != NUL)
13596 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013597 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 return;
13601 }
13602 }
13603#endif
13604}
13605
13606/*
13607 * "histdel()" function
13608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013610f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611{
13612#ifdef FEAT_CMDHIST
13613 int n;
13614 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013615 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013617 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13618 if (str == NULL)
13619 n = 0;
13620 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013623 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 else
13628 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013629 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013630 get_tv_string_buf(&argvars[1], buf));
13631 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632#endif
13633}
13634
13635/*
13636 * "histget()" function
13637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013639f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640{
13641#ifdef FEAT_CMDHIST
13642 int type;
13643 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013644 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013646 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13647 if (str == NULL)
13648 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013650 {
13651 type = get_histtype(str);
13652 if (argvars[1].v_type == VAR_UNKNOWN)
13653 idx = get_history_idx(type);
13654 else
13655 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13656 /* -1 on type error */
13657 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663}
13664
13665/*
13666 * "histnr()" function
13667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013669f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670{
13671 int i;
13672
13673#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674 char_u *history = get_tv_string_chk(&argvars[0]);
13675
13676 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 if (i >= HIST_CMD && i < HIST_COUNT)
13678 i = get_history_idx(i);
13679 else
13680#endif
13681 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683}
13684
13685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 * "highlightID(name)" function
13687 */
13688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013689f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013691 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692}
13693
13694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013695 * "highlight_exists()" function
13696 */
13697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013698f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013699{
13700 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13701}
13702
13703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 * "hostname()" function
13705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013707f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708{
13709 char_u hostname[256];
13710
13711 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 rettv->v_type = VAR_STRING;
13713 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714}
13715
13716/*
13717 * iconv() function
13718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013720f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721{
13722#ifdef FEAT_MBYTE
13723 char_u buf1[NUMBUFLEN];
13724 char_u buf2[NUMBUFLEN];
13725 char_u *from, *to, *str;
13726 vimconv_T vimconv;
13727#endif
13728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->v_type = VAR_STRING;
13730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731
13732#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733 str = get_tv_string(&argvars[0]);
13734 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13735 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736 vimconv.vc_type = CONV_NONE;
13737 convert_setup(&vimconv, from, to);
13738
13739 /* If the encodings are equal, no conversion needed. */
13740 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013743 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744
13745 convert_setup(&vimconv, NULL, NULL);
13746 vim_free(from);
13747 vim_free(to);
13748#endif
13749}
13750
13751/*
13752 * "indent()" function
13753 */
13754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013755f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756{
13757 linenr_T lnum;
13758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013759 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013763 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764}
13765
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013766/*
13767 * "index()" function
13768 */
13769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013770f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013771{
Bram Moolenaar33570922005-01-25 22:26:29 +000013772 list_T *l;
13773 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013774 long idx = 0;
13775 int ic = FALSE;
13776
13777 rettv->vval.v_number = -1;
13778 if (argvars[0].v_type != VAR_LIST)
13779 {
13780 EMSG(_(e_listreq));
13781 return;
13782 }
13783 l = argvars[0].vval.v_list;
13784 if (l != NULL)
13785 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013786 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013787 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789 int error = FALSE;
13790
Bram Moolenaar758711c2005-02-02 23:11:38 +000013791 /* Start at specified item. Use the cached index that list_find()
13792 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013793 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013794 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013795 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013796 ic = get_tv_number_chk(&argvars[3], &error);
13797 if (error)
13798 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013799 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013800
Bram Moolenaar758711c2005-02-02 23:11:38 +000013801 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013802 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013803 {
13804 rettv->vval.v_number = idx;
13805 break;
13806 }
13807 }
13808}
13809
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810static int inputsecret_flag = 0;
13811
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013812static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013813
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013815 * This function is used by f_input() and f_inputdialog() functions. The third
13816 * argument to f_input() specifies the type of completion to use at the
13817 * prompt. The third argument to f_inputdialog() specifies the value to return
13818 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 */
13820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013821get_user_input(
13822 typval_T *argvars,
13823 typval_T *rettv,
13824 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 char_u *p = NULL;
13828 int c;
13829 char_u buf[NUMBUFLEN];
13830 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013832 int xp_type = EXPAND_NOTHING;
13833 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013835 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013836 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837
13838#ifdef NO_CONSOLE_INPUT
13839 /* While starting up, there is no place to enter text. */
13840 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842#endif
13843
13844 cmd_silent = FALSE; /* Want to see the prompt. */
13845 if (prompt != NULL)
13846 {
13847 /* Only the part of the message after the last NL is considered as
13848 * prompt for the command line */
13849 p = vim_strrchr(prompt, '\n');
13850 if (p == NULL)
13851 p = prompt;
13852 else
13853 {
13854 ++p;
13855 c = *p;
13856 *p = NUL;
13857 msg_start();
13858 msg_clr_eos();
13859 msg_puts_attr(prompt, echo_attr);
13860 msg_didout = FALSE;
13861 msg_starthere();
13862 *p = c;
13863 }
13864 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013866 if (argvars[1].v_type != VAR_UNKNOWN)
13867 {
13868 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13869 if (defstr != NULL)
13870 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013872 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013873 {
13874 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013875 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013876 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013877
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013878 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013879 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013880
Bram Moolenaar4463f292005-09-25 22:20:24 +000013881 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13882 if (xp_name == NULL)
13883 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013884
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013885 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013886
Bram Moolenaar4463f292005-09-25 22:20:24 +000013887 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13888 &xp_arg) == FAIL)
13889 return;
13890 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013891 }
13892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013893 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013894 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013895 int save_ex_normal_busy = ex_normal_busy;
13896 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013898 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13899 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013900 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013901 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013902 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013903 && argvars[1].v_type != VAR_UNKNOWN
13904 && argvars[2].v_type != VAR_UNKNOWN)
13905 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13906 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013907
13908 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013910 /* since the user typed this, no need to wait for return */
13911 need_wait_return = FALSE;
13912 msg_didout = FALSE;
13913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 cmd_silent = cmd_silent_save;
13915}
13916
13917/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013918 * "input()" function
13919 * Also handles inputsecret() when inputsecret is set.
13920 */
13921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013922f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013923{
13924 get_user_input(argvars, rettv, FALSE);
13925}
13926
13927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 * "inputdialog()" function
13929 */
13930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013931f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932{
13933#if defined(FEAT_GUI_TEXTDIALOG)
13934 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13935 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13936 {
13937 char_u *message;
13938 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013939 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013941 message = get_tv_string_chk(&argvars[0]);
13942 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013943 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013944 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945 else
13946 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013947 if (message != NULL && defstr != NULL
13948 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013949 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013950 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 else
13952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013953 if (message != NULL && defstr != NULL
13954 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013955 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956 rettv->vval.v_string = vim_strsave(
13957 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013959 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013961 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 }
13963 else
13964#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013965 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966}
13967
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013968/*
13969 * "inputlist()" function
13970 */
13971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013972f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013973{
13974 listitem_T *li;
13975 int selected;
13976 int mouse_used;
13977
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013978#ifdef NO_CONSOLE_INPUT
13979 /* While starting up, there is no place to enter text. */
13980 if (no_console_input())
13981 return;
13982#endif
13983 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13984 {
13985 EMSG2(_(e_listarg), "inputlist()");
13986 return;
13987 }
13988
13989 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013990 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013991 lines_left = Rows; /* avoid more prompt */
13992 msg_scroll = TRUE;
13993 msg_clr_eos();
13994
13995 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13996 {
13997 msg_puts(get_tv_string(&li->li_tv));
13998 msg_putchar('\n');
13999 }
14000
14001 /* Ask for choice. */
14002 selected = prompt_for_number(&mouse_used);
14003 if (mouse_used)
14004 selected -= lines_left;
14005
14006 rettv->vval.v_number = selected;
14007}
14008
14009
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14011
14012/*
14013 * "inputrestore()" function
14014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014016f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017{
14018 if (ga_userinput.ga_len > 0)
14019 {
14020 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14022 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014023 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 }
14025 else if (p_verbose > 1)
14026 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014027 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014028 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 }
14030}
14031
14032/*
14033 * "inputsave()" function
14034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014036f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014038 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 if (ga_grow(&ga_userinput, 1) == OK)
14040 {
14041 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14042 + ga_userinput.ga_len);
14043 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014044 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 }
14046 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048}
14049
14050/*
14051 * "inputsecret()" function
14052 */
14053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014054f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055{
14056 ++cmdline_star;
14057 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014058 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 --cmdline_star;
14060 --inputsecret_flag;
14061}
14062
14063/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014064 * "insert()" function
14065 */
14066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014067f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014068{
14069 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014070 listitem_T *item;
14071 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014072 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014073
14074 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014075 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014076 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014077 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014078 {
14079 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014080 before = get_tv_number_chk(&argvars[2], &error);
14081 if (error)
14082 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014083
Bram Moolenaar758711c2005-02-02 23:11:38 +000014084 if (before == l->lv_len)
14085 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014086 else
14087 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014088 item = list_find(l, before);
14089 if (item == NULL)
14090 {
14091 EMSGN(_(e_listidx), before);
14092 l = NULL;
14093 }
14094 }
14095 if (l != NULL)
14096 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014097 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014098 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014099 }
14100 }
14101}
14102
14103/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014104 * "invert(expr)" function
14105 */
14106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014107f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014108{
14109 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14110}
14111
14112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113 * "isdirectory()" function
14114 */
14115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014116f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014118 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119}
14120
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014121/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014122 * "islocked()" function
14123 */
14124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014125f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014126{
14127 lval_T lv;
14128 char_u *end;
14129 dictitem_T *di;
14130
14131 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014132 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14133 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014134 if (end != NULL && lv.ll_name != NULL)
14135 {
14136 if (*end != NUL)
14137 EMSG(_(e_trailing));
14138 else
14139 {
14140 if (lv.ll_tv == NULL)
14141 {
14142 if (check_changedtick(lv.ll_name))
14143 rettv->vval.v_number = 1; /* always locked */
14144 else
14145 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014146 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014147 if (di != NULL)
14148 {
14149 /* Consider a variable locked when:
14150 * 1. the variable itself is locked
14151 * 2. the value of the variable is locked.
14152 * 3. the List or Dict value is locked.
14153 */
14154 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14155 || tv_islocked(&di->di_tv));
14156 }
14157 }
14158 }
14159 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014160 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014161 else if (lv.ll_newkey != NULL)
14162 EMSG2(_(e_dictkey), lv.ll_newkey);
14163 else if (lv.ll_list != NULL)
14164 /* List item. */
14165 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14166 else
14167 /* Dictionary item. */
14168 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14169 }
14170 }
14171
14172 clear_lval(&lv);
14173}
14174
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014175static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014176
14177/*
14178 * Turn a dict into a list:
14179 * "what" == 0: list of keys
14180 * "what" == 1: list of values
14181 * "what" == 2: list of items
14182 */
14183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014184dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014185{
Bram Moolenaar33570922005-01-25 22:26:29 +000014186 list_T *l2;
14187 dictitem_T *di;
14188 hashitem_T *hi;
14189 listitem_T *li;
14190 listitem_T *li2;
14191 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014192 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014193
Bram Moolenaar8c711452005-01-14 21:53:12 +000014194 if (argvars[0].v_type != VAR_DICT)
14195 {
14196 EMSG(_(e_dictreq));
14197 return;
14198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014199 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014200 return;
14201
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014202 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014203 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014204
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014205 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014208 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014210 --todo;
14211 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014212
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014213 li = listitem_alloc();
14214 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014215 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014216 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014217
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014218 if (what == 0)
14219 {
14220 /* keys() */
14221 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014222 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014223 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14224 }
14225 else if (what == 1)
14226 {
14227 /* values() */
14228 copy_tv(&di->di_tv, &li->li_tv);
14229 }
14230 else
14231 {
14232 /* items() */
14233 l2 = list_alloc();
14234 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014235 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014236 li->li_tv.vval.v_list = l2;
14237 if (l2 == NULL)
14238 break;
14239 ++l2->lv_refcount;
14240
14241 li2 = listitem_alloc();
14242 if (li2 == NULL)
14243 break;
14244 list_append(l2, li2);
14245 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014246 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014247 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14248
14249 li2 = listitem_alloc();
14250 if (li2 == NULL)
14251 break;
14252 list_append(l2, li2);
14253 copy_tv(&di->di_tv, &li2->li_tv);
14254 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014255 }
14256 }
14257}
14258
14259/*
14260 * "items(dict)" function
14261 */
14262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014263f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014264{
14265 dict_list(argvars, rettv, 2);
14266}
14267
Bram Moolenaar835dc632016-02-07 14:27:38 +010014268#ifdef FEAT_JOB
14269/*
14270 * "job_start()" function
14271 */
14272 static void
14273f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14274{
14275 job_T *job;
14276 char_u *cmd = NULL;
14277#if defined(UNIX)
14278# define USE_ARGV
14279 char **argv = NULL;
14280 int argc = 0;
14281#else
14282 garray_T ga;
14283#endif
14284
14285 rettv->v_type = VAR_JOB;
14286 job = job_alloc();
14287 rettv->vval.v_job = job;
14288 if (job == NULL)
14289 return;
14290
14291 rettv->vval.v_job->jv_status = JOB_FAILED;
14292#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014293 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014294#endif
14295
14296 if (argvars[0].v_type == VAR_STRING)
14297 {
14298 /* Command is a string. */
14299 cmd = argvars[0].vval.v_string;
14300#ifdef USE_ARGV
14301 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14302 return;
14303 argv[argc] = NULL;
14304#endif
14305 }
14306 else if (argvars[0].v_type != VAR_LIST
14307 || argvars[0].vval.v_list == NULL
14308 || argvars[0].vval.v_list->lv_len < 1)
14309 {
14310 EMSG(_(e_invarg));
14311 return;
14312 }
14313 else
14314 {
14315 list_T *l = argvars[0].vval.v_list;
14316 listitem_T *li;
14317 char_u *s;
14318
14319#ifdef USE_ARGV
14320 /* Pass argv[] to mch_call_shell(). */
14321 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14322 if (argv == NULL)
14323 return;
14324#endif
14325 for (li = l->lv_first; li != NULL; li = li->li_next)
14326 {
14327 s = get_tv_string_chk(&li->li_tv);
14328 if (s == NULL)
14329 goto theend;
14330#ifdef USE_ARGV
14331 argv[argc++] = (char *)s;
14332#else
14333 if (li != l->lv_first)
14334 {
14335 s = vim_strsave_shellescape(s, FALSE, TRUE);
14336 if (s == NULL)
14337 goto theend;
14338 }
14339 ga_concat(&ga, s);
14340 vim_free(s);
14341 if (li->li_next != NULL)
14342 ga_append(&ga, ' ');
14343#endif
14344 }
14345#ifdef USE_ARGV
14346 argv[argc] = NULL;
14347#else
14348 cmd = ga.ga_data;
14349#endif
14350 }
14351#ifdef USE_ARGV
14352 mch_start_job(argv, job);
14353#else
14354 mch_start_job(cmd, job);
14355#endif
14356
14357theend:
14358#ifdef USE_ARGV
14359 if (argv != NULL)
14360 vim_free(argv);
14361#else
14362 vim_free(ga.ga_data);
14363#endif
14364}
14365
14366/*
14367 * "job_status()" function
14368 */
14369 static void
14370f_job_status(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14371{
14372 char *result;
14373
14374 if (argvars[0].v_type != VAR_JOB)
14375 EMSG(_(e_invarg));
14376 else
14377 {
14378 job_T *job = argvars[0].vval.v_job;
14379
14380 if (job->jv_status == JOB_ENDED)
14381 /* No need to check, dead is dead. */
14382 result = "dead";
14383 else if (job->jv_status == JOB_FAILED)
14384 result = "fail";
14385 else
14386 result = mch_job_status(job);
14387 rettv->v_type = VAR_STRING;
14388 rettv->vval.v_string = vim_strsave((char_u *)result);
14389 }
14390}
14391
14392/*
14393 * "job_stop()" function
14394 */
14395 static void
14396f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14397{
14398 if (argvars[0].v_type != VAR_JOB)
14399 EMSG(_(e_invarg));
14400 else
14401 {
14402 char_u *arg;
14403
14404 if (argvars[1].v_type == VAR_UNKNOWN)
14405 arg = (char_u *)"";
14406 else
14407 {
14408 arg = get_tv_string_chk(&argvars[1]);
14409 if (arg == NULL)
14410 {
14411 EMSG(_(e_invarg));
14412 return;
14413 }
14414 }
14415 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14416 rettv->vval.v_number = 0;
14417 else
14418 rettv->vval.v_number = 1;
14419 }
14420}
14421#endif
14422
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014424 * "join()" function
14425 */
14426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014427f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014428{
14429 garray_T ga;
14430 char_u *sep;
14431
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014432 if (argvars[0].v_type != VAR_LIST)
14433 {
14434 EMSG(_(e_listreq));
14435 return;
14436 }
14437 if (argvars[0].vval.v_list == NULL)
14438 return;
14439 if (argvars[1].v_type == VAR_UNKNOWN)
14440 sep = (char_u *)" ";
14441 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014442 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014443
14444 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445
14446 if (sep != NULL)
14447 {
14448 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014449 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014450 ga_append(&ga, NUL);
14451 rettv->vval.v_string = (char_u *)ga.ga_data;
14452 }
14453 else
14454 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014455}
14456
14457/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014458 * "jsdecode()" function
14459 */
14460 static void
14461f_jsdecode(typval_T *argvars, typval_T *rettv)
14462{
14463 js_read_T reader;
14464
14465 reader.js_buf = get_tv_string(&argvars[0]);
14466 reader.js_fill = NULL;
14467 reader.js_used = 0;
14468 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14469 EMSG(_(e_invarg));
14470}
14471
14472/*
14473 * "jsencode()" function
14474 */
14475 static void
14476f_jsencode(typval_T *argvars, typval_T *rettv)
14477{
14478 rettv->v_type = VAR_STRING;
14479 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14480}
14481
14482/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014483 * "jsondecode()" function
14484 */
14485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014486f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014487{
14488 js_read_T reader;
14489
14490 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014491 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014492 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014493 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014494 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014495}
14496
14497/*
14498 * "jsonencode()" function
14499 */
14500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014501f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014502{
14503 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014504 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014505}
14506
14507/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014508 * "keys()" function
14509 */
14510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014511f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014512{
14513 dict_list(argvars, rettv, 0);
14514}
14515
14516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 * "last_buffer_nr()" function.
14518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014520f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521{
14522 int n = 0;
14523 buf_T *buf;
14524
14525 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14526 if (n < buf->b_fnum)
14527 n = buf->b_fnum;
14528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014530}
14531
14532/*
14533 * "len()" function
14534 */
14535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014536f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014537{
14538 switch (argvars[0].v_type)
14539 {
14540 case VAR_STRING:
14541 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542 rettv->vval.v_number = (varnumber_T)STRLEN(
14543 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014544 break;
14545 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014547 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014548 case VAR_DICT:
14549 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14550 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014551 case VAR_UNKNOWN:
14552 case VAR_SPECIAL:
14553 case VAR_FLOAT:
14554 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014555 case VAR_JOB:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014556 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014557 break;
14558 }
14559}
14560
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014561static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014562
14563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014564libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014565{
14566#ifdef FEAT_LIBCALL
14567 char_u *string_in;
14568 char_u **string_result;
14569 int nr_result;
14570#endif
14571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014573 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014574 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014575
14576 if (check_restricted() || check_secure())
14577 return;
14578
14579#ifdef FEAT_LIBCALL
14580 /* The first two args must be strings, otherwise its meaningless */
14581 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14582 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014583 string_in = NULL;
14584 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014585 string_in = argvars[2].vval.v_string;
14586 if (type == VAR_NUMBER)
14587 string_result = NULL;
14588 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014589 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014590 if (mch_libcall(argvars[0].vval.v_string,
14591 argvars[1].vval.v_string,
14592 string_in,
14593 argvars[2].vval.v_number,
14594 string_result,
14595 &nr_result) == OK
14596 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014597 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014598 }
14599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600}
14601
14602/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603 * "libcall()" function
14604 */
14605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014606f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014607{
14608 libcall_common(argvars, rettv, VAR_STRING);
14609}
14610
14611/*
14612 * "libcallnr()" function
14613 */
14614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014615f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014616{
14617 libcall_common(argvars, rettv, VAR_NUMBER);
14618}
14619
14620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 * "line(string)" function
14622 */
14623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014624f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625{
14626 linenr_T lnum = 0;
14627 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014628 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014630 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 if (fp != NULL)
14632 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014633 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634}
14635
14636/*
14637 * "line2byte(lnum)" function
14638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014640f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641{
14642#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644#else
14645 linenr_T lnum;
14646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14652 if (rettv->vval.v_number >= 0)
14653 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654#endif
14655}
14656
14657/*
14658 * "lispindent(lnum)" function
14659 */
14660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014661f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662{
14663#ifdef FEAT_LISP
14664 pos_T pos;
14665 linenr_T lnum;
14666
14667 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014668 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14670 {
14671 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014672 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 curwin->w_cursor = pos;
14674 }
14675 else
14676#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014677 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678}
14679
14680/*
14681 * "localtime()" function
14682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014684f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687}
14688
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014689static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690
14691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014692get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693{
14694 char_u *keys;
14695 char_u *which;
14696 char_u buf[NUMBUFLEN];
14697 char_u *keys_buf = NULL;
14698 char_u *rhs;
14699 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014700 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014701 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014702 mapblock_T *mp;
14703 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704
14705 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014706 rettv->v_type = VAR_STRING;
14707 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014709 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 if (*keys == NUL)
14711 return;
14712
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014713 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014715 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014716 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014717 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014718 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014719 if (argvars[3].v_type != VAR_UNKNOWN)
14720 get_dict = get_tv_number(&argvars[3]);
14721 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 else
14724 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014725 if (which == NULL)
14726 return;
14727
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728 mode = get_map_mode(&which, 0);
14729
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014730 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014731 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014733
14734 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014736 /* Return a string. */
14737 if (rhs != NULL)
14738 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739
Bram Moolenaarbd743252010-10-20 21:23:33 +020014740 }
14741 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14742 {
14743 /* Return a dictionary. */
14744 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14745 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14746 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747
Bram Moolenaarbd743252010-10-20 21:23:33 +020014748 dict_add_nr_str(dict, "lhs", 0L, lhs);
14749 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14750 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14751 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14752 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14753 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14754 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014755 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014756 dict_add_nr_str(dict, "mode", 0L, mapmode);
14757
14758 vim_free(lhs);
14759 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 }
14761}
14762
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014763#ifdef FEAT_FLOAT
14764/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014765 * "log()" function
14766 */
14767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014768f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014769{
14770 float_T f;
14771
14772 rettv->v_type = VAR_FLOAT;
14773 if (get_float_arg(argvars, &f) == OK)
14774 rettv->vval.v_float = log(f);
14775 else
14776 rettv->vval.v_float = 0.0;
14777}
14778
14779/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014780 * "log10()" function
14781 */
14782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014783f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014784{
14785 float_T f;
14786
14787 rettv->v_type = VAR_FLOAT;
14788 if (get_float_arg(argvars, &f) == OK)
14789 rettv->vval.v_float = log10(f);
14790 else
14791 rettv->vval.v_float = 0.0;
14792}
14793#endif
14794
Bram Moolenaar1dced572012-04-05 16:54:08 +020014795#ifdef FEAT_LUA
14796/*
14797 * "luaeval()" function
14798 */
14799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014800f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014801{
14802 char_u *str;
14803 char_u buf[NUMBUFLEN];
14804
14805 str = get_tv_string_buf(&argvars[0], buf);
14806 do_luaeval(str, argvars + 1, rettv);
14807}
14808#endif
14809
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014811 * "map()" function
14812 */
14813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014814f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014815{
14816 filter_map(argvars, rettv, TRUE);
14817}
14818
14819/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014820 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 */
14822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014823f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014825 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826}
14827
14828/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014829 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830 */
14831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014832f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014834 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835}
14836
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014837static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838
14839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014840find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014842 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014843 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014844 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 char_u *pat;
14846 regmatch_T regmatch;
14847 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014848 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 char_u *save_cpo;
14850 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014851 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014852 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014853 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014854 list_T *l = NULL;
14855 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014856 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858
14859 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14860 save_cpo = p_cpo;
14861 p_cpo = (char_u *)"";
14862
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014863 rettv->vval.v_number = -1;
14864 if (type == 3)
14865 {
14866 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014867 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014868 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014869 }
14870 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014872 rettv->v_type = VAR_STRING;
14873 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014876 if (argvars[0].v_type == VAR_LIST)
14877 {
14878 if ((l = argvars[0].vval.v_list) == NULL)
14879 goto theend;
14880 li = l->lv_first;
14881 }
14882 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014883 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014884 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014885 len = (long)STRLEN(str);
14886 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014888 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14889 if (pat == NULL)
14890 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014891
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014892 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014894 int error = FALSE;
14895
14896 start = get_tv_number_chk(&argvars[2], &error);
14897 if (error)
14898 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014899 if (l != NULL)
14900 {
14901 li = list_find(l, start);
14902 if (li == NULL)
14903 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014904 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014905 }
14906 else
14907 {
14908 if (start < 0)
14909 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014910 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014911 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014912 /* When "count" argument is there ignore matches before "start",
14913 * otherwise skip part of the string. Differs when pattern is "^"
14914 * or "\<". */
14915 if (argvars[3].v_type != VAR_UNKNOWN)
14916 startcol = start;
14917 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014918 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014919 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014920 len -= start;
14921 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014922 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014923
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014924 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014925 nth = get_tv_number_chk(&argvars[3], &error);
14926 if (error)
14927 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 }
14929
14930 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14931 if (regmatch.regprog != NULL)
14932 {
14933 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014934
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014935 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014936 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014937 if (l != NULL)
14938 {
14939 if (li == NULL)
14940 {
14941 match = FALSE;
14942 break;
14943 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014944 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014945 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014946 if (str == NULL)
14947 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014948 }
14949
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014950 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014951
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014952 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014953 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014954 if (l == NULL && !match)
14955 break;
14956
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014957 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014958 if (l != NULL)
14959 {
14960 li = li->li_next;
14961 ++idx;
14962 }
14963 else
14964 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014965#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014966 startcol = (colnr_T)(regmatch.startp[0]
14967 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014968#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014969 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014970#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014971 if (startcol > (colnr_T)len
14972 || str + startcol <= regmatch.startp[0])
14973 {
14974 match = FALSE;
14975 break;
14976 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014977 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014978 }
14979
14980 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014982 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014984 int i;
14985
14986 /* return list with matched string and submatches */
14987 for (i = 0; i < NSUBEXP; ++i)
14988 {
14989 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014990 {
14991 if (list_append_string(rettv->vval.v_list,
14992 (char_u *)"", 0) == FAIL)
14993 break;
14994 }
14995 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014996 regmatch.startp[i],
14997 (int)(regmatch.endp[i] - regmatch.startp[i]))
14998 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014999 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015000 }
15001 }
15002 else if (type == 2)
15003 {
15004 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015005 if (l != NULL)
15006 copy_tv(&li->li_tv, rettv);
15007 else
15008 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015010 }
15011 else if (l != NULL)
15012 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 else
15014 {
15015 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015016 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 (varnumber_T)(regmatch.startp[0] - str);
15018 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015021 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022 }
15023 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015024 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 }
15026
15027theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015028 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 p_cpo = save_cpo;
15030}
15031
15032/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015033 * "match()" function
15034 */
15035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015036f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037{
15038 find_some_match(argvars, rettv, 1);
15039}
15040
15041/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015042 * "matchadd()" function
15043 */
15044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015045f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015046{
15047#ifdef FEAT_SEARCH_EXTRA
15048 char_u buf[NUMBUFLEN];
15049 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15050 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15051 int prio = 10; /* default priority */
15052 int id = -1;
15053 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015054 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015055
15056 rettv->vval.v_number = -1;
15057
15058 if (grp == NULL || pat == NULL)
15059 return;
15060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015061 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015062 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015063 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015064 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015065 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015066 if (argvars[4].v_type != VAR_UNKNOWN)
15067 {
15068 if (argvars[4].v_type != VAR_DICT)
15069 {
15070 EMSG(_(e_dictreq));
15071 return;
15072 }
15073 if (dict_find(argvars[4].vval.v_dict,
15074 (char_u *)"conceal", -1) != NULL)
15075 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15076 (char_u *)"conceal", FALSE);
15077 }
15078 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015079 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015080 if (error == TRUE)
15081 return;
15082 if (id >= 1 && id <= 3)
15083 {
15084 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15085 return;
15086 }
15087
Bram Moolenaar6561d522015-07-21 15:48:27 +020015088 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15089 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015090#endif
15091}
15092
15093/*
15094 * "matchaddpos()" function
15095 */
15096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015097f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015098{
15099#ifdef FEAT_SEARCH_EXTRA
15100 char_u buf[NUMBUFLEN];
15101 char_u *group;
15102 int prio = 10;
15103 int id = -1;
15104 int error = FALSE;
15105 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015106 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015107
15108 rettv->vval.v_number = -1;
15109
15110 group = get_tv_string_buf_chk(&argvars[0], buf);
15111 if (group == NULL)
15112 return;
15113
15114 if (argvars[1].v_type != VAR_LIST)
15115 {
15116 EMSG2(_(e_listarg), "matchaddpos()");
15117 return;
15118 }
15119 l = argvars[1].vval.v_list;
15120 if (l == NULL)
15121 return;
15122
15123 if (argvars[2].v_type != VAR_UNKNOWN)
15124 {
15125 prio = get_tv_number_chk(&argvars[2], &error);
15126 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015127 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015128 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015129 if (argvars[4].v_type != VAR_UNKNOWN)
15130 {
15131 if (argvars[4].v_type != VAR_DICT)
15132 {
15133 EMSG(_(e_dictreq));
15134 return;
15135 }
15136 if (dict_find(argvars[4].vval.v_dict,
15137 (char_u *)"conceal", -1) != NULL)
15138 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15139 (char_u *)"conceal", FALSE);
15140 }
15141 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015142 }
15143 if (error == TRUE)
15144 return;
15145
15146 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15147 if (id == 1 || id == 2)
15148 {
15149 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15150 return;
15151 }
15152
Bram Moolenaar6561d522015-07-21 15:48:27 +020015153 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15154 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015155#endif
15156}
15157
15158/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015159 * "matcharg()" function
15160 */
15161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015162f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015163{
15164 if (rettv_list_alloc(rettv) == OK)
15165 {
15166#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015167 int id = get_tv_number(&argvars[0]);
15168 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015169
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015170 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015171 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015172 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15173 {
15174 list_append_string(rettv->vval.v_list,
15175 syn_id2name(m->hlg_id), -1);
15176 list_append_string(rettv->vval.v_list, m->pattern, -1);
15177 }
15178 else
15179 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015180 list_append_string(rettv->vval.v_list, NULL, -1);
15181 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015182 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015183 }
15184#endif
15185 }
15186}
15187
15188/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015189 * "matchdelete()" function
15190 */
15191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015192f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015193{
15194#ifdef FEAT_SEARCH_EXTRA
15195 rettv->vval.v_number = match_delete(curwin,
15196 (int)get_tv_number(&argvars[0]), TRUE);
15197#endif
15198}
15199
15200/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201 * "matchend()" function
15202 */
15203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015204f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015205{
15206 find_some_match(argvars, rettv, 0);
15207}
15208
15209/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015210 * "matchlist()" function
15211 */
15212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015213f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015214{
15215 find_some_match(argvars, rettv, 3);
15216}
15217
15218/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219 * "matchstr()" function
15220 */
15221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015222f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223{
15224 find_some_match(argvars, rettv, 2);
15225}
15226
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015227static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015228
15229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015230max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015231{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015232 long n = 0;
15233 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015234 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015235
15236 if (argvars[0].v_type == VAR_LIST)
15237 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015238 list_T *l;
15239 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015240
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015241 l = argvars[0].vval.v_list;
15242 if (l != NULL)
15243 {
15244 li = l->lv_first;
15245 if (li != NULL)
15246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015248 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015249 {
15250 li = li->li_next;
15251 if (li == NULL)
15252 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015253 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015254 if (domax ? i > n : i < n)
15255 n = i;
15256 }
15257 }
15258 }
15259 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015260 else if (argvars[0].v_type == VAR_DICT)
15261 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015262 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015263 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015264 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015265 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015266
15267 d = argvars[0].vval.v_dict;
15268 if (d != NULL)
15269 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015270 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015271 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015272 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015273 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015274 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015275 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015276 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015277 if (first)
15278 {
15279 n = i;
15280 first = FALSE;
15281 }
15282 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015283 n = i;
15284 }
15285 }
15286 }
15287 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015288 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015289 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015290 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015291}
15292
15293/*
15294 * "max()" function
15295 */
15296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015297f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015298{
15299 max_min(argvars, rettv, TRUE);
15300}
15301
15302/*
15303 * "min()" function
15304 */
15305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015306f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015307{
15308 max_min(argvars, rettv, FALSE);
15309}
15310
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015311static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015312
15313/*
15314 * Create the directory in which "dir" is located, and higher levels when
15315 * needed.
15316 */
15317 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015318mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015319{
15320 char_u *p;
15321 char_u *updir;
15322 int r = FAIL;
15323
15324 /* Get end of directory name in "dir".
15325 * We're done when it's "/" or "c:/". */
15326 p = gettail_sep(dir);
15327 if (p <= get_past_head(dir))
15328 return OK;
15329
15330 /* If the directory exists we're done. Otherwise: create it.*/
15331 updir = vim_strnsave(dir, (int)(p - dir));
15332 if (updir == NULL)
15333 return FAIL;
15334 if (mch_isdir(updir))
15335 r = OK;
15336 else if (mkdir_recurse(updir, prot) == OK)
15337 r = vim_mkdir_emsg(updir, prot);
15338 vim_free(updir);
15339 return r;
15340}
15341
15342#ifdef vim_mkdir
15343/*
15344 * "mkdir()" function
15345 */
15346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015347f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015348{
15349 char_u *dir;
15350 char_u buf[NUMBUFLEN];
15351 int prot = 0755;
15352
15353 rettv->vval.v_number = FAIL;
15354 if (check_restricted() || check_secure())
15355 return;
15356
15357 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015358 if (*dir == NUL)
15359 rettv->vval.v_number = FAIL;
15360 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015361 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015362 if (*gettail(dir) == NUL)
15363 /* remove trailing slashes */
15364 *gettail_sep(dir) = NUL;
15365
15366 if (argvars[1].v_type != VAR_UNKNOWN)
15367 {
15368 if (argvars[2].v_type != VAR_UNKNOWN)
15369 prot = get_tv_number_chk(&argvars[2], NULL);
15370 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15371 mkdir_recurse(dir, prot);
15372 }
15373 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015374 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015375}
15376#endif
15377
Bram Moolenaar0d660222005-01-07 21:51:51 +000015378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 * "mode()" function
15380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015382f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015384 char_u buf[3];
15385
15386 buf[1] = NUL;
15387 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 if (VIsual_active)
15390 {
15391 if (VIsual_select)
15392 buf[0] = VIsual_mode + 's' - 'v';
15393 else
15394 buf[0] = VIsual_mode;
15395 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015396 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015397 || State == CONFIRM)
15398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015400 if (State == ASKMORE)
15401 buf[1] = 'm';
15402 else if (State == CONFIRM)
15403 buf[1] = '?';
15404 }
15405 else if (State == EXTERNCMD)
15406 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 else if (State & INSERT)
15408 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015409#ifdef FEAT_VREPLACE
15410 if (State & VREPLACE_FLAG)
15411 {
15412 buf[0] = 'R';
15413 buf[1] = 'v';
15414 }
15415 else
15416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 if (State & REPLACE_FLAG)
15418 buf[0] = 'R';
15419 else
15420 buf[0] = 'i';
15421 }
15422 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015425 if (exmode_active)
15426 buf[1] = 'v';
15427 }
15428 else if (exmode_active)
15429 {
15430 buf[0] = 'c';
15431 buf[1] = 'e';
15432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015436 if (finish_op)
15437 buf[1] = 'o';
15438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015440 /* Clear out the minor mode when the argument is not a non-zero number or
15441 * non-empty string. */
15442 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015443 buf[1] = NUL;
15444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445 rettv->vval.v_string = vim_strsave(buf);
15446 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447}
15448
Bram Moolenaar429fa852013-04-15 12:27:36 +020015449#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015450/*
15451 * "mzeval()" function
15452 */
15453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015454f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015455{
15456 char_u *str;
15457 char_u buf[NUMBUFLEN];
15458
15459 str = get_tv_string_buf(&argvars[0], buf);
15460 do_mzeval(str, rettv);
15461}
Bram Moolenaar75676462013-01-30 14:55:42 +010015462
15463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015464mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015465{
15466 typval_T argvars[3];
15467
15468 argvars[0].v_type = VAR_STRING;
15469 argvars[0].vval.v_string = name;
15470 copy_tv(args, &argvars[1]);
15471 argvars[2].v_type = VAR_UNKNOWN;
15472 f_call(argvars, rettv);
15473 clear_tv(&argvars[1]);
15474}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015475#endif
15476
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015478 * "nextnonblank()" function
15479 */
15480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015481f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015482{
15483 linenr_T lnum;
15484
15485 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015487 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015488 {
15489 lnum = 0;
15490 break;
15491 }
15492 if (*skipwhite(ml_get(lnum)) != NUL)
15493 break;
15494 }
15495 rettv->vval.v_number = lnum;
15496}
15497
15498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499 * "nr2char()" function
15500 */
15501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015502f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503{
15504 char_u buf[NUMBUFLEN];
15505
15506#ifdef FEAT_MBYTE
15507 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015508 {
15509 int utf8 = 0;
15510
15511 if (argvars[1].v_type != VAR_UNKNOWN)
15512 utf8 = get_tv_number_chk(&argvars[1], NULL);
15513 if (utf8)
15514 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15515 else
15516 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518 else
15519#endif
15520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522 buf[1] = NUL;
15523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015524 rettv->v_type = VAR_STRING;
15525 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015526}
15527
15528/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015529 * "or(expr, expr)" function
15530 */
15531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015532f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015533{
15534 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15535 | get_tv_number_chk(&argvars[1], NULL);
15536}
15537
15538/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015539 * "pathshorten()" function
15540 */
15541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015542f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015543{
15544 char_u *p;
15545
15546 rettv->v_type = VAR_STRING;
15547 p = get_tv_string_chk(&argvars[0]);
15548 if (p == NULL)
15549 rettv->vval.v_string = NULL;
15550 else
15551 {
15552 p = vim_strsave(p);
15553 rettv->vval.v_string = p;
15554 if (p != NULL)
15555 shorten_dir(p);
15556 }
15557}
15558
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015559#ifdef FEAT_PERL
15560/*
15561 * "perleval()" function
15562 */
15563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015564f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015565{
15566 char_u *str;
15567 char_u buf[NUMBUFLEN];
15568
15569 str = get_tv_string_buf(&argvars[0], buf);
15570 do_perleval(str, rettv);
15571}
15572#endif
15573
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015574#ifdef FEAT_FLOAT
15575/*
15576 * "pow()" function
15577 */
15578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015579f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015580{
15581 float_T fx, fy;
15582
15583 rettv->v_type = VAR_FLOAT;
15584 if (get_float_arg(argvars, &fx) == OK
15585 && get_float_arg(&argvars[1], &fy) == OK)
15586 rettv->vval.v_float = pow(fx, fy);
15587 else
15588 rettv->vval.v_float = 0.0;
15589}
15590#endif
15591
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015592/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593 * "prevnonblank()" function
15594 */
15595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015596f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597{
15598 linenr_T lnum;
15599
15600 lnum = get_tv_lnum(argvars);
15601 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15602 lnum = 0;
15603 else
15604 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15605 --lnum;
15606 rettv->vval.v_number = lnum;
15607}
15608
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015609/* This dummy va_list is here because:
15610 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15611 * - locally in the function results in a "used before set" warning
15612 * - using va_start() to initialize it gives "function with fixed args" error */
15613static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015614
Bram Moolenaar8c711452005-01-14 21:53:12 +000015615/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015616 * "printf()" function
15617 */
15618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015619f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015620{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015621 char_u buf[NUMBUFLEN];
15622 int len;
15623 char_u *s;
15624 int saved_did_emsg = did_emsg;
15625 char *fmt;
15626
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015627 rettv->v_type = VAR_STRING;
15628 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015629
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015630 /* Get the required length, allocate the buffer and do it for real. */
15631 did_emsg = FALSE;
15632 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15633 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15634 if (!did_emsg)
15635 {
15636 s = alloc(len + 1);
15637 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015638 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015639 rettv->vval.v_string = s;
15640 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015641 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015642 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015643 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015644}
15645
15646/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015647 * "pumvisible()" function
15648 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015650f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015651{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015652#ifdef FEAT_INS_EXPAND
15653 if (pum_visible())
15654 rettv->vval.v_number = 1;
15655#endif
15656}
15657
Bram Moolenaardb913952012-06-29 12:54:53 +020015658#ifdef FEAT_PYTHON3
15659/*
15660 * "py3eval()" function
15661 */
15662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015663f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015664{
15665 char_u *str;
15666 char_u buf[NUMBUFLEN];
15667
15668 str = get_tv_string_buf(&argvars[0], buf);
15669 do_py3eval(str, rettv);
15670}
15671#endif
15672
15673#ifdef FEAT_PYTHON
15674/*
15675 * "pyeval()" function
15676 */
15677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015678f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015679{
15680 char_u *str;
15681 char_u buf[NUMBUFLEN];
15682
15683 str = get_tv_string_buf(&argvars[0], buf);
15684 do_pyeval(str, rettv);
15685}
15686#endif
15687
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015688/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015689 * "range()" function
15690 */
15691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015692f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015693{
15694 long start;
15695 long end;
15696 long stride = 1;
15697 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015698 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015700 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015701 if (argvars[1].v_type == VAR_UNKNOWN)
15702 {
15703 end = start - 1;
15704 start = 0;
15705 }
15706 else
15707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015708 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015709 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015710 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015711 }
15712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015713 if (error)
15714 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015715 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015716 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015717 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015718 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015719 else
15720 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015721 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015722 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015723 if (list_append_number(rettv->vval.v_list,
15724 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015725 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015726 }
15727}
15728
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015729/*
15730 * "readfile()" function
15731 */
15732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015733f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734{
15735 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015736 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015737 char_u *fname;
15738 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015739 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15740 int io_size = sizeof(buf);
15741 int readlen; /* size of last fread() */
15742 char_u *prev = NULL; /* previously read bytes, if any */
15743 long prevlen = 0; /* length of data in prev */
15744 long prevsize = 0; /* size of prev buffer */
15745 long maxline = MAXLNUM;
15746 long cnt = 0;
15747 char_u *p; /* position in buf */
15748 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015749
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015750 if (argvars[1].v_type != VAR_UNKNOWN)
15751 {
15752 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15753 binary = TRUE;
15754 if (argvars[2].v_type != VAR_UNKNOWN)
15755 maxline = get_tv_number(&argvars[2]);
15756 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015757
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015758 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015759 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015760
15761 /* Always open the file in binary mode, library functions have a mind of
15762 * their own about CR-LF conversion. */
15763 fname = get_tv_string(&argvars[0]);
15764 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15765 {
15766 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15767 return;
15768 }
15769
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015770 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015771 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015772 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015773
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015774 /* This for loop processes what was read, but is also entered at end
15775 * of file so that either:
15776 * - an incomplete line gets written
15777 * - a "binary" file gets an empty line at the end if it ends in a
15778 * newline. */
15779 for (p = buf, start = buf;
15780 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15781 ++p)
15782 {
15783 if (*p == '\n' || readlen <= 0)
15784 {
15785 listitem_T *li;
15786 char_u *s = NULL;
15787 long_u len = p - start;
15788
15789 /* Finished a line. Remove CRs before NL. */
15790 if (readlen > 0 && !binary)
15791 {
15792 while (len > 0 && start[len - 1] == '\r')
15793 --len;
15794 /* removal may cross back to the "prev" string */
15795 if (len == 0)
15796 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15797 --prevlen;
15798 }
15799 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015800 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015801 else
15802 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015803 /* Change "prev" buffer to be the right size. This way
15804 * the bytes are only copied once, and very long lines are
15805 * allocated only once. */
15806 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015807 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015808 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015809 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015810 prev = NULL; /* the list will own the string */
15811 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015812 }
15813 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015814 if (s == NULL)
15815 {
15816 do_outofmem_msg((long_u) prevlen + len + 1);
15817 failed = TRUE;
15818 break;
15819 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015820
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015821 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015822 {
15823 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015824 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015825 break;
15826 }
15827 li->li_tv.v_type = VAR_STRING;
15828 li->li_tv.v_lock = 0;
15829 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015830 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015831
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015832 start = p + 1; /* step over newline */
15833 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015834 break;
15835 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015836 else if (*p == NUL)
15837 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015838#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015839 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15840 * when finding the BF and check the previous two bytes. */
15841 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015842 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015843 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15844 * + 1, these may be in the "prev" string. */
15845 char_u back1 = p >= buf + 1 ? p[-1]
15846 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15847 char_u back2 = p >= buf + 2 ? p[-2]
15848 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15849 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015850
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015851 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015852 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015853 char_u *dest = p - 2;
15854
15855 /* Usually a BOM is at the beginning of a file, and so at
15856 * the beginning of a line; then we can just step over it.
15857 */
15858 if (start == dest)
15859 start = p + 1;
15860 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015861 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015862 /* have to shuffle buf to close gap */
15863 int adjust_prevlen = 0;
15864
15865 if (dest < buf)
15866 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015867 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015868 dest = buf;
15869 }
15870 if (readlen > p - buf + 1)
15871 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15872 readlen -= 3 - adjust_prevlen;
15873 prevlen -= adjust_prevlen;
15874 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015875 }
15876 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015877 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015878#endif
15879 } /* for */
15880
15881 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15882 break;
15883 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015884 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015885 /* There's part of a line in buf, store it in "prev". */
15886 if (p - start + prevlen >= prevsize)
15887 {
15888 /* need bigger "prev" buffer */
15889 char_u *newprev;
15890
15891 /* A common use case is ordinary text files and "prev" gets a
15892 * fragment of a line, so the first allocation is made
15893 * small, to avoid repeatedly 'allocing' large and
15894 * 'reallocing' small. */
15895 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015896 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015897 else
15898 {
15899 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015900 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015901 prevsize = grow50pc > growmin ? grow50pc : growmin;
15902 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015903 newprev = prev == NULL ? alloc(prevsize)
15904 : vim_realloc(prev, prevsize);
15905 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015906 {
15907 do_outofmem_msg((long_u)prevsize);
15908 failed = TRUE;
15909 break;
15910 }
15911 prev = newprev;
15912 }
15913 /* Add the line part to end of "prev". */
15914 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015915 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015916 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015917 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015918
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015919 /*
15920 * For a negative line count use only the lines at the end of the file,
15921 * free the rest.
15922 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015923 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015924 while (cnt > -maxline)
15925 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015926 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015927 --cnt;
15928 }
15929
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015930 if (failed)
15931 {
15932 list_free(rettv->vval.v_list, TRUE);
15933 /* readfile doc says an empty list is returned on error */
15934 rettv->vval.v_list = list_alloc();
15935 }
15936
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015937 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015938 fclose(fd);
15939}
15940
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015941#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015942static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015943
15944/*
15945 * Convert a List to proftime_T.
15946 * Return FAIL when there is something wrong.
15947 */
15948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015949list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015950{
15951 long n1, n2;
15952 int error = FALSE;
15953
15954 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15955 || arg->vval.v_list->lv_len != 2)
15956 return FAIL;
15957 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15958 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15959# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015960 tm->HighPart = n1;
15961 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015962# else
15963 tm->tv_sec = n1;
15964 tm->tv_usec = n2;
15965# endif
15966 return error ? FAIL : OK;
15967}
15968#endif /* FEAT_RELTIME */
15969
15970/*
15971 * "reltime()" function
15972 */
15973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015974f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015975{
15976#ifdef FEAT_RELTIME
15977 proftime_T res;
15978 proftime_T start;
15979
15980 if (argvars[0].v_type == VAR_UNKNOWN)
15981 {
15982 /* No arguments: get current time. */
15983 profile_start(&res);
15984 }
15985 else if (argvars[1].v_type == VAR_UNKNOWN)
15986 {
15987 if (list2proftime(&argvars[0], &res) == FAIL)
15988 return;
15989 profile_end(&res);
15990 }
15991 else
15992 {
15993 /* Two arguments: compute the difference. */
15994 if (list2proftime(&argvars[0], &start) == FAIL
15995 || list2proftime(&argvars[1], &res) == FAIL)
15996 return;
15997 profile_sub(&res, &start);
15998 }
15999
16000 if (rettv_list_alloc(rettv) == OK)
16001 {
16002 long n1, n2;
16003
16004# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016005 n1 = res.HighPart;
16006 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016007# else
16008 n1 = res.tv_sec;
16009 n2 = res.tv_usec;
16010# endif
16011 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16012 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16013 }
16014#endif
16015}
16016
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016017#ifdef FEAT_FLOAT
16018/*
16019 * "reltimefloat()" function
16020 */
16021 static void
16022f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16023{
16024# ifdef FEAT_RELTIME
16025 proftime_T tm;
16026# endif
16027
16028 rettv->v_type = VAR_FLOAT;
16029 rettv->vval.v_float = 0;
16030# ifdef FEAT_RELTIME
16031 if (list2proftime(&argvars[0], &tm) == OK)
16032 rettv->vval.v_float = profile_float(&tm);
16033# endif
16034}
16035#endif
16036
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016037/*
16038 * "reltimestr()" function
16039 */
16040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016041f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016042{
16043#ifdef FEAT_RELTIME
16044 proftime_T tm;
16045#endif
16046
16047 rettv->v_type = VAR_STRING;
16048 rettv->vval.v_string = NULL;
16049#ifdef FEAT_RELTIME
16050 if (list2proftime(&argvars[0], &tm) == OK)
16051 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16052#endif
16053}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016054
Bram Moolenaar0d660222005-01-07 21:51:51 +000016055#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016056static void make_connection(void);
16057static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058
16059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016060make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016061{
16062 if (X_DISPLAY == NULL
16063# ifdef FEAT_GUI
16064 && !gui.in_use
16065# endif
16066 )
16067 {
16068 x_force_connect = TRUE;
16069 setup_term_clip();
16070 x_force_connect = FALSE;
16071 }
16072}
16073
16074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016075check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016076{
16077 make_connection();
16078 if (X_DISPLAY == NULL)
16079 {
16080 EMSG(_("E240: No connection to Vim server"));
16081 return FAIL;
16082 }
16083 return OK;
16084}
16085#endif
16086
16087#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016088static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016089
16090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016091remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016092{
16093 char_u *server_name;
16094 char_u *keys;
16095 char_u *r = NULL;
16096 char_u buf[NUMBUFLEN];
16097# ifdef WIN32
16098 HWND w;
16099# else
16100 Window w;
16101# endif
16102
16103 if (check_restricted() || check_secure())
16104 return;
16105
16106# ifdef FEAT_X11
16107 if (check_connection() == FAIL)
16108 return;
16109# endif
16110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111 server_name = get_tv_string_chk(&argvars[0]);
16112 if (server_name == NULL)
16113 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016114 keys = get_tv_string_buf(&argvars[1], buf);
16115# ifdef WIN32
16116 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16117# else
16118 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16119 < 0)
16120# endif
16121 {
16122 if (r != NULL)
16123 EMSG(r); /* sending worked but evaluation failed */
16124 else
16125 EMSG2(_("E241: Unable to send to %s"), server_name);
16126 return;
16127 }
16128
16129 rettv->vval.v_string = r;
16130
16131 if (argvars[2].v_type != VAR_UNKNOWN)
16132 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016133 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016134 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016135 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016137 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 v.di_tv.v_type = VAR_STRING;
16139 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016140 idvar = get_tv_string_chk(&argvars[2]);
16141 if (idvar != NULL)
16142 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016143 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016144 }
16145}
16146#endif
16147
16148/*
16149 * "remote_expr()" function
16150 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016152f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016153{
16154 rettv->v_type = VAR_STRING;
16155 rettv->vval.v_string = NULL;
16156#ifdef FEAT_CLIENTSERVER
16157 remote_common(argvars, rettv, TRUE);
16158#endif
16159}
16160
16161/*
16162 * "remote_foreground()" function
16163 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016165f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016166{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167#ifdef FEAT_CLIENTSERVER
16168# ifdef WIN32
16169 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016170 {
16171 char_u *server_name = get_tv_string_chk(&argvars[0]);
16172
16173 if (server_name != NULL)
16174 serverForeground(server_name);
16175 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016176# else
16177 /* Send a foreground() expression to the server. */
16178 argvars[1].v_type = VAR_STRING;
16179 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16180 argvars[2].v_type = VAR_UNKNOWN;
16181 remote_common(argvars, rettv, TRUE);
16182 vim_free(argvars[1].vval.v_string);
16183# endif
16184#endif
16185}
16186
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016188f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189{
16190#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016191 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016192 char_u *s = NULL;
16193# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016194 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016196 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197
16198 if (check_restricted() || check_secure())
16199 {
16200 rettv->vval.v_number = -1;
16201 return;
16202 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016203 serverid = get_tv_string_chk(&argvars[0]);
16204 if (serverid == NULL)
16205 {
16206 rettv->vval.v_number = -1;
16207 return; /* type error; errmsg already given */
16208 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016209# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016210 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016211 if (n == 0)
16212 rettv->vval.v_number = -1;
16213 else
16214 {
16215 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16216 rettv->vval.v_number = (s != NULL);
16217 }
16218# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 if (check_connection() == FAIL)
16220 return;
16221
16222 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016223 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016224# endif
16225
16226 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016228 char_u *retvar;
16229
Bram Moolenaar33570922005-01-25 22:26:29 +000016230 v.di_tv.v_type = VAR_STRING;
16231 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016232 retvar = get_tv_string_chk(&argvars[1]);
16233 if (retvar != NULL)
16234 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016235 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016236 }
16237#else
16238 rettv->vval.v_number = -1;
16239#endif
16240}
16241
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016243f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244{
16245 char_u *r = NULL;
16246
16247#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016248 char_u *serverid = get_tv_string_chk(&argvars[0]);
16249
16250 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251 {
16252# ifdef WIN32
16253 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016254 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016256 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257 if (n != 0)
16258 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16259 if (r == NULL)
16260# else
16261 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016263# endif
16264 EMSG(_("E277: Unable to read a server reply"));
16265 }
16266#endif
16267 rettv->v_type = VAR_STRING;
16268 rettv->vval.v_string = r;
16269}
16270
16271/*
16272 * "remote_send()" function
16273 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016275f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016276{
16277 rettv->v_type = VAR_STRING;
16278 rettv->vval.v_string = NULL;
16279#ifdef FEAT_CLIENTSERVER
16280 remote_common(argvars, rettv, FALSE);
16281#endif
16282}
16283
16284/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016285 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016286 */
16287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016288f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016289{
Bram Moolenaar33570922005-01-25 22:26:29 +000016290 list_T *l;
16291 listitem_T *item, *item2;
16292 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016293 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016294 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016295 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016296 dict_T *d;
16297 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016298 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016299
Bram Moolenaar8c711452005-01-14 21:53:12 +000016300 if (argvars[0].v_type == VAR_DICT)
16301 {
16302 if (argvars[2].v_type != VAR_UNKNOWN)
16303 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016304 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016305 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016307 key = get_tv_string_chk(&argvars[1]);
16308 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016310 di = dict_find(d, key, -1);
16311 if (di == NULL)
16312 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016313 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16314 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016315 {
16316 *rettv = di->di_tv;
16317 init_tv(&di->di_tv);
16318 dictitem_remove(d, di);
16319 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016320 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016321 }
16322 }
16323 else if (argvars[0].v_type != VAR_LIST)
16324 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016325 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016326 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016328 int error = FALSE;
16329
16330 idx = get_tv_number_chk(&argvars[1], &error);
16331 if (error)
16332 ; /* type error: do nothing, errmsg already given */
16333 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016334 EMSGN(_(e_listidx), idx);
16335 else
16336 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016337 if (argvars[2].v_type == VAR_UNKNOWN)
16338 {
16339 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016340 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016341 *rettv = item->li_tv;
16342 vim_free(item);
16343 }
16344 else
16345 {
16346 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016347 end = get_tv_number_chk(&argvars[2], &error);
16348 if (error)
16349 ; /* type error: do nothing */
16350 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016351 EMSGN(_(e_listidx), end);
16352 else
16353 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016354 int cnt = 0;
16355
16356 for (li = item; li != NULL; li = li->li_next)
16357 {
16358 ++cnt;
16359 if (li == item2)
16360 break;
16361 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016362 if (li == NULL) /* didn't find "item2" after "item" */
16363 EMSG(_(e_invrange));
16364 else
16365 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016366 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016367 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016368 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016369 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016370 l->lv_first = item;
16371 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016372 item->li_prev = NULL;
16373 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016374 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016375 }
16376 }
16377 }
16378 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016379 }
16380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381}
16382
16383/*
16384 * "rename({from}, {to})" function
16385 */
16386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016387f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388{
16389 char_u buf[NUMBUFLEN];
16390
16391 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016392 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016394 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16395 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396}
16397
16398/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016399 * "repeat()" function
16400 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016402f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016403{
16404 char_u *p;
16405 int n;
16406 int slen;
16407 int len;
16408 char_u *r;
16409 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016410
16411 n = get_tv_number(&argvars[1]);
16412 if (argvars[0].v_type == VAR_LIST)
16413 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016414 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016415 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016416 if (list_extend(rettv->vval.v_list,
16417 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016418 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016419 }
16420 else
16421 {
16422 p = get_tv_string(&argvars[0]);
16423 rettv->v_type = VAR_STRING;
16424 rettv->vval.v_string = NULL;
16425
16426 slen = (int)STRLEN(p);
16427 len = slen * n;
16428 if (len <= 0)
16429 return;
16430
16431 r = alloc(len + 1);
16432 if (r != NULL)
16433 {
16434 for (i = 0; i < n; i++)
16435 mch_memmove(r + i * slen, p, (size_t)slen);
16436 r[len] = NUL;
16437 }
16438
16439 rettv->vval.v_string = r;
16440 }
16441}
16442
16443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 * "resolve()" function
16445 */
16446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016447f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448{
16449 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016450#ifdef HAVE_READLINK
16451 char_u *buf = NULL;
16452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016454 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455#ifdef FEAT_SHORTCUT
16456 {
16457 char_u *v = NULL;
16458
16459 v = mch_resolve_shortcut(p);
16460 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016461 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016463 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 }
16465#else
16466# ifdef HAVE_READLINK
16467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 char_u *cpy;
16469 int len;
16470 char_u *remain = NULL;
16471 char_u *q;
16472 int is_relative_to_current = FALSE;
16473 int has_trailing_pathsep = FALSE;
16474 int limit = 100;
16475
16476 p = vim_strsave(p);
16477
16478 if (p[0] == '.' && (vim_ispathsep(p[1])
16479 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16480 is_relative_to_current = TRUE;
16481
16482 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016483 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016486 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488
16489 q = getnextcomp(p);
16490 if (*q != NUL)
16491 {
16492 /* Separate the first path component in "p", and keep the
16493 * remainder (beginning with the path separator). */
16494 remain = vim_strsave(q - 1);
16495 q[-1] = NUL;
16496 }
16497
Bram Moolenaard9462e32011-04-11 21:35:11 +020016498 buf = alloc(MAXPATHL + 1);
16499 if (buf == NULL)
16500 goto fail;
16501
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502 for (;;)
16503 {
16504 for (;;)
16505 {
16506 len = readlink((char *)p, (char *)buf, MAXPATHL);
16507 if (len <= 0)
16508 break;
16509 buf[len] = NUL;
16510
16511 if (limit-- == 0)
16512 {
16513 vim_free(p);
16514 vim_free(remain);
16515 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016516 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 goto fail;
16518 }
16519
16520 /* Ensure that the result will have a trailing path separator
16521 * if the argument has one. */
16522 if (remain == NULL && has_trailing_pathsep)
16523 add_pathsep(buf);
16524
16525 /* Separate the first path component in the link value and
16526 * concatenate the remainders. */
16527 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16528 if (*q != NUL)
16529 {
16530 if (remain == NULL)
16531 remain = vim_strsave(q - 1);
16532 else
16533 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016534 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 if (cpy != NULL)
16536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 vim_free(remain);
16538 remain = cpy;
16539 }
16540 }
16541 q[-1] = NUL;
16542 }
16543
16544 q = gettail(p);
16545 if (q > p && *q == NUL)
16546 {
16547 /* Ignore trailing path separator. */
16548 q[-1] = NUL;
16549 q = gettail(p);
16550 }
16551 if (q > p && !mch_isFullName(buf))
16552 {
16553 /* symlink is relative to directory of argument */
16554 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16555 if (cpy != NULL)
16556 {
16557 STRCPY(cpy, p);
16558 STRCPY(gettail(cpy), buf);
16559 vim_free(p);
16560 p = cpy;
16561 }
16562 }
16563 else
16564 {
16565 vim_free(p);
16566 p = vim_strsave(buf);
16567 }
16568 }
16569
16570 if (remain == NULL)
16571 break;
16572
16573 /* Append the first path component of "remain" to "p". */
16574 q = getnextcomp(remain + 1);
16575 len = q - remain - (*q != NUL);
16576 cpy = vim_strnsave(p, STRLEN(p) + len);
16577 if (cpy != NULL)
16578 {
16579 STRNCAT(cpy, remain, len);
16580 vim_free(p);
16581 p = cpy;
16582 }
16583 /* Shorten "remain". */
16584 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016585 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 else
16587 {
16588 vim_free(remain);
16589 remain = NULL;
16590 }
16591 }
16592
16593 /* If the result is a relative path name, make it explicitly relative to
16594 * the current directory if and only if the argument had this form. */
16595 if (!vim_ispathsep(*p))
16596 {
16597 if (is_relative_to_current
16598 && *p != NUL
16599 && !(p[0] == '.'
16600 && (p[1] == NUL
16601 || vim_ispathsep(p[1])
16602 || (p[1] == '.'
16603 && (p[2] == NUL
16604 || vim_ispathsep(p[2]))))))
16605 {
16606 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016607 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 if (cpy != NULL)
16609 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610 vim_free(p);
16611 p = cpy;
16612 }
16613 }
16614 else if (!is_relative_to_current)
16615 {
16616 /* Strip leading "./". */
16617 q = p;
16618 while (q[0] == '.' && vim_ispathsep(q[1]))
16619 q += 2;
16620 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016621 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 }
16623 }
16624
16625 /* Ensure that the result will have no trailing path separator
16626 * if the argument had none. But keep "/" or "//". */
16627 if (!has_trailing_pathsep)
16628 {
16629 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016630 if (after_pathsep(p, q))
16631 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 }
16633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 }
16636# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638# endif
16639#endif
16640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016641 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642
16643#ifdef HAVE_READLINK
16644fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016645 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016647 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648}
16649
16650/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016651 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 */
16653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016654f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655{
Bram Moolenaar33570922005-01-25 22:26:29 +000016656 list_T *l;
16657 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658
Bram Moolenaar0d660222005-01-07 21:51:51 +000016659 if (argvars[0].v_type != VAR_LIST)
16660 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016661 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016662 && !tv_check_lock(l->lv_lock,
16663 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016664 {
16665 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016666 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016667 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016668 while (li != NULL)
16669 {
16670 ni = li->li_prev;
16671 list_append(l, li);
16672 li = ni;
16673 }
16674 rettv->vval.v_list = l;
16675 rettv->v_type = VAR_LIST;
16676 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016677 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679}
16680
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016681#define SP_NOMOVE 0x01 /* don't move cursor */
16682#define SP_REPEAT 0x02 /* repeat to find outer pair */
16683#define SP_RETCOUNT 0x04 /* return matchcount */
16684#define SP_SETPCMARK 0x08 /* set previous context mark */
16685#define SP_START 0x10 /* accept match at start position */
16686#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16687#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016688#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016689
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016690static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691
16692/*
16693 * Get flags for a search function.
16694 * Possibly sets "p_ws".
16695 * Returns BACKWARD, FORWARD or zero (for an error).
16696 */
16697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016698get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016699{
16700 int dir = FORWARD;
16701 char_u *flags;
16702 char_u nbuf[NUMBUFLEN];
16703 int mask;
16704
16705 if (varp->v_type != VAR_UNKNOWN)
16706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016707 flags = get_tv_string_buf_chk(varp, nbuf);
16708 if (flags == NULL)
16709 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016710 while (*flags != NUL)
16711 {
16712 switch (*flags)
16713 {
16714 case 'b': dir = BACKWARD; break;
16715 case 'w': p_ws = TRUE; break;
16716 case 'W': p_ws = FALSE; break;
16717 default: mask = 0;
16718 if (flagsp != NULL)
16719 switch (*flags)
16720 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016721 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016722 case 'e': mask = SP_END; break;
16723 case 'm': mask = SP_RETCOUNT; break;
16724 case 'n': mask = SP_NOMOVE; break;
16725 case 'p': mask = SP_SUBPAT; break;
16726 case 'r': mask = SP_REPEAT; break;
16727 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016728 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 }
16730 if (mask == 0)
16731 {
16732 EMSG2(_(e_invarg2), flags);
16733 dir = 0;
16734 }
16735 else
16736 *flagsp |= mask;
16737 }
16738 if (dir == 0)
16739 break;
16740 ++flags;
16741 }
16742 }
16743 return dir;
16744}
16745
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016747 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016749 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016750search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016752 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 char_u *pat;
16754 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016755 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 int save_p_ws = p_ws;
16757 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016758 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016759 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016760 proftime_T tm;
16761#ifdef FEAT_RELTIME
16762 long time_limit = 0;
16763#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016764 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016765 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016767 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016768 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016769 if (dir == 0)
16770 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016771 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016772 if (flags & SP_START)
16773 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016774 if (flags & SP_END)
16775 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016776 if (flags & SP_COLUMN)
16777 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016778
Bram Moolenaar76929292008-01-06 19:07:36 +000016779 /* Optional arguments: line number to stop searching and timeout. */
16780 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016781 {
16782 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16783 if (lnum_stop < 0)
16784 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016785#ifdef FEAT_RELTIME
16786 if (argvars[3].v_type != VAR_UNKNOWN)
16787 {
16788 time_limit = get_tv_number_chk(&argvars[3], NULL);
16789 if (time_limit < 0)
16790 goto theend;
16791 }
16792#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016793 }
16794
Bram Moolenaar76929292008-01-06 19:07:36 +000016795#ifdef FEAT_RELTIME
16796 /* Set the time limit, if there is one. */
16797 profile_setlimit(time_limit, &tm);
16798#endif
16799
Bram Moolenaar231334e2005-07-25 20:46:57 +000016800 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016801 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016802 * Check to make sure only those flags are set.
16803 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16804 * flags cannot be set. Check for that condition also.
16805 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016806 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016807 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016809 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016810 goto theend;
16811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016813 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016814 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016815 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016816 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016818 if (flags & SP_SUBPAT)
16819 retval = subpatnum;
16820 else
16821 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016822 if (flags & SP_SETPCMARK)
16823 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016825 if (match_pos != NULL)
16826 {
16827 /* Store the match cursor position */
16828 match_pos->lnum = pos.lnum;
16829 match_pos->col = pos.col + 1;
16830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 /* "/$" will put the cursor after the end of the line, may need to
16832 * correct that here */
16833 check_cursor();
16834 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016835
16836 /* If 'n' flag is used: restore cursor position. */
16837 if (flags & SP_NOMOVE)
16838 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016839 else
16840 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016841theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016843
16844 return retval;
16845}
16846
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016847#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016848
16849/*
16850 * round() is not in C90, use ceil() or floor() instead.
16851 */
16852 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016853vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016854{
16855 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16856}
16857
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016858/*
16859 * "round({float})" function
16860 */
16861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016862f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016863{
16864 float_T f;
16865
16866 rettv->v_type = VAR_FLOAT;
16867 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016868 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016869 else
16870 rettv->vval.v_float = 0.0;
16871}
16872#endif
16873
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016874/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016875 * "screenattr()" function
16876 */
16877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016878f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016879{
16880 int row;
16881 int col;
16882 int c;
16883
16884 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16885 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16886 if (row < 0 || row >= screen_Rows
16887 || col < 0 || col >= screen_Columns)
16888 c = -1;
16889 else
16890 c = ScreenAttrs[LineOffset[row] + col];
16891 rettv->vval.v_number = c;
16892}
16893
16894/*
16895 * "screenchar()" function
16896 */
16897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016898f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016899{
16900 int row;
16901 int col;
16902 int off;
16903 int c;
16904
16905 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16906 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16907 if (row < 0 || row >= screen_Rows
16908 || col < 0 || col >= screen_Columns)
16909 c = -1;
16910 else
16911 {
16912 off = LineOffset[row] + col;
16913#ifdef FEAT_MBYTE
16914 if (enc_utf8 && ScreenLinesUC[off] != 0)
16915 c = ScreenLinesUC[off];
16916 else
16917#endif
16918 c = ScreenLines[off];
16919 }
16920 rettv->vval.v_number = c;
16921}
16922
16923/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016924 * "screencol()" function
16925 *
16926 * First column is 1 to be consistent with virtcol().
16927 */
16928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016929f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016930{
16931 rettv->vval.v_number = screen_screencol() + 1;
16932}
16933
16934/*
16935 * "screenrow()" function
16936 */
16937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016938f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016939{
16940 rettv->vval.v_number = screen_screenrow() + 1;
16941}
16942
16943/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016944 * "search()" function
16945 */
16946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016947f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016948{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016949 int flags = 0;
16950
16951 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952}
16953
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016955 * "searchdecl()" function
16956 */
16957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016958f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016959{
16960 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016961 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016962 int error = FALSE;
16963 char_u *name;
16964
16965 rettv->vval.v_number = 1; /* default: FAIL */
16966
16967 name = get_tv_string_chk(&argvars[0]);
16968 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016969 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016970 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016971 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16972 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16973 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016974 if (!error && name != NULL)
16975 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016976 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016977}
16978
16979/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016980 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016983searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984{
16985 char_u *spat, *mpat, *epat;
16986 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 int dir;
16989 int flags = 0;
16990 char_u nbuf1[NUMBUFLEN];
16991 char_u nbuf2[NUMBUFLEN];
16992 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016993 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016994 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016995 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016998 spat = get_tv_string_chk(&argvars[0]);
16999 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17000 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17001 if (spat == NULL || mpat == NULL || epat == NULL)
17002 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 /* Handle the optional fourth argument: flags */
17005 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017006 if (dir == 0)
17007 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017008
17009 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017010 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17011 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017012 if ((flags & (SP_END | SP_SUBPAT)) != 0
17013 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017014 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017015 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017016 goto theend;
17017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017019 /* Using 'r' implies 'W', otherwise it doesn't work. */
17020 if (flags & SP_REPEAT)
17021 p_ws = FALSE;
17022
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017023 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017024 if (argvars[3].v_type == VAR_UNKNOWN
17025 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 skip = (char_u *)"";
17027 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017030 if (argvars[5].v_type != VAR_UNKNOWN)
17031 {
17032 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17033 if (lnum_stop < 0)
17034 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017035#ifdef FEAT_RELTIME
17036 if (argvars[6].v_type != VAR_UNKNOWN)
17037 {
17038 time_limit = get_tv_number_chk(&argvars[6], NULL);
17039 if (time_limit < 0)
17040 goto theend;
17041 }
17042#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017043 }
17044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045 if (skip == NULL)
17046 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017048 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017049 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017050
17051theend:
17052 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017053
17054 return retval;
17055}
17056
17057/*
17058 * "searchpair()" function
17059 */
17060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017061f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017062{
17063 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17064}
17065
17066/*
17067 * "searchpairpos()" function
17068 */
17069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017070f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017071{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017072 pos_T match_pos;
17073 int lnum = 0;
17074 int col = 0;
17075
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017076 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017077 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017078
17079 if (searchpair_cmn(argvars, &match_pos) > 0)
17080 {
17081 lnum = match_pos.lnum;
17082 col = match_pos.col;
17083 }
17084
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017085 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17086 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017087}
17088
17089/*
17090 * Search for a start/middle/end thing.
17091 * Used by searchpair(), see its documentation for the details.
17092 * Returns 0 or -1 for no match,
17093 */
17094 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017095do_searchpair(
17096 char_u *spat, /* start pattern */
17097 char_u *mpat, /* middle pattern */
17098 char_u *epat, /* end pattern */
17099 int dir, /* BACKWARD or FORWARD */
17100 char_u *skip, /* skip expression */
17101 int flags, /* SP_SETPCMARK and other SP_ values */
17102 pos_T *match_pos,
17103 linenr_T lnum_stop, /* stop at this line if not zero */
17104 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017105{
17106 char_u *save_cpo;
17107 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17108 long retval = 0;
17109 pos_T pos;
17110 pos_T firstpos;
17111 pos_T foundpos;
17112 pos_T save_cursor;
17113 pos_T save_pos;
17114 int n;
17115 int r;
17116 int nest = 1;
17117 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017118 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017119 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017120
17121 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17122 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017123 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017124
Bram Moolenaar76929292008-01-06 19:07:36 +000017125#ifdef FEAT_RELTIME
17126 /* Set the time limit, if there is one. */
17127 profile_setlimit(time_limit, &tm);
17128#endif
17129
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017130 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17131 * start/middle/end (pat3, for the top pair). */
17132 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17133 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17134 if (pat2 == NULL || pat3 == NULL)
17135 goto theend;
17136 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17137 if (*mpat == NUL)
17138 STRCPY(pat3, pat2);
17139 else
17140 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17141 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017142 if (flags & SP_START)
17143 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017144
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 save_cursor = curwin->w_cursor;
17146 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017147 clearpos(&firstpos);
17148 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 pat = pat3;
17150 for (;;)
17151 {
17152 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017153 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17155 /* didn't find it or found the first match again: FAIL */
17156 break;
17157
17158 if (firstpos.lnum == 0)
17159 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017160 if (equalpos(pos, foundpos))
17161 {
17162 /* Found the same position again. Can happen with a pattern that
17163 * has "\zs" at the end and searching backwards. Advance one
17164 * character and try again. */
17165 if (dir == BACKWARD)
17166 decl(&pos);
17167 else
17168 incl(&pos);
17169 }
17170 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017172 /* clear the start flag to avoid getting stuck here */
17173 options &= ~SEARCH_START;
17174
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 /* If the skip pattern matches, ignore this match. */
17176 if (*skip != NUL)
17177 {
17178 save_pos = curwin->w_cursor;
17179 curwin->w_cursor = pos;
17180 r = eval_to_bool(skip, &err, NULL, FALSE);
17181 curwin->w_cursor = save_pos;
17182 if (err)
17183 {
17184 /* Evaluating {skip} caused an error, break here. */
17185 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017186 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 break;
17188 }
17189 if (r)
17190 continue;
17191 }
17192
17193 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17194 {
17195 /* Found end when searching backwards or start when searching
17196 * forward: nested pair. */
17197 ++nest;
17198 pat = pat2; /* nested, don't search for middle */
17199 }
17200 else
17201 {
17202 /* Found end when searching forward or start when searching
17203 * backward: end of (nested) pair; or found middle in outer pair. */
17204 if (--nest == 1)
17205 pat = pat3; /* outer level, search for middle */
17206 }
17207
17208 if (nest == 0)
17209 {
17210 /* Found the match: return matchcount or line number. */
17211 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017212 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017214 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017215 if (flags & SP_SETPCMARK)
17216 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217 curwin->w_cursor = pos;
17218 if (!(flags & SP_REPEAT))
17219 break;
17220 nest = 1; /* search for next unmatched */
17221 }
17222 }
17223
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017224 if (match_pos != NULL)
17225 {
17226 /* Store the match cursor position */
17227 match_pos->lnum = curwin->w_cursor.lnum;
17228 match_pos->col = curwin->w_cursor.col + 1;
17229 }
17230
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017232 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 curwin->w_cursor = save_cursor;
17234
17235theend:
17236 vim_free(pat2);
17237 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017238 if (p_cpo == empty_option)
17239 p_cpo = save_cpo;
17240 else
17241 /* Darn, evaluating the {skip} expression changed the value. */
17242 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017243
17244 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245}
17246
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017247/*
17248 * "searchpos()" function
17249 */
17250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017251f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017252{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017253 pos_T match_pos;
17254 int lnum = 0;
17255 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017256 int n;
17257 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017258
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017259 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017260 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017261
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017262 n = search_cmn(argvars, &match_pos, &flags);
17263 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017264 {
17265 lnum = match_pos.lnum;
17266 col = match_pos.col;
17267 }
17268
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017269 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17270 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017271 if (flags & SP_SUBPAT)
17272 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017273}
17274
Bram Moolenaar0d660222005-01-07 21:51:51 +000017275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017276f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017278#ifdef FEAT_CLIENTSERVER
17279 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017280 char_u *server = get_tv_string_chk(&argvars[0]);
17281 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282
Bram Moolenaar0d660222005-01-07 21:51:51 +000017283 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017284 if (server == NULL || reply == NULL)
17285 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017286 if (check_restricted() || check_secure())
17287 return;
17288# ifdef FEAT_X11
17289 if (check_connection() == FAIL)
17290 return;
17291# endif
17292
17293 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017295 EMSG(_("E258: Unable to send to client"));
17296 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017298 rettv->vval.v_number = 0;
17299#else
17300 rettv->vval.v_number = -1;
17301#endif
17302}
17303
Bram Moolenaar0d660222005-01-07 21:51:51 +000017304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017305f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017306{
17307 char_u *r = NULL;
17308
17309#ifdef FEAT_CLIENTSERVER
17310# ifdef WIN32
17311 r = serverGetVimNames();
17312# else
17313 make_connection();
17314 if (X_DISPLAY != NULL)
17315 r = serverGetVimNames(X_DISPLAY);
17316# endif
17317#endif
17318 rettv->v_type = VAR_STRING;
17319 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320}
17321
17322/*
17323 * "setbufvar()" function
17324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017326f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327{
17328 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017331 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 char_u nbuf[NUMBUFLEN];
17333
17334 if (check_restricted() || check_secure())
17335 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017336 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17337 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017338 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 varp = &argvars[2];
17340
17341 if (buf != NULL && varname != NULL && varp != NULL)
17342 {
17343 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345
17346 if (*varname == '&')
17347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017348 long numval;
17349 char_u *strval;
17350 int error = FALSE;
17351
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017353 numval = get_tv_number_chk(varp, &error);
17354 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017355 if (!error && strval != NULL)
17356 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 }
17358 else
17359 {
17360 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17361 if (bufvarname != NULL)
17362 {
17363 STRCPY(bufvarname, "b:");
17364 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017365 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 vim_free(bufvarname);
17367 }
17368 }
17369
17370 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373}
17374
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017376f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017377{
17378 dict_T *d;
17379 dictitem_T *di;
17380 char_u *csearch;
17381
17382 if (argvars[0].v_type != VAR_DICT)
17383 {
17384 EMSG(_(e_dictreq));
17385 return;
17386 }
17387
17388 if ((d = argvars[0].vval.v_dict) != NULL)
17389 {
17390 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17391 if (csearch != NULL)
17392 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017393#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017394 if (enc_utf8)
17395 {
17396 int pcc[MAX_MCO];
17397 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017398
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017399 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17400 }
17401 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017402#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017403 set_last_csearch(PTR2CHAR(csearch),
17404 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017405 }
17406
17407 di = dict_find(d, (char_u *)"forward", -1);
17408 if (di != NULL)
17409 set_csearch_direction(get_tv_number(&di->di_tv)
17410 ? FORWARD : BACKWARD);
17411
17412 di = dict_find(d, (char_u *)"until", -1);
17413 if (di != NULL)
17414 set_csearch_until(!!get_tv_number(&di->di_tv));
17415 }
17416}
17417
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418/*
17419 * "setcmdpos()" function
17420 */
17421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017422f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017424 int pos = (int)get_tv_number(&argvars[0]) - 1;
17425
17426 if (pos >= 0)
17427 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428}
17429
17430/*
17431 * "setline()" function
17432 */
17433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017434f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435{
17436 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017437 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017438 list_T *l = NULL;
17439 listitem_T *li = NULL;
17440 long added = 0;
17441 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017443 lnum = get_tv_lnum(&argvars[0]);
17444 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017446 l = argvars[1].vval.v_list;
17447 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017449 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017450 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017451
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017452 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017453 for (;;)
17454 {
17455 if (l != NULL)
17456 {
17457 /* list argument, get next string */
17458 if (li == NULL)
17459 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017460 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017461 li = li->li_next;
17462 }
17463
17464 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017465 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017466 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017467
17468 /* When coming here from Insert mode, sync undo, so that this can be
17469 * undone separately from what was previously inserted. */
17470 if (u_sync_once == 2)
17471 {
17472 u_sync_once = 1; /* notify that u_sync() was called */
17473 u_sync(TRUE);
17474 }
17475
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017476 if (lnum <= curbuf->b_ml.ml_line_count)
17477 {
17478 /* existing line, replace it */
17479 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17480 {
17481 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017482 if (lnum == curwin->w_cursor.lnum)
17483 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017484 rettv->vval.v_number = 0; /* OK */
17485 }
17486 }
17487 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17488 {
17489 /* lnum is one past the last line, append the line */
17490 ++added;
17491 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17492 rettv->vval.v_number = 0; /* OK */
17493 }
17494
17495 if (l == NULL) /* only one string argument */
17496 break;
17497 ++lnum;
17498 }
17499
17500 if (added > 0)
17501 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502}
17503
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017504static 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 +000017505
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017507 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017508 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017510set_qf_ll_list(
17511 win_T *wp UNUSED,
17512 typval_T *list_arg UNUSED,
17513 typval_T *action_arg UNUSED,
17514 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017515{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017516#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017517 char_u *act;
17518 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017519#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017520
Bram Moolenaar2641f772005-03-25 21:58:17 +000017521 rettv->vval.v_number = -1;
17522
17523#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017524 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017525 EMSG(_(e_listreq));
17526 else
17527 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017528 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017529
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017530 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017531 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017532 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017533 if (act == NULL)
17534 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017535 if (*act == 'a' || *act == 'r')
17536 action = *act;
17537 }
17538
Bram Moolenaar81484f42012-12-05 15:16:47 +010017539 if (l != NULL && set_errorlist(wp, l, action,
17540 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017541 rettv->vval.v_number = 0;
17542 }
17543#endif
17544}
17545
17546/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017547 * "setloclist()" function
17548 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017550f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017551{
17552 win_T *win;
17553
17554 rettv->vval.v_number = -1;
17555
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017556 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017557 if (win != NULL)
17558 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17559}
17560
17561/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017562 * "setmatches()" function
17563 */
17564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017565f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017566{
17567#ifdef FEAT_SEARCH_EXTRA
17568 list_T *l;
17569 listitem_T *li;
17570 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017571 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017572
17573 rettv->vval.v_number = -1;
17574 if (argvars[0].v_type != VAR_LIST)
17575 {
17576 EMSG(_(e_listreq));
17577 return;
17578 }
17579 if ((l = argvars[0].vval.v_list) != NULL)
17580 {
17581
17582 /* To some extent make sure that we are dealing with a list from
17583 * "getmatches()". */
17584 li = l->lv_first;
17585 while (li != NULL)
17586 {
17587 if (li->li_tv.v_type != VAR_DICT
17588 || (d = li->li_tv.vval.v_dict) == NULL)
17589 {
17590 EMSG(_(e_invarg));
17591 return;
17592 }
17593 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017594 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17595 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017596 && dict_find(d, (char_u *)"priority", -1) != NULL
17597 && dict_find(d, (char_u *)"id", -1) != NULL))
17598 {
17599 EMSG(_(e_invarg));
17600 return;
17601 }
17602 li = li->li_next;
17603 }
17604
17605 clear_matches(curwin);
17606 li = l->lv_first;
17607 while (li != NULL)
17608 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017609 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017610 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017611 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017612 char_u *group;
17613 int priority;
17614 int id;
17615 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017616
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017617 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017618 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17619 {
17620 if (s == NULL)
17621 {
17622 s = list_alloc();
17623 if (s == NULL)
17624 return;
17625 }
17626
17627 /* match from matchaddpos() */
17628 for (i = 1; i < 9; i++)
17629 {
17630 sprintf((char *)buf, (char *)"pos%d", i);
17631 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17632 {
17633 if (di->di_tv.v_type != VAR_LIST)
17634 return;
17635
17636 list_append_tv(s, &di->di_tv);
17637 s->lv_refcount++;
17638 }
17639 else
17640 break;
17641 }
17642 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017643
17644 group = get_dict_string(d, (char_u *)"group", FALSE);
17645 priority = (int)get_dict_number(d, (char_u *)"priority");
17646 id = (int)get_dict_number(d, (char_u *)"id");
17647 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17648 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17649 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017650 if (i == 0)
17651 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017652 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017653 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017654 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017655 }
17656 else
17657 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017658 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017659 list_unref(s);
17660 s = NULL;
17661 }
17662
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017663 li = li->li_next;
17664 }
17665 rettv->vval.v_number = 0;
17666 }
17667#endif
17668}
17669
17670/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017671 * "setpos()" function
17672 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017674f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017675{
17676 pos_T pos;
17677 int fnum;
17678 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017679 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017680
Bram Moolenaar08250432008-02-13 11:42:46 +000017681 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017682 name = get_tv_string_chk(argvars);
17683 if (name != NULL)
17684 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017685 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017686 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017687 if (--pos.col < 0)
17688 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017689 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017690 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017691 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017692 if (fnum == curbuf->b_fnum)
17693 {
17694 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017695 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017696 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017697 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017698 curwin->w_set_curswant = FALSE;
17699 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017700 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017701 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017702 }
17703 else
17704 EMSG(_(e_invarg));
17705 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017706 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17707 {
17708 /* set mark */
17709 if (setmark_pos(name[1], &pos, fnum) == OK)
17710 rettv->vval.v_number = 0;
17711 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017712 else
17713 EMSG(_(e_invarg));
17714 }
17715 }
17716}
17717
17718/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017719 * "setqflist()" function
17720 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017722f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017723{
17724 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17725}
17726
17727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 * "setreg()" function
17729 */
17730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017731f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732{
17733 int regname;
17734 char_u *strregname;
17735 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017736 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 int append;
17738 char_u yank_type;
17739 long block_len;
17740
17741 block_len = -1;
17742 yank_type = MAUTO;
17743 append = FALSE;
17744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017745 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017746 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017748 if (strregname == NULL)
17749 return; /* type error; errmsg already given */
17750 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 if (regname == 0 || regname == '@')
17752 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017754 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017756 stropt = get_tv_string_chk(&argvars[2]);
17757 if (stropt == NULL)
17758 return; /* type error */
17759 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 switch (*stropt)
17761 {
17762 case 'a': case 'A': /* append */
17763 append = TRUE;
17764 break;
17765 case 'v': case 'c': /* character-wise selection */
17766 yank_type = MCHAR;
17767 break;
17768 case 'V': case 'l': /* line-wise selection */
17769 yank_type = MLINE;
17770 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 case 'b': case Ctrl_V: /* block-wise selection */
17772 yank_type = MBLOCK;
17773 if (VIM_ISDIGIT(stropt[1]))
17774 {
17775 ++stropt;
17776 block_len = getdigits(&stropt) - 1;
17777 --stropt;
17778 }
17779 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 }
17781 }
17782
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017783 if (argvars[1].v_type == VAR_LIST)
17784 {
17785 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017786 char_u **allocval;
17787 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017788 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017789 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017790 int len = argvars[1].vval.v_list->lv_len;
17791 listitem_T *li;
17792
Bram Moolenaar7d647822014-04-05 21:28:56 +020017793 /* First half: use for pointers to result lines; second half: use for
17794 * pointers to allocated copies. */
17795 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017796 if (lstval == NULL)
17797 return;
17798 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017799 allocval = lstval + len + 2;
17800 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017801
17802 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17803 li = li->li_next)
17804 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017805 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017806 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017807 goto free_lstval;
17808 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017809 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017810 /* Need to make a copy, next get_tv_string_buf_chk() will
17811 * overwrite the string. */
17812 strval = vim_strsave(buf);
17813 if (strval == NULL)
17814 goto free_lstval;
17815 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017816 }
17817 *curval++ = strval;
17818 }
17819 *curval++ = NULL;
17820
17821 write_reg_contents_lst(regname, lstval, -1,
17822 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017823free_lstval:
17824 while (curallocval > allocval)
17825 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017826 vim_free(lstval);
17827 }
17828 else
17829 {
17830 strval = get_tv_string_chk(&argvars[1]);
17831 if (strval == NULL)
17832 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017833 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017836 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837}
17838
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017839/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017840 * "settabvar()" function
17841 */
17842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017843f_settabvar(typval_T *argvars, typval_T *rettv)
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 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017847 tabpage_T *tp;
17848#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017849 char_u *varname, *tabvarname;
17850 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017851
17852 rettv->vval.v_number = 0;
17853
17854 if (check_restricted() || check_secure())
17855 return;
17856
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017857#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017858 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017859#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017860 varname = get_tv_string_chk(&argvars[1]);
17861 varp = &argvars[2];
17862
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017863 if (varname != NULL && varp != NULL
17864#ifdef FEAT_WINDOWS
17865 && tp != NULL
17866#endif
17867 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017868 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017869#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017870 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017871 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017872#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017873
17874 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17875 if (tabvarname != NULL)
17876 {
17877 STRCPY(tabvarname, "t:");
17878 STRCPY(tabvarname + 2, varname);
17879 set_var(tabvarname, varp, TRUE);
17880 vim_free(tabvarname);
17881 }
17882
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017883#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017884 /* Restore current tabpage */
17885 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017886 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017887#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017888 }
17889}
17890
17891/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017892 * "settabwinvar()" function
17893 */
17894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017895f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017896{
17897 setwinvar(argvars, rettv, 1);
17898}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899
17900/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017901 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017904f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017906 setwinvar(argvars, rettv, 0);
17907}
17908
17909/*
17910 * "setwinvar()" and "settabwinvar()" functions
17911 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017912
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017914setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017915{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 win_T *win;
17917#ifdef FEAT_WINDOWS
17918 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017919 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017920 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921#endif
17922 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017923 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017925 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926
17927 if (check_restricted() || check_secure())
17928 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017929
17930#ifdef FEAT_WINDOWS
17931 if (off == 1)
17932 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17933 else
17934 tp = curtab;
17935#endif
17936 win = find_win_by_nr(&argvars[off], tp);
17937 varname = get_tv_string_chk(&argvars[off + 1]);
17938 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939
17940 if (win != NULL && varname != NULL && varp != NULL)
17941 {
17942#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017943 need_switch_win = !(tp == curtab && win == curwin);
17944 if (!need_switch_win
17945 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017948 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017950 long numval;
17951 char_u *strval;
17952 int error = FALSE;
17953
17954 ++varname;
17955 numval = get_tv_number_chk(varp, &error);
17956 strval = get_tv_string_buf_chk(varp, nbuf);
17957 if (!error && strval != NULL)
17958 set_option_value(varname, numval, strval, OPT_LOCAL);
17959 }
17960 else
17961 {
17962 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17963 if (winvarname != NULL)
17964 {
17965 STRCPY(winvarname, "w:");
17966 STRCPY(winvarname + 2, varname);
17967 set_var(winvarname, varp, TRUE);
17968 vim_free(winvarname);
17969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 }
17971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017973 if (need_switch_win)
17974 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975#endif
17976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977}
17978
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017979#ifdef FEAT_CRYPT
17980/*
17981 * "sha256({string})" function
17982 */
17983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017984f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017985{
17986 char_u *p;
17987
17988 p = get_tv_string(&argvars[0]);
17989 rettv->vval.v_string = vim_strsave(
17990 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17991 rettv->v_type = VAR_STRING;
17992}
17993#endif /* FEAT_CRYPT */
17994
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017996 * "shellescape({string})" function
17997 */
17998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017999f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018000{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018001 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018002 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018003 rettv->v_type = VAR_STRING;
18004}
18005
18006/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018007 * shiftwidth() function
18008 */
18009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018010f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018011{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018012 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018013}
18014
18015/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018016 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 */
18018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018019f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018021 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022
Bram Moolenaar0d660222005-01-07 21:51:51 +000018023 p = get_tv_string(&argvars[0]);
18024 rettv->vval.v_string = vim_strsave(p);
18025 simplify_filename(rettv->vval.v_string); /* simplify in place */
18026 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027}
18028
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018029#ifdef FEAT_FLOAT
18030/*
18031 * "sin()" function
18032 */
18033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018034f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018035{
18036 float_T f;
18037
18038 rettv->v_type = VAR_FLOAT;
18039 if (get_float_arg(argvars, &f) == OK)
18040 rettv->vval.v_float = sin(f);
18041 else
18042 rettv->vval.v_float = 0.0;
18043}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018044
18045/*
18046 * "sinh()" function
18047 */
18048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018049f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018050{
18051 float_T f;
18052
18053 rettv->v_type = VAR_FLOAT;
18054 if (get_float_arg(argvars, &f) == OK)
18055 rettv->vval.v_float = sinh(f);
18056 else
18057 rettv->vval.v_float = 0.0;
18058}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018059#endif
18060
Bram Moolenaar0d660222005-01-07 21:51:51 +000018061static int
18062#ifdef __BORLANDC__
18063 _RTLENTRYF
18064#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018065 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018066static int
18067#ifdef __BORLANDC__
18068 _RTLENTRYF
18069#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018070 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018071
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018072/* struct used in the array that's given to qsort() */
18073typedef struct
18074{
18075 listitem_T *item;
18076 int idx;
18077} sortItem_T;
18078
Bram Moolenaar0d660222005-01-07 21:51:51 +000018079static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018080static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018081static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018082#ifdef FEAT_FLOAT
18083static int item_compare_float;
18084#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018085static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018086static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018087static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018088static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018089static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018090#define ITEM_COMPARE_FAIL 999
18091
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018093 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018095 static int
18096#ifdef __BORLANDC__
18097_RTLENTRYF
18098#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018099item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018101 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018102 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018103 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018104 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018105 int res;
18106 char_u numbuf1[NUMBUFLEN];
18107 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018109 si1 = (sortItem_T *)s1;
18110 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018111 tv1 = &si1->item->li_tv;
18112 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018113
18114 if (item_compare_numbers)
18115 {
18116 long v1 = get_tv_number(tv1);
18117 long v2 = get_tv_number(tv2);
18118
18119 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18120 }
18121
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018122#ifdef FEAT_FLOAT
18123 if (item_compare_float)
18124 {
18125 float_T v1 = get_tv_float(tv1);
18126 float_T v2 = get_tv_float(tv2);
18127
18128 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18129 }
18130#endif
18131
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018132 /* tv2string() puts quotes around a string and allocates memory. Don't do
18133 * that for string variables. Use a single quote when comparing with a
18134 * non-string to do what the docs promise. */
18135 if (tv1->v_type == VAR_STRING)
18136 {
18137 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18138 p1 = (char_u *)"'";
18139 else
18140 p1 = tv1->vval.v_string;
18141 }
18142 else
18143 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18144 if (tv2->v_type == VAR_STRING)
18145 {
18146 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18147 p2 = (char_u *)"'";
18148 else
18149 p2 = tv2->vval.v_string;
18150 }
18151 else
18152 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018153 if (p1 == NULL)
18154 p1 = (char_u *)"";
18155 if (p2 == NULL)
18156 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018157 if (!item_compare_numeric)
18158 {
18159 if (item_compare_ic)
18160 res = STRICMP(p1, p2);
18161 else
18162 res = STRCMP(p1, p2);
18163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018165 {
18166 double n1, n2;
18167 n1 = strtod((char *)p1, (char **)&p1);
18168 n2 = strtod((char *)p2, (char **)&p2);
18169 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18170 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018171
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018172 /* When the result would be zero, compare the item indexes. Makes the
18173 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018174 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018175 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018176
Bram Moolenaar0d660222005-01-07 21:51:51 +000018177 vim_free(tofree1);
18178 vim_free(tofree2);
18179 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180}
18181
18182 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018183#ifdef __BORLANDC__
18184_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018186item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018187{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018188 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018189 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018190 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018191 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018192 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018194 /* shortcut after failure in previous call; compare all items equal */
18195 if (item_compare_func_err)
18196 return 0;
18197
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018198 si1 = (sortItem_T *)s1;
18199 si2 = (sortItem_T *)s2;
18200
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018201 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018202 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018203 copy_tv(&si1->item->li_tv, &argv[0]);
18204 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018205
18206 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018207 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018208 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18209 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018210 clear_tv(&argv[0]);
18211 clear_tv(&argv[1]);
18212
18213 if (res == FAIL)
18214 res = ITEM_COMPARE_FAIL;
18215 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018216 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18217 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018218 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018219 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018220
18221 /* When the result would be zero, compare the pointers themselves. Makes
18222 * the sort stable. */
18223 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018224 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018225
Bram Moolenaar0d660222005-01-07 21:51:51 +000018226 return res;
18227}
18228
18229/*
18230 * "sort({list})" function
18231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018233do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234{
Bram Moolenaar33570922005-01-25 22:26:29 +000018235 list_T *l;
18236 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018237 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018238 long len;
18239 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240
Bram Moolenaar0d660222005-01-07 21:51:51 +000018241 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018242 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 else
18244 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018246 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018247 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18248 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018249 return;
18250 rettv->vval.v_list = l;
18251 rettv->v_type = VAR_LIST;
18252 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253
Bram Moolenaar0d660222005-01-07 21:51:51 +000018254 len = list_len(l);
18255 if (len <= 1)
18256 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257
Bram Moolenaar0d660222005-01-07 21:51:51 +000018258 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018259 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018260 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018261#ifdef FEAT_FLOAT
18262 item_compare_float = FALSE;
18263#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018265 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018266 if (argvars[1].v_type != VAR_UNKNOWN)
18267 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018268 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018269 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018270 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018271 else
18272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018273 int error = FALSE;
18274
18275 i = get_tv_number_chk(&argvars[1], &error);
18276 if (error)
18277 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018278 if (i == 1)
18279 item_compare_ic = TRUE;
18280 else
18281 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018282 if (item_compare_func != NULL)
18283 {
18284 if (STRCMP(item_compare_func, "n") == 0)
18285 {
18286 item_compare_func = NULL;
18287 item_compare_numeric = TRUE;
18288 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018289 else if (STRCMP(item_compare_func, "N") == 0)
18290 {
18291 item_compare_func = NULL;
18292 item_compare_numbers = TRUE;
18293 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018294#ifdef FEAT_FLOAT
18295 else if (STRCMP(item_compare_func, "f") == 0)
18296 {
18297 item_compare_func = NULL;
18298 item_compare_float = TRUE;
18299 }
18300#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018301 else if (STRCMP(item_compare_func, "i") == 0)
18302 {
18303 item_compare_func = NULL;
18304 item_compare_ic = TRUE;
18305 }
18306 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018307 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018308
18309 if (argvars[2].v_type != VAR_UNKNOWN)
18310 {
18311 /* optional third argument: {dict} */
18312 if (argvars[2].v_type != VAR_DICT)
18313 {
18314 EMSG(_(e_dictreq));
18315 return;
18316 }
18317 item_compare_selfdict = argvars[2].vval.v_dict;
18318 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320
Bram Moolenaar0d660222005-01-07 21:51:51 +000018321 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018322 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018323 if (ptrs == NULL)
18324 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325
Bram Moolenaar327aa022014-03-25 18:24:23 +010018326 i = 0;
18327 if (sort)
18328 {
18329 /* sort(): ptrs will be the list to sort */
18330 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018331 {
18332 ptrs[i].item = li;
18333 ptrs[i].idx = i;
18334 ++i;
18335 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018336
18337 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018338 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018339 /* test the compare function */
18340 if (item_compare_func != NULL
18341 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018342 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018343 EMSG(_("E702: Sort compare function failed"));
18344 else
18345 {
18346 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018347 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018348 item_compare_func == NULL ? item_compare : item_compare2);
18349
18350 if (!item_compare_func_err)
18351 {
18352 /* Clear the List and append the items in sorted order. */
18353 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18354 l->lv_len = 0;
18355 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018356 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018357 }
18358 }
18359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018361 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018362 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018363
18364 /* f_uniq(): ptrs will be a stack of items to remove */
18365 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018366 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018367 item_compare_func_ptr = item_compare_func
18368 ? item_compare2 : item_compare;
18369
18370 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18371 li = li->li_next)
18372 {
18373 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18374 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018375 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018376 if (item_compare_func_err)
18377 {
18378 EMSG(_("E882: Uniq compare function failed"));
18379 break;
18380 }
18381 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018383 if (!item_compare_func_err)
18384 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018385 while (--i >= 0)
18386 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018387 li = ptrs[i].item->li_next;
18388 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018389 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018390 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018391 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018392 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018393 list_fix_watch(l, li);
18394 listitem_free(li);
18395 l->lv_len--;
18396 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018397 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018398 }
18399
18400 vim_free(ptrs);
18401 }
18402}
18403
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018404/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018405 * "sort({list})" function
18406 */
18407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018408f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018409{
18410 do_sort_uniq(argvars, rettv, TRUE);
18411}
18412
18413/*
18414 * "uniq({list})" function
18415 */
18416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018417f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018418{
18419 do_sort_uniq(argvars, rettv, FALSE);
18420}
18421
18422/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018423 * "soundfold({word})" function
18424 */
18425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018426f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018427{
18428 char_u *s;
18429
18430 rettv->v_type = VAR_STRING;
18431 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018432#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018433 rettv->vval.v_string = eval_soundfold(s);
18434#else
18435 rettv->vval.v_string = vim_strsave(s);
18436#endif
18437}
18438
18439/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018440 * "spellbadword()" function
18441 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018443f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018444{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018445 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018446 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018447 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018448
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018449 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018450 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018451
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018452#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018453 if (argvars[0].v_type == VAR_UNKNOWN)
18454 {
18455 /* Find the start and length of the badly spelled word. */
18456 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18457 if (len != 0)
18458 word = ml_get_cursor();
18459 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018460 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018461 {
18462 char_u *str = get_tv_string_chk(&argvars[0]);
18463 int capcol = -1;
18464
18465 if (str != NULL)
18466 {
18467 /* Check the argument for spelling. */
18468 while (*str != NUL)
18469 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018470 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018471 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018472 {
18473 word = str;
18474 break;
18475 }
18476 str += len;
18477 }
18478 }
18479 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018480#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018481
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018482 list_append_string(rettv->vval.v_list, word, len);
18483 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018484 attr == HLF_SPB ? "bad" :
18485 attr == HLF_SPR ? "rare" :
18486 attr == HLF_SPL ? "local" :
18487 attr == HLF_SPC ? "caps" :
18488 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018489}
18490
18491/*
18492 * "spellsuggest()" function
18493 */
18494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018495f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018496{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018497#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018498 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018499 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018500 int maxcount;
18501 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018502 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018503 listitem_T *li;
18504 int need_capital = FALSE;
18505#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018506
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018507 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018508 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018509
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018510#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018511 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018512 {
18513 str = get_tv_string(&argvars[0]);
18514 if (argvars[1].v_type != VAR_UNKNOWN)
18515 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018516 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018517 if (maxcount <= 0)
18518 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018519 if (argvars[2].v_type != VAR_UNKNOWN)
18520 {
18521 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18522 if (typeerr)
18523 return;
18524 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018525 }
18526 else
18527 maxcount = 25;
18528
Bram Moolenaar4770d092006-01-12 23:22:24 +000018529 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018530
18531 for (i = 0; i < ga.ga_len; ++i)
18532 {
18533 str = ((char_u **)ga.ga_data)[i];
18534
18535 li = listitem_alloc();
18536 if (li == NULL)
18537 vim_free(str);
18538 else
18539 {
18540 li->li_tv.v_type = VAR_STRING;
18541 li->li_tv.v_lock = 0;
18542 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018543 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018544 }
18545 }
18546 ga_clear(&ga);
18547 }
18548#endif
18549}
18550
Bram Moolenaar0d660222005-01-07 21:51:51 +000018551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018552f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018553{
18554 char_u *str;
18555 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018556 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018557 regmatch_T regmatch;
18558 char_u patbuf[NUMBUFLEN];
18559 char_u *save_cpo;
18560 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018561 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018562 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018563 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018564
18565 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18566 save_cpo = p_cpo;
18567 p_cpo = (char_u *)"";
18568
18569 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018570 if (argvars[1].v_type != VAR_UNKNOWN)
18571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018572 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18573 if (pat == NULL)
18574 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018575 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018576 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018577 }
18578 if (pat == NULL || *pat == NUL)
18579 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018580
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018581 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018583 if (typeerr)
18584 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585
Bram Moolenaar0d660222005-01-07 21:51:51 +000018586 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18587 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018589 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018590 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018591 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018592 if (*str == NUL)
18593 match = FALSE; /* empty item at the end */
18594 else
18595 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018596 if (match)
18597 end = regmatch.startp[0];
18598 else
18599 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018600 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18601 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018602 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018603 if (list_append_string(rettv->vval.v_list, str,
18604 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018605 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018606 }
18607 if (!match)
18608 break;
18609 /* Advance to just after the match. */
18610 if (regmatch.endp[0] > str)
18611 col = 0;
18612 else
18613 {
18614 /* Don't get stuck at the same match. */
18615#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018616 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018617#else
18618 col = 1;
18619#endif
18620 }
18621 str = regmatch.endp[0];
18622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623
Bram Moolenaar473de612013-06-08 18:19:48 +020018624 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626
Bram Moolenaar0d660222005-01-07 21:51:51 +000018627 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628}
18629
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018630#ifdef FEAT_FLOAT
18631/*
18632 * "sqrt()" function
18633 */
18634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018635f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018636{
18637 float_T f;
18638
18639 rettv->v_type = VAR_FLOAT;
18640 if (get_float_arg(argvars, &f) == OK)
18641 rettv->vval.v_float = sqrt(f);
18642 else
18643 rettv->vval.v_float = 0.0;
18644}
18645
18646/*
18647 * "str2float()" function
18648 */
18649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018650f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018651{
18652 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18653
18654 if (*p == '+')
18655 p = skipwhite(p + 1);
18656 (void)string2float(p, &rettv->vval.v_float);
18657 rettv->v_type = VAR_FLOAT;
18658}
18659#endif
18660
Bram Moolenaar2c932302006-03-18 21:42:09 +000018661/*
18662 * "str2nr()" function
18663 */
18664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018665f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018666{
18667 int base = 10;
18668 char_u *p;
18669 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018670 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018671
18672 if (argvars[1].v_type != VAR_UNKNOWN)
18673 {
18674 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018675 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018676 {
18677 EMSG(_(e_invarg));
18678 return;
18679 }
18680 }
18681
18682 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018683 if (*p == '+')
18684 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018685 switch (base)
18686 {
18687 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18688 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18689 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18690 default: what = 0;
18691 }
18692 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018693 rettv->vval.v_number = n;
18694}
18695
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696#ifdef HAVE_STRFTIME
18697/*
18698 * "strftime({format}[, {time}])" function
18699 */
18700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018701f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702{
18703 char_u result_buf[256];
18704 struct tm *curtime;
18705 time_t seconds;
18706 char_u *p;
18707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018708 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018710 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018711 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712 seconds = time(NULL);
18713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018714 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 curtime = localtime(&seconds);
18716 /* MSVC returns NULL for an invalid value of seconds. */
18717 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018718 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 else
18720 {
18721# ifdef FEAT_MBYTE
18722 vimconv_T conv;
18723 char_u *enc;
18724
18725 conv.vc_type = CONV_NONE;
18726 enc = enc_locale();
18727 convert_setup(&conv, p_enc, enc);
18728 if (conv.vc_type != CONV_NONE)
18729 p = string_convert(&conv, p, NULL);
18730# endif
18731 if (p != NULL)
18732 (void)strftime((char *)result_buf, sizeof(result_buf),
18733 (char *)p, curtime);
18734 else
18735 result_buf[0] = NUL;
18736
18737# ifdef FEAT_MBYTE
18738 if (conv.vc_type != CONV_NONE)
18739 vim_free(p);
18740 convert_setup(&conv, enc, p_enc);
18741 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 else
18744# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018745 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746
18747# ifdef FEAT_MBYTE
18748 /* Release conversion descriptors */
18749 convert_setup(&conv, NULL, NULL);
18750 vim_free(enc);
18751# endif
18752 }
18753}
18754#endif
18755
18756/*
18757 * "stridx()" function
18758 */
18759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018760f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761{
18762 char_u buf[NUMBUFLEN];
18763 char_u *needle;
18764 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018765 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018767 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018769 needle = get_tv_string_chk(&argvars[1]);
18770 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018772 if (needle == NULL || haystack == NULL)
18773 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774
Bram Moolenaar33570922005-01-25 22:26:29 +000018775 if (argvars[2].v_type != VAR_UNKNOWN)
18776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018777 int error = FALSE;
18778
18779 start_idx = get_tv_number_chk(&argvars[2], &error);
18780 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018781 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018782 if (start_idx >= 0)
18783 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018784 }
18785
18786 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18787 if (pos != NULL)
18788 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789}
18790
18791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018792 * "string()" function
18793 */
18794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018795f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018796{
18797 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018798 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018801 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018802 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018803 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018804 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805}
18806
18807/*
18808 * "strlen()" function
18809 */
18810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018811f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018813 rettv->vval.v_number = (varnumber_T)(STRLEN(
18814 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815}
18816
18817/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018818 * "strchars()" function
18819 */
18820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018821f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018822{
18823 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018824 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018825#ifdef FEAT_MBYTE
18826 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018827 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018828#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018829
18830 if (argvars[1].v_type != VAR_UNKNOWN)
18831 skipcc = get_tv_number_chk(&argvars[1], NULL);
18832 if (skipcc < 0 || skipcc > 1)
18833 EMSG(_(e_invarg));
18834 else
18835 {
18836#ifdef FEAT_MBYTE
18837 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18838 while (*s != NUL)
18839 {
18840 func_mb_ptr2char_adv(&s);
18841 ++len;
18842 }
18843 rettv->vval.v_number = len;
18844#else
18845 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18846#endif
18847 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018848}
18849
18850/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018851 * "strdisplaywidth()" function
18852 */
18853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018854f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018855{
18856 char_u *s = get_tv_string(&argvars[0]);
18857 int col = 0;
18858
18859 if (argvars[1].v_type != VAR_UNKNOWN)
18860 col = get_tv_number(&argvars[1]);
18861
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018862 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018863}
18864
18865/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018866 * "strwidth()" function
18867 */
18868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018869f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018870{
18871 char_u *s = get_tv_string(&argvars[0]);
18872
18873 rettv->vval.v_number = (varnumber_T)(
18874#ifdef FEAT_MBYTE
18875 mb_string2cells(s, -1)
18876#else
18877 STRLEN(s)
18878#endif
18879 );
18880}
18881
18882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 * "strpart()" function
18884 */
18885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018886f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887{
18888 char_u *p;
18889 int n;
18890 int len;
18891 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018892 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018894 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 slen = (int)STRLEN(p);
18896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018897 n = get_tv_number_chk(&argvars[1], &error);
18898 if (error)
18899 len = 0;
18900 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018901 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 else
18903 len = slen - n; /* default len: all bytes that are available. */
18904
18905 /*
18906 * Only return the overlap between the specified part and the actual
18907 * string.
18908 */
18909 if (n < 0)
18910 {
18911 len += n;
18912 n = 0;
18913 }
18914 else if (n > slen)
18915 n = slen;
18916 if (len < 0)
18917 len = 0;
18918 else if (n + len > slen)
18919 len = slen - n;
18920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018921 rettv->v_type = VAR_STRING;
18922 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923}
18924
18925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018926 * "strridx()" function
18927 */
18928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018929f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018930{
18931 char_u buf[NUMBUFLEN];
18932 char_u *needle;
18933 char_u *haystack;
18934 char_u *rest;
18935 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018936 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018938 needle = get_tv_string_chk(&argvars[1]);
18939 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018940
18941 rettv->vval.v_number = -1;
18942 if (needle == NULL || haystack == NULL)
18943 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018944
18945 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018946 if (argvars[2].v_type != VAR_UNKNOWN)
18947 {
18948 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018949 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018950 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018951 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018952 }
18953 else
18954 end_idx = haystack_len;
18955
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018957 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018959 lastmatch = haystack + end_idx;
18960 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018961 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018962 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 for (rest = haystack; *rest != '\0'; ++rest)
18964 {
18965 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018966 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018967 break;
18968 lastmatch = rest;
18969 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018971
18972 if (lastmatch == NULL)
18973 rettv->vval.v_number = -1;
18974 else
18975 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18976}
18977
18978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 * "strtrans()" function
18980 */
18981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018982f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018984 rettv->v_type = VAR_STRING;
18985 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986}
18987
18988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018989 * "submatch()" function
18990 */
18991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018992f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018993{
Bram Moolenaar41571762014-04-02 19:00:58 +020018994 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018995 int no;
18996 int retList = 0;
18997
18998 no = (int)get_tv_number_chk(&argvars[0], &error);
18999 if (error)
19000 return;
19001 error = FALSE;
19002 if (argvars[1].v_type != VAR_UNKNOWN)
19003 retList = get_tv_number_chk(&argvars[1], &error);
19004 if (error)
19005 return;
19006
19007 if (retList == 0)
19008 {
19009 rettv->v_type = VAR_STRING;
19010 rettv->vval.v_string = reg_submatch(no);
19011 }
19012 else
19013 {
19014 rettv->v_type = VAR_LIST;
19015 rettv->vval.v_list = reg_submatch_list(no);
19016 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019017}
19018
19019/*
19020 * "substitute()" function
19021 */
19022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019023f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019024{
19025 char_u patbuf[NUMBUFLEN];
19026 char_u subbuf[NUMBUFLEN];
19027 char_u flagsbuf[NUMBUFLEN];
19028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019029 char_u *str = get_tv_string_chk(&argvars[0]);
19030 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19031 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19032 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19033
Bram Moolenaar0d660222005-01-07 21:51:51 +000019034 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019035 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19036 rettv->vval.v_string = NULL;
19037 else
19038 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019039}
19040
19041/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019042 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019045f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046{
19047 int id = 0;
19048#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019049 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 long col;
19051 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019052 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019054 lnum = get_tv_lnum(argvars); /* -1 on type error */
19055 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19056 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019058 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019059 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019060 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061#endif
19062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019063 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064}
19065
19066/*
19067 * "synIDattr(id, what [, mode])" function
19068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019070f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071{
19072 char_u *p = NULL;
19073#ifdef FEAT_SYN_HL
19074 int id;
19075 char_u *what;
19076 char_u *mode;
19077 char_u modebuf[NUMBUFLEN];
19078 int modec;
19079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019080 id = get_tv_number(&argvars[0]);
19081 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019084 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019086 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 modec = 0; /* replace invalid with current */
19088 }
19089 else
19090 {
19091#ifdef FEAT_GUI
19092 if (gui.in_use)
19093 modec = 'g';
19094 else
19095#endif
19096 if (t_colors > 1)
19097 modec = 'c';
19098 else
19099 modec = 't';
19100 }
19101
19102
19103 switch (TOLOWER_ASC(what[0]))
19104 {
19105 case 'b':
19106 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19107 p = highlight_color(id, what, modec);
19108 else /* bold */
19109 p = highlight_has_attr(id, HL_BOLD, modec);
19110 break;
19111
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019112 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 p = highlight_color(id, what, modec);
19114 break;
19115
19116 case 'i':
19117 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19118 p = highlight_has_attr(id, HL_INVERSE, modec);
19119 else /* italic */
19120 p = highlight_has_attr(id, HL_ITALIC, modec);
19121 break;
19122
19123 case 'n': /* name */
19124 p = get_highlight_name(NULL, id - 1);
19125 break;
19126
19127 case 'r': /* reverse */
19128 p = highlight_has_attr(id, HL_INVERSE, modec);
19129 break;
19130
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019131 case 's':
19132 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19133 p = highlight_color(id, what, modec);
19134 else /* standout */
19135 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136 break;
19137
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019138 case 'u':
19139 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19140 /* underline */
19141 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19142 else
19143 /* undercurl */
19144 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 break;
19146 }
19147
19148 if (p != NULL)
19149 p = vim_strsave(p);
19150#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019151 rettv->v_type = VAR_STRING;
19152 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153}
19154
19155/*
19156 * "synIDtrans(id)" function
19157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019159f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160{
19161 int id;
19162
19163#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019164 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165
19166 if (id > 0)
19167 id = syn_get_final_id(id);
19168 else
19169#endif
19170 id = 0;
19171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019172 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173}
19174
19175/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019176 * "synconcealed(lnum, col)" function
19177 */
19178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019179f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019180{
19181#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19182 long lnum;
19183 long col;
19184 int syntax_flags = 0;
19185 int cchar;
19186 int matchid = 0;
19187 char_u str[NUMBUFLEN];
19188#endif
19189
19190 rettv->v_type = VAR_LIST;
19191 rettv->vval.v_list = NULL;
19192
19193#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19194 lnum = get_tv_lnum(argvars); /* -1 on type error */
19195 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19196
19197 vim_memset(str, NUL, sizeof(str));
19198
19199 if (rettv_list_alloc(rettv) != FAIL)
19200 {
19201 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19202 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19203 && curwin->w_p_cole > 0)
19204 {
19205 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19206 syntax_flags = get_syntax_info(&matchid);
19207
19208 /* get the conceal character */
19209 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19210 {
19211 cchar = syn_get_sub_char();
19212 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19213 cchar = lcs_conceal;
19214 if (cchar != NUL)
19215 {
19216# ifdef FEAT_MBYTE
19217 if (has_mbyte)
19218 (*mb_char2bytes)(cchar, str);
19219 else
19220# endif
19221 str[0] = cchar;
19222 }
19223 }
19224 }
19225
19226 list_append_number(rettv->vval.v_list,
19227 (syntax_flags & HL_CONCEAL) != 0);
19228 /* -1 to auto-determine strlen */
19229 list_append_string(rettv->vval.v_list, str, -1);
19230 list_append_number(rettv->vval.v_list, matchid);
19231 }
19232#endif
19233}
19234
19235/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019236 * "synstack(lnum, col)" function
19237 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019239f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019240{
19241#ifdef FEAT_SYN_HL
19242 long lnum;
19243 long col;
19244 int i;
19245 int id;
19246#endif
19247
19248 rettv->v_type = VAR_LIST;
19249 rettv->vval.v_list = NULL;
19250
19251#ifdef FEAT_SYN_HL
19252 lnum = get_tv_lnum(argvars); /* -1 on type error */
19253 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19254
19255 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019256 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019257 && rettv_list_alloc(rettv) != FAIL)
19258 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019259 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019260 for (i = 0; ; ++i)
19261 {
19262 id = syn_get_stack_item(i);
19263 if (id < 0)
19264 break;
19265 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19266 break;
19267 }
19268 }
19269#endif
19270}
19271
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019273get_cmd_output_as_rettv(
19274 typval_T *argvars,
19275 typval_T *rettv,
19276 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019278 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019280 char_u *infile = NULL;
19281 char_u buf[NUMBUFLEN];
19282 int err = FALSE;
19283 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019284 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019285 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019287 rettv->v_type = VAR_STRING;
19288 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019289 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019290 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019291
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019292 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019293 {
19294 /*
19295 * Write the string to a temp file, to be used for input of the shell
19296 * command.
19297 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019298 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019299 {
19300 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019301 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019302 }
19303
19304 fd = mch_fopen((char *)infile, WRITEBIN);
19305 if (fd == NULL)
19306 {
19307 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019308 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019309 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019310 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019311 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019312 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19313 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019314 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019315 else
19316 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019317 size_t len;
19318
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019319 p = get_tv_string_buf_chk(&argvars[1], buf);
19320 if (p == NULL)
19321 {
19322 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019323 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019324 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019325 len = STRLEN(p);
19326 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019327 err = TRUE;
19328 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019329 if (fclose(fd) != 0)
19330 err = TRUE;
19331 if (err)
19332 {
19333 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019334 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019335 }
19336 }
19337
Bram Moolenaar52a72462014-08-29 15:53:52 +020019338 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19339 * echoes typeahead, that messes up the display. */
19340 if (!msg_silent)
19341 flags += SHELL_COOKED;
19342
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019343 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019345 int len;
19346 listitem_T *li;
19347 char_u *s = NULL;
19348 char_u *start;
19349 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019350 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351
Bram Moolenaar52a72462014-08-29 15:53:52 +020019352 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019353 if (res == NULL)
19354 goto errret;
19355
19356 list = list_alloc();
19357 if (list == NULL)
19358 goto errret;
19359
19360 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019362 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019363 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019364 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019365 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019366
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019367 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019368 if (s == NULL)
19369 goto errret;
19370
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019371 for (p = s; start < end; ++p, ++start)
19372 *p = *start == NUL ? NL : *start;
19373 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019374
19375 li = listitem_alloc();
19376 if (li == NULL)
19377 {
19378 vim_free(s);
19379 goto errret;
19380 }
19381 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019382 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019383 li->li_tv.vval.v_string = s;
19384 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019386
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019387 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019388 rettv->v_type = VAR_LIST;
19389 rettv->vval.v_list = list;
19390 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019392 else
19393 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019394 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019395#ifdef USE_CR
19396 /* translate <CR> into <NL> */
19397 if (res != NULL)
19398 {
19399 char_u *s;
19400
19401 for (s = res; *s; ++s)
19402 {
19403 if (*s == CAR)
19404 *s = NL;
19405 }
19406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407#else
19408# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019409 /* translate <CR><NL> into <NL> */
19410 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019412 char_u *s, *d;
19413
19414 d = res;
19415 for (s = res; *s; ++s)
19416 {
19417 if (s[0] == CAR && s[1] == NL)
19418 ++s;
19419 *d++ = *s;
19420 }
19421 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423# endif
19424#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019425 rettv->vval.v_string = res;
19426 res = NULL;
19427 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019428
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019429errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019430 if (infile != NULL)
19431 {
19432 mch_remove(infile);
19433 vim_free(infile);
19434 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019435 if (res != NULL)
19436 vim_free(res);
19437 if (list != NULL)
19438 list_free(list, TRUE);
19439}
19440
19441/*
19442 * "system()" function
19443 */
19444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019445f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019446{
19447 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19448}
19449
19450/*
19451 * "systemlist()" function
19452 */
19453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019454f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019455{
19456 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457}
19458
19459/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019460 * "tabpagebuflist()" function
19461 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019463f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019464{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019465#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019466 tabpage_T *tp;
19467 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019468
19469 if (argvars[0].v_type == VAR_UNKNOWN)
19470 wp = firstwin;
19471 else
19472 {
19473 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19474 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019475 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019476 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019477 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019478 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019479 for (; wp != NULL; wp = wp->w_next)
19480 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019481 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019482 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019483 }
19484#endif
19485}
19486
19487
19488/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019489 * "tabpagenr()" function
19490 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019492f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019493{
19494 int nr = 1;
19495#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019496 char_u *arg;
19497
19498 if (argvars[0].v_type != VAR_UNKNOWN)
19499 {
19500 arg = get_tv_string_chk(&argvars[0]);
19501 nr = 0;
19502 if (arg != NULL)
19503 {
19504 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019505 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019506 else
19507 EMSG2(_(e_invexpr2), arg);
19508 }
19509 }
19510 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019511 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019512#endif
19513 rettv->vval.v_number = nr;
19514}
19515
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019516
19517#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019518static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019519
19520/*
19521 * Common code for tabpagewinnr() and winnr().
19522 */
19523 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019524get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019525{
19526 win_T *twin;
19527 int nr = 1;
19528 win_T *wp;
19529 char_u *arg;
19530
19531 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19532 if (argvar->v_type != VAR_UNKNOWN)
19533 {
19534 arg = get_tv_string_chk(argvar);
19535 if (arg == NULL)
19536 nr = 0; /* type error; errmsg already given */
19537 else if (STRCMP(arg, "$") == 0)
19538 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19539 else if (STRCMP(arg, "#") == 0)
19540 {
19541 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19542 if (twin == NULL)
19543 nr = 0;
19544 }
19545 else
19546 {
19547 EMSG2(_(e_invexpr2), arg);
19548 nr = 0;
19549 }
19550 }
19551
19552 if (nr > 0)
19553 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19554 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019555 {
19556 if (wp == NULL)
19557 {
19558 /* didn't find it in this tabpage */
19559 nr = 0;
19560 break;
19561 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019562 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019563 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019564 return nr;
19565}
19566#endif
19567
19568/*
19569 * "tabpagewinnr()" function
19570 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019572f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019573{
19574 int nr = 1;
19575#ifdef FEAT_WINDOWS
19576 tabpage_T *tp;
19577
19578 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19579 if (tp == NULL)
19580 nr = 0;
19581 else
19582 nr = get_winnr(tp, &argvars[1]);
19583#endif
19584 rettv->vval.v_number = nr;
19585}
19586
19587
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019588/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019589 * "tagfiles()" function
19590 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019592f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019593{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019594 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019595 tagname_T tn;
19596 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019597
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019598 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019599 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019600 fname = alloc(MAXPATHL);
19601 if (fname == NULL)
19602 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019603
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019604 for (first = TRUE; ; first = FALSE)
19605 if (get_tagfname(&tn, first, fname) == FAIL
19606 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019607 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019608 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019609 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019610}
19611
19612/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019613 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019614 */
19615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019616f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019617{
19618 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019619
19620 tag_pattern = get_tv_string(&argvars[0]);
19621
19622 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019623 if (*tag_pattern == NUL)
19624 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019625
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019626 if (rettv_list_alloc(rettv) == OK)
19627 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019628}
19629
19630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 * "tempname()" function
19632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019634f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635{
19636 static int x = 'A';
19637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019638 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019639 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640
19641 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19642 * names. Skip 'I' and 'O', they are used for shell redirection. */
19643 do
19644 {
19645 if (x == 'Z')
19646 x = '0';
19647 else if (x == '9')
19648 x = 'A';
19649 else
19650 {
19651#ifdef EBCDIC
19652 if (x == 'I')
19653 x = 'J';
19654 else if (x == 'R')
19655 x = 'S';
19656 else
19657#endif
19658 ++x;
19659 }
19660 } while (x == 'I' || x == 'O');
19661}
19662
19663/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019664 * "test(list)" function: Just checking the walls...
19665 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019667f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019668{
19669 /* Used for unit testing. Change the code below to your liking. */
19670#if 0
19671 listitem_T *li;
19672 list_T *l;
19673 char_u *bad, *good;
19674
19675 if (argvars[0].v_type != VAR_LIST)
19676 return;
19677 l = argvars[0].vval.v_list;
19678 if (l == NULL)
19679 return;
19680 li = l->lv_first;
19681 if (li == NULL)
19682 return;
19683 bad = get_tv_string(&li->li_tv);
19684 li = li->li_next;
19685 if (li == NULL)
19686 return;
19687 good = get_tv_string(&li->li_tv);
19688 rettv->vval.v_number = test_edit_score(bad, good);
19689#endif
19690}
19691
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019692#ifdef FEAT_FLOAT
19693/*
19694 * "tan()" function
19695 */
19696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019697f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019698{
19699 float_T f;
19700
19701 rettv->v_type = VAR_FLOAT;
19702 if (get_float_arg(argvars, &f) == OK)
19703 rettv->vval.v_float = tan(f);
19704 else
19705 rettv->vval.v_float = 0.0;
19706}
19707
19708/*
19709 * "tanh()" function
19710 */
19711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019712f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019713{
19714 float_T f;
19715
19716 rettv->v_type = VAR_FLOAT;
19717 if (get_float_arg(argvars, &f) == OK)
19718 rettv->vval.v_float = tanh(f);
19719 else
19720 rettv->vval.v_float = 0.0;
19721}
19722#endif
19723
Bram Moolenaard52d9742005-08-21 22:20:28 +000019724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 * "tolower(string)" function
19726 */
19727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019728f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729{
19730 char_u *p;
19731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019732 p = vim_strsave(get_tv_string(&argvars[0]));
19733 rettv->v_type = VAR_STRING;
19734 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735
19736 if (p != NULL)
19737 while (*p != NUL)
19738 {
19739#ifdef FEAT_MBYTE
19740 int l;
19741
19742 if (enc_utf8)
19743 {
19744 int c, lc;
19745
19746 c = utf_ptr2char(p);
19747 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019748 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 /* TODO: reallocate string when byte count changes. */
19750 if (utf_char2len(lc) == l)
19751 utf_char2bytes(lc, p);
19752 p += l;
19753 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019754 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 p += l; /* skip multi-byte character */
19756 else
19757#endif
19758 {
19759 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19760 ++p;
19761 }
19762 }
19763}
19764
19765/*
19766 * "toupper(string)" function
19767 */
19768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019769f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019771 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019772 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773}
19774
19775/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019776 * "tr(string, fromstr, tostr)" function
19777 */
19778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019779f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019780{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019781 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019782 char_u *fromstr;
19783 char_u *tostr;
19784 char_u *p;
19785#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019786 int inlen;
19787 int fromlen;
19788 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019789 int idx;
19790 char_u *cpstr;
19791 int cplen;
19792 int first = TRUE;
19793#endif
19794 char_u buf[NUMBUFLEN];
19795 char_u buf2[NUMBUFLEN];
19796 garray_T ga;
19797
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019798 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019799 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19800 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019801
19802 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019803 rettv->v_type = VAR_STRING;
19804 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019805 if (fromstr == NULL || tostr == NULL)
19806 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019807 ga_init2(&ga, (int)sizeof(char), 80);
19808
19809#ifdef FEAT_MBYTE
19810 if (!has_mbyte)
19811#endif
19812 /* not multi-byte: fromstr and tostr must be the same length */
19813 if (STRLEN(fromstr) != STRLEN(tostr))
19814 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019815#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019816error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019817#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019818 EMSG2(_(e_invarg2), fromstr);
19819 ga_clear(&ga);
19820 return;
19821 }
19822
19823 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019824 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019825 {
19826#ifdef FEAT_MBYTE
19827 if (has_mbyte)
19828 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019829 inlen = (*mb_ptr2len)(in_str);
19830 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019831 cplen = inlen;
19832 idx = 0;
19833 for (p = fromstr; *p != NUL; p += fromlen)
19834 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019835 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019836 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019837 {
19838 for (p = tostr; *p != NUL; p += tolen)
19839 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019840 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019841 if (idx-- == 0)
19842 {
19843 cplen = tolen;
19844 cpstr = p;
19845 break;
19846 }
19847 }
19848 if (*p == NUL) /* tostr is shorter than fromstr */
19849 goto error;
19850 break;
19851 }
19852 ++idx;
19853 }
19854
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019855 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019856 {
19857 /* Check that fromstr and tostr have the same number of
19858 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019859 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019860 first = FALSE;
19861 for (p = tostr; *p != NUL; p += tolen)
19862 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019863 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019864 --idx;
19865 }
19866 if (idx != 0)
19867 goto error;
19868 }
19869
Bram Moolenaarcde88542015-08-11 19:14:00 +020019870 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019871 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019872 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019873
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019874 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019875 }
19876 else
19877#endif
19878 {
19879 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019880 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019881 if (p != NULL)
19882 ga_append(&ga, tostr[p - fromstr]);
19883 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019884 ga_append(&ga, *in_str);
19885 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019886 }
19887 }
19888
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019889 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019890 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019891 ga_append(&ga, NUL);
19892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019894}
19895
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019896#ifdef FEAT_FLOAT
19897/*
19898 * "trunc({float})" function
19899 */
19900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019901f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019902{
19903 float_T f;
19904
19905 rettv->v_type = VAR_FLOAT;
19906 if (get_float_arg(argvars, &f) == OK)
19907 /* trunc() is not in C90, use floor() or ceil() instead. */
19908 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19909 else
19910 rettv->vval.v_float = 0.0;
19911}
19912#endif
19913
Bram Moolenaar8299df92004-07-10 09:47:34 +000019914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 * "type(expr)" function
19916 */
19917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019918f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019920 int n;
19921
19922 switch (argvars[0].v_type)
19923 {
19924 case VAR_NUMBER: n = 0; break;
19925 case VAR_STRING: n = 1; break;
19926 case VAR_FUNC: n = 2; break;
19927 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019928 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019929 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019930 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010019931 if (argvars[0].vval.v_number == VVAL_FALSE
19932 || argvars[0].vval.v_number == VVAL_TRUE)
19933 n = 6;
19934 else
19935 n = 7;
19936 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010019937 case VAR_JOB: n = 8; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010019938 case VAR_UNKNOWN:
19939 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
19940 n = -1;
19941 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019942 }
19943 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944}
19945
19946/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019947 * "undofile(name)" function
19948 */
19949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019950f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019951{
19952 rettv->v_type = VAR_STRING;
19953#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019954 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019955 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019956
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019957 if (*fname == NUL)
19958 {
19959 /* If there is no file name there will be no undo file. */
19960 rettv->vval.v_string = NULL;
19961 }
19962 else
19963 {
19964 char_u *ffname = FullName_save(fname, FALSE);
19965
19966 if (ffname != NULL)
19967 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19968 vim_free(ffname);
19969 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019970 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019971#else
19972 rettv->vval.v_string = NULL;
19973#endif
19974}
19975
19976/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019977 * "undotree()" function
19978 */
19979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019980f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019981{
19982 if (rettv_dict_alloc(rettv) == OK)
19983 {
19984 dict_T *dict = rettv->vval.v_dict;
19985 list_T *list;
19986
Bram Moolenaar730cde92010-06-27 05:18:54 +020019987 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019988 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019989 dict_add_nr_str(dict, "save_last",
19990 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019991 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19992 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019993 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019994
19995 list = list_alloc();
19996 if (list != NULL)
19997 {
19998 u_eval_tree(curbuf->b_u_oldhead, list);
19999 dict_add_list(dict, "entries", list);
20000 }
20001 }
20002}
20003
20004/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020005 * "values(dict)" function
20006 */
20007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020008f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020009{
20010 dict_list(argvars, rettv, 1);
20011}
20012
20013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 * "virtcol(string)" function
20015 */
20016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020017f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018{
20019 colnr_T vcol = 0;
20020 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020021 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020023 fp = var2fpos(&argvars[0], FALSE, &fnum);
20024 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20025 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 {
20027 getvvcol(curwin, fp, NULL, NULL, &vcol);
20028 ++vcol;
20029 }
20030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020031 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032}
20033
20034/*
20035 * "visualmode()" function
20036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020038f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 char_u str[2];
20041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 str[0] = curbuf->b_visual_mode_eval;
20044 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020045 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046
20047 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020048 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050}
20051
20052/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020053 * "wildmenumode()" function
20054 */
20055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020056f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020057{
20058#ifdef FEAT_WILDMENU
20059 if (wild_menu_showing)
20060 rettv->vval.v_number = 1;
20061#endif
20062}
20063
20064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 * "winbufnr(nr)" function
20066 */
20067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020068f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069{
20070 win_T *wp;
20071
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020072 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020074 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077}
20078
20079/*
20080 * "wincol()" function
20081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020083f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084{
20085 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020086 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087}
20088
20089/*
20090 * "winheight(nr)" function
20091 */
20092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020093f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094{
20095 win_T *wp;
20096
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020097 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020099 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020101 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102}
20103
20104/*
20105 * "winline()" function
20106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020108f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109{
20110 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020111 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112}
20113
20114/*
20115 * "winnr()" function
20116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020118f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119{
20120 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020121
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020123 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020125 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126}
20127
20128/*
20129 * "winrestcmd()" function
20130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020132f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133{
20134#ifdef FEAT_WINDOWS
20135 win_T *wp;
20136 int winnr = 1;
20137 garray_T ga;
20138 char_u buf[50];
20139
20140 ga_init2(&ga, (int)sizeof(char), 70);
20141 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20142 {
20143 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20144 ga_concat(&ga, buf);
20145# ifdef FEAT_VERTSPLIT
20146 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20147 ga_concat(&ga, buf);
20148# endif
20149 ++winnr;
20150 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020151 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020153 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020155 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020157 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158}
20159
20160/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020161 * "winrestview()" function
20162 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020164f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020165{
20166 dict_T *dict;
20167
20168 if (argvars[0].v_type != VAR_DICT
20169 || (dict = argvars[0].vval.v_dict) == NULL)
20170 EMSG(_(e_invarg));
20171 else
20172 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020173 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20174 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20175 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20176 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020177#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020178 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20179 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020180#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020181 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20182 {
20183 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20184 curwin->w_set_curswant = FALSE;
20185 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020186
Bram Moolenaar82c25852014-05-28 16:47:16 +020020187 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20188 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020189#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020190 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20191 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020192#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020193 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20194 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20195 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20196 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020197
20198 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020199 win_new_height(curwin, curwin->w_height);
20200# ifdef FEAT_VERTSPLIT
20201 win_new_width(curwin, W_WIDTH(curwin));
20202# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020203 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020204
Bram Moolenaarb851a962014-10-31 15:45:52 +010020205 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020206 curwin->w_topline = 1;
20207 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20208 curwin->w_topline = curbuf->b_ml.ml_line_count;
20209#ifdef FEAT_DIFF
20210 check_topfill(curwin, TRUE);
20211#endif
20212 }
20213}
20214
20215/*
20216 * "winsaveview()" function
20217 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020219f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020220{
20221 dict_T *dict;
20222
Bram Moolenaara800b422010-06-27 01:15:55 +020020223 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020224 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020225 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020226
20227 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20228 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20229#ifdef FEAT_VIRTUALEDIT
20230 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20231#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020232 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020233 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20234
20235 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20236#ifdef FEAT_DIFF
20237 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20238#endif
20239 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20240 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20241}
20242
20243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 * "winwidth(nr)" function
20245 */
20246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020247f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248{
20249 win_T *wp;
20250
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020251 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 else
20255#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020258 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259#endif
20260}
20261
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020263 * "wordcount()" function
20264 */
20265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020266f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020267{
20268 if (rettv_dict_alloc(rettv) == FAIL)
20269 return;
20270 cursor_pos_info(rettv->vval.v_dict);
20271}
20272
20273/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020274 * Write list of strings to file
20275 */
20276 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020277write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020278{
20279 listitem_T *li;
20280 int c;
20281 int ret = OK;
20282 char_u *s;
20283
20284 for (li = list->lv_first; li != NULL; li = li->li_next)
20285 {
20286 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20287 {
20288 if (*s == '\n')
20289 c = putc(NUL, fd);
20290 else
20291 c = putc(*s, fd);
20292 if (c == EOF)
20293 {
20294 ret = FAIL;
20295 break;
20296 }
20297 }
20298 if (!binary || li->li_next != NULL)
20299 if (putc('\n', fd) == EOF)
20300 {
20301 ret = FAIL;
20302 break;
20303 }
20304 if (ret == FAIL)
20305 {
20306 EMSG(_(e_write));
20307 break;
20308 }
20309 }
20310 return ret;
20311}
20312
20313/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020314 * "writefile()" function
20315 */
20316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020317f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020318{
20319 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020320 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020321 char_u *fname;
20322 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020323 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020324
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020325 if (check_restricted() || check_secure())
20326 return;
20327
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020328 if (argvars[0].v_type != VAR_LIST)
20329 {
20330 EMSG2(_(e_listarg), "writefile()");
20331 return;
20332 }
20333 if (argvars[0].vval.v_list == NULL)
20334 return;
20335
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020336 if (argvars[2].v_type != VAR_UNKNOWN)
20337 {
20338 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20339 binary = TRUE;
20340 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20341 append = TRUE;
20342 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020343
20344 /* Always open the file in binary mode, library functions have a mind of
20345 * their own about CR-LF conversion. */
20346 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020347 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20348 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020349 {
20350 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20351 ret = -1;
20352 }
20353 else
20354 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020355 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20356 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020357 fclose(fd);
20358 }
20359
20360 rettv->vval.v_number = ret;
20361}
20362
20363/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020364 * "xor(expr, expr)" function
20365 */
20366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020367f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020368{
20369 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20370 ^ get_tv_number_chk(&argvars[1], NULL);
20371}
20372
20373
20374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020376 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 */
20378 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020379var2fpos(
20380 typval_T *varp,
20381 int dollar_lnum, /* TRUE when $ is last line */
20382 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020384 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020386 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387
Bram Moolenaara5525202006-03-02 22:52:09 +000020388 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020389 if (varp->v_type == VAR_LIST)
20390 {
20391 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020392 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020393 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020394 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020395
20396 l = varp->vval.v_list;
20397 if (l == NULL)
20398 return NULL;
20399
20400 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020401 pos.lnum = list_find_nr(l, 0L, &error);
20402 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020403 return NULL; /* invalid line number */
20404
20405 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020406 pos.col = list_find_nr(l, 1L, &error);
20407 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020408 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020409 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020410
20411 /* We accept "$" for the column number: last column. */
20412 li = list_find(l, 1L);
20413 if (li != NULL && li->li_tv.v_type == VAR_STRING
20414 && li->li_tv.vval.v_string != NULL
20415 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20416 pos.col = len + 1;
20417
Bram Moolenaara5525202006-03-02 22:52:09 +000020418 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020419 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020420 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020421 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020422
Bram Moolenaara5525202006-03-02 22:52:09 +000020423#ifdef FEAT_VIRTUALEDIT
20424 /* Get the virtual offset. Defaults to zero. */
20425 pos.coladd = list_find_nr(l, 2L, &error);
20426 if (error)
20427 pos.coladd = 0;
20428#endif
20429
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020430 return &pos;
20431 }
20432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020433 name = get_tv_string_chk(varp);
20434 if (name == NULL)
20435 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020436 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020438 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20439 {
20440 if (VIsual_active)
20441 return &VIsual;
20442 return &curwin->w_cursor;
20443 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020444 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020446 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20448 return NULL;
20449 return pp;
20450 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020451
20452#ifdef FEAT_VIRTUALEDIT
20453 pos.coladd = 0;
20454#endif
20455
Bram Moolenaar477933c2007-07-17 14:32:23 +000020456 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020457 {
20458 pos.col = 0;
20459 if (name[1] == '0') /* "w0": first visible line */
20460 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020461 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020462 pos.lnum = curwin->w_topline;
20463 return &pos;
20464 }
20465 else if (name[1] == '$') /* "w$": last visible line */
20466 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020467 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020468 pos.lnum = curwin->w_botline - 1;
20469 return &pos;
20470 }
20471 }
20472 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020474 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 {
20476 pos.lnum = curbuf->b_ml.ml_line_count;
20477 pos.col = 0;
20478 }
20479 else
20480 {
20481 pos.lnum = curwin->w_cursor.lnum;
20482 pos.col = (colnr_T)STRLEN(ml_get_curline());
20483 }
20484 return &pos;
20485 }
20486 return NULL;
20487}
20488
20489/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020490 * Convert list in "arg" into a position and optional file number.
20491 * When "fnump" is NULL there is no file number, only 3 items.
20492 * Note that the column is passed on as-is, the caller may want to decrement
20493 * it to use 1 for the first column.
20494 * Return FAIL when conversion is not possible, doesn't check the position for
20495 * validity.
20496 */
20497 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020498list2fpos(
20499 typval_T *arg,
20500 pos_T *posp,
20501 int *fnump,
20502 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020503{
20504 list_T *l = arg->vval.v_list;
20505 long i = 0;
20506 long n;
20507
Bram Moolenaar493c1782014-05-28 14:34:46 +020020508 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20509 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020510 if (arg->v_type != VAR_LIST
20511 || l == NULL
20512 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020513 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020514 return FAIL;
20515
20516 if (fnump != NULL)
20517 {
20518 n = list_find_nr(l, i++, NULL); /* fnum */
20519 if (n < 0)
20520 return FAIL;
20521 if (n == 0)
20522 n = curbuf->b_fnum; /* current buffer */
20523 *fnump = n;
20524 }
20525
20526 n = list_find_nr(l, i++, NULL); /* lnum */
20527 if (n < 0)
20528 return FAIL;
20529 posp->lnum = n;
20530
20531 n = list_find_nr(l, i++, NULL); /* col */
20532 if (n < 0)
20533 return FAIL;
20534 posp->col = n;
20535
20536#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020537 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020538 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020539 posp->coladd = 0;
20540 else
20541 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020542#endif
20543
Bram Moolenaar493c1782014-05-28 14:34:46 +020020544 if (curswantp != NULL)
20545 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20546
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020547 return OK;
20548}
20549
20550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 * Get the length of an environment variable name.
20552 * Advance "arg" to the first character after the name.
20553 * Return 0 for error.
20554 */
20555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020556get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557{
20558 char_u *p;
20559 int len;
20560
20561 for (p = *arg; vim_isIDc(*p); ++p)
20562 ;
20563 if (p == *arg) /* no name found */
20564 return 0;
20565
20566 len = (int)(p - *arg);
20567 *arg = p;
20568 return len;
20569}
20570
20571/*
20572 * Get the length of the name of a function or internal variable.
20573 * "arg" is advanced to the first non-white character after the name.
20574 * Return 0 if something is wrong.
20575 */
20576 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020577get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578{
20579 char_u *p;
20580 int len;
20581
20582 /* Find the end of the name. */
20583 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020584 {
20585 if (*p == ':')
20586 {
20587 /* "s:" is start of "s:var", but "n:" is not and can be used in
20588 * slice "[n:]". Also "xx:" is not a namespace. */
20589 len = (int)(p - *arg);
20590 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20591 || len > 1)
20592 break;
20593 }
20594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 if (p == *arg) /* no name found */
20596 return 0;
20597
20598 len = (int)(p - *arg);
20599 *arg = skipwhite(p);
20600
20601 return len;
20602}
20603
20604/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020605 * Get the length of the name of a variable or function.
20606 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020608 * Return -1 if curly braces expansion failed.
20609 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 * If the name contains 'magic' {}'s, expand them and return the
20611 * expanded name in an allocated string via 'alias' - caller must free.
20612 */
20613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020614get_name_len(
20615 char_u **arg,
20616 char_u **alias,
20617 int evaluate,
20618 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619{
20620 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 char_u *p;
20622 char_u *expr_start;
20623 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624
20625 *alias = NULL; /* default to no alias */
20626
20627 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20628 && (*arg)[2] == (int)KE_SNR)
20629 {
20630 /* hard coded <SNR>, already translated */
20631 *arg += 3;
20632 return get_id_len(arg) + 3;
20633 }
20634 len = eval_fname_script(*arg);
20635 if (len > 0)
20636 {
20637 /* literal "<SID>", "s:" or "<SNR>" */
20638 *arg += len;
20639 }
20640
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020642 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020644 p = find_name_end(*arg, &expr_start, &expr_end,
20645 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 if (expr_start != NULL)
20647 {
20648 char_u *temp_string;
20649
20650 if (!evaluate)
20651 {
20652 len += (int)(p - *arg);
20653 *arg = skipwhite(p);
20654 return len;
20655 }
20656
20657 /*
20658 * Include any <SID> etc in the expanded string:
20659 * Thus the -len here.
20660 */
20661 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20662 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020663 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 *alias = temp_string;
20665 *arg = skipwhite(p);
20666 return (int)STRLEN(temp_string);
20667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668
20669 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020670 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 EMSG2(_(e_invexpr2), *arg);
20672
20673 return len;
20674}
20675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020676/*
20677 * Find the end of a variable or function name, taking care of magic braces.
20678 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20679 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020680 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020681 * Return a pointer to just after the name. Equal to "arg" if there is no
20682 * valid name.
20683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020685find_name_end(
20686 char_u *arg,
20687 char_u **expr_start,
20688 char_u **expr_end,
20689 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020691 int mb_nest = 0;
20692 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020694 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020696 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020698 *expr_start = NULL;
20699 *expr_end = NULL;
20700 }
20701
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020702 /* Quick check for valid starting character. */
20703 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20704 return arg;
20705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020706 for (p = arg; *p != NUL
20707 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020708 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020709 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020710 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020711 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020712 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020713 if (*p == '\'')
20714 {
20715 /* skip over 'string' to avoid counting [ and ] inside it. */
20716 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20717 ;
20718 if (*p == NUL)
20719 break;
20720 }
20721 else if (*p == '"')
20722 {
20723 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20724 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20725 if (*p == '\\' && p[1] != NUL)
20726 ++p;
20727 if (*p == NUL)
20728 break;
20729 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020730 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20731 {
20732 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020733 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020734 len = (int)(p - arg);
20735 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020736 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020737 break;
20738 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020740 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742 if (*p == '[')
20743 ++br_nest;
20744 else if (*p == ']')
20745 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020748 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020750 if (*p == '{')
20751 {
20752 mb_nest++;
20753 if (expr_start != NULL && *expr_start == NULL)
20754 *expr_start = p;
20755 }
20756 else if (*p == '}')
20757 {
20758 mb_nest--;
20759 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20760 *expr_end = p;
20761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763 }
20764
20765 return p;
20766}
20767
20768/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020769 * Expands out the 'magic' {}'s in a variable/function name.
20770 * Note that this can call itself recursively, to deal with
20771 * constructs like foo{bar}{baz}{bam}
20772 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20773 * "in_start" ^
20774 * "expr_start" ^
20775 * "expr_end" ^
20776 * "in_end" ^
20777 *
20778 * Returns a new allocated string, which the caller must free.
20779 * Returns NULL for failure.
20780 */
20781 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020782make_expanded_name(
20783 char_u *in_start,
20784 char_u *expr_start,
20785 char_u *expr_end,
20786 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020787{
20788 char_u c1;
20789 char_u *retval = NULL;
20790 char_u *temp_result;
20791 char_u *nextcmd = NULL;
20792
20793 if (expr_end == NULL || in_end == NULL)
20794 return NULL;
20795 *expr_start = NUL;
20796 *expr_end = NUL;
20797 c1 = *in_end;
20798 *in_end = NUL;
20799
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020800 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020801 if (temp_result != NULL && nextcmd == NULL)
20802 {
20803 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20804 + (in_end - expr_end) + 1));
20805 if (retval != NULL)
20806 {
20807 STRCPY(retval, in_start);
20808 STRCAT(retval, temp_result);
20809 STRCAT(retval, expr_end + 1);
20810 }
20811 }
20812 vim_free(temp_result);
20813
20814 *in_end = c1; /* put char back for error messages */
20815 *expr_start = '{';
20816 *expr_end = '}';
20817
20818 if (retval != NULL)
20819 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020820 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020821 if (expr_start != NULL)
20822 {
20823 /* Further expansion! */
20824 temp_result = make_expanded_name(retval, expr_start,
20825 expr_end, temp_result);
20826 vim_free(retval);
20827 retval = temp_result;
20828 }
20829 }
20830
20831 return retval;
20832}
20833
20834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020836 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 */
20838 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020839eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020841 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20842}
20843
20844/*
20845 * Return TRUE if character "c" can be used as the first character in a
20846 * variable or function name (excluding '{' and '}').
20847 */
20848 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020849eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020850{
20851 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852}
20853
20854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 * Set number v: variable to "val".
20856 */
20857 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020858set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020860 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861}
20862
20863/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020864 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865 */
20866 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020867get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020869 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870}
20871
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020872/*
20873 * Get string v: variable value. Uses a static buffer, can only be used once.
20874 */
20875 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020876get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020877{
20878 return get_tv_string(&vimvars[idx].vv_tv);
20879}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020880
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020882 * Get List v: variable value. Caller must take care of reference count when
20883 * needed.
20884 */
20885 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020886get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020887{
20888 return vimvars[idx].vv_list;
20889}
20890
20891/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020892 * Set v:char to character "c".
20893 */
20894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020895set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020896{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020897 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020898
20899#ifdef FEAT_MBYTE
20900 if (has_mbyte)
20901 buf[(*mb_char2bytes)(c, buf)] = NUL;
20902 else
20903#endif
20904 {
20905 buf[0] = c;
20906 buf[1] = NUL;
20907 }
20908 set_vim_var_string(VV_CHAR, buf, -1);
20909}
20910
20911/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020912 * Set v:count to "count" and v:count1 to "count1".
20913 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 */
20915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020916set_vcount(
20917 long count,
20918 long count1,
20919 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020921 if (set_prevcount)
20922 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020923 vimvars[VV_COUNT].vv_nr = count;
20924 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925}
20926
20927/*
20928 * Set string v: variable to a copy of "val".
20929 */
20930 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020931set_vim_var_string(
20932 int idx,
20933 char_u *val,
20934 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935{
Bram Moolenaara542c682016-01-31 16:28:04 +010020936 clear_tv(&vimvars[idx].vv_di.di_tv);
20937 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020939 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020941 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020943 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944}
20945
20946/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020947 * Set List v: variable to "val".
20948 */
20949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020950set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020951{
Bram Moolenaara542c682016-01-31 16:28:04 +010020952 clear_tv(&vimvars[idx].vv_di.di_tv);
20953 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020954 vimvars[idx].vv_list = val;
20955 if (val != NULL)
20956 ++val->lv_refcount;
20957}
20958
20959/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020960 * Set Dictionary v: variable to "val".
20961 */
20962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020963set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020964{
20965 int todo;
20966 hashitem_T *hi;
20967
Bram Moolenaara542c682016-01-31 16:28:04 +010020968 clear_tv(&vimvars[idx].vv_di.di_tv);
20969 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020970 vimvars[idx].vv_dict = val;
20971 if (val != NULL)
20972 {
20973 ++val->dv_refcount;
20974
20975 /* Set readonly */
20976 todo = (int)val->dv_hashtab.ht_used;
20977 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20978 {
20979 if (HASHITEM_EMPTY(hi))
20980 continue;
20981 --todo;
20982 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20983 }
20984 }
20985}
20986
20987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 * Set v:register if needed.
20989 */
20990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020991set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992{
20993 char_u regname;
20994
20995 if (c == 0 || c == ' ')
20996 regname = '"';
20997 else
20998 regname = c;
20999 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021000 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 set_vim_var_string(VV_REG, &regname, 1);
21002}
21003
21004/*
21005 * Get or set v:exception. If "oldval" == NULL, return the current value.
21006 * Otherwise, restore the value to "oldval" and return NULL.
21007 * Must always be called in pairs to save and restore v:exception! Does not
21008 * take care of memory allocations.
21009 */
21010 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021011v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012{
21013 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021014 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015
Bram Moolenaare9a41262005-01-15 22:18:47 +000021016 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 return NULL;
21018}
21019
21020/*
21021 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21022 * Otherwise, restore the value to "oldval" and return NULL.
21023 * Must always be called in pairs to save and restore v:throwpoint! Does not
21024 * take care of memory allocations.
21025 */
21026 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021027v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028{
21029 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021030 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031
Bram Moolenaare9a41262005-01-15 22:18:47 +000021032 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 return NULL;
21034}
21035
21036#if defined(FEAT_AUTOCMD) || defined(PROTO)
21037/*
21038 * Set v:cmdarg.
21039 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21040 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21041 * Must always be called in pairs!
21042 */
21043 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021044set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045{
21046 char_u *oldval;
21047 char_u *newval;
21048 unsigned len;
21049
Bram Moolenaare9a41262005-01-15 22:18:47 +000021050 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021051 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021053 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021054 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021055 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056 }
21057
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021058 if (eap->force_bin == FORCE_BIN)
21059 len = 6;
21060 else if (eap->force_bin == FORCE_NOBIN)
21061 len = 8;
21062 else
21063 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021064
21065 if (eap->read_edit)
21066 len += 7;
21067
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021068 if (eap->force_ff != 0)
21069 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21070# ifdef FEAT_MBYTE
21071 if (eap->force_enc != 0)
21072 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021073 if (eap->bad_char != 0)
21074 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021075# endif
21076
21077 newval = alloc(len + 1);
21078 if (newval == NULL)
21079 return NULL;
21080
21081 if (eap->force_bin == FORCE_BIN)
21082 sprintf((char *)newval, " ++bin");
21083 else if (eap->force_bin == FORCE_NOBIN)
21084 sprintf((char *)newval, " ++nobin");
21085 else
21086 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021087
21088 if (eap->read_edit)
21089 STRCAT(newval, " ++edit");
21090
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021091 if (eap->force_ff != 0)
21092 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21093 eap->cmd + eap->force_ff);
21094# ifdef FEAT_MBYTE
21095 if (eap->force_enc != 0)
21096 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21097 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021098 if (eap->bad_char == BAD_KEEP)
21099 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21100 else if (eap->bad_char == BAD_DROP)
21101 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21102 else if (eap->bad_char != 0)
21103 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021104# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021105 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021106 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107}
21108#endif
21109
21110/*
21111 * Get the value of internal variable "name".
21112 * Return OK or FAIL.
21113 */
21114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021115get_var_tv(
21116 char_u *name,
21117 int len, /* length of "name" */
21118 typval_T *rettv, /* NULL when only checking existence */
21119 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21120 int verbose, /* may give error message */
21121 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122{
21123 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021124 typval_T *tv = NULL;
21125 typval_T atv;
21126 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128
21129 /* truncate the name, so that we can use strcmp() */
21130 cc = name[len];
21131 name[len] = NUL;
21132
21133 /*
21134 * Check for "b:changedtick".
21135 */
21136 if (STRCMP(name, "b:changedtick") == 0)
21137 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021138 atv.v_type = VAR_NUMBER;
21139 atv.vval.v_number = curbuf->b_changedtick;
21140 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 }
21142
21143 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 * Check for user-defined variables.
21145 */
21146 else
21147 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021148 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021150 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021151 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021152 if (dip != NULL)
21153 *dip = v;
21154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155 }
21156
Bram Moolenaare9a41262005-01-15 22:18:47 +000021157 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021159 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021160 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 ret = FAIL;
21162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021163 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021164 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165
21166 name[len] = cc;
21167
21168 return ret;
21169}
21170
21171/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021172 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21173 * Also handle function call with Funcref variable: func(expr)
21174 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21175 */
21176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021177handle_subscript(
21178 char_u **arg,
21179 typval_T *rettv,
21180 int evaluate, /* do more than finding the end */
21181 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021182{
21183 int ret = OK;
21184 dict_T *selfdict = NULL;
21185 char_u *s;
21186 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021187 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021188
21189 while (ret == OK
21190 && (**arg == '['
21191 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021192 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021193 && !vim_iswhite(*(*arg - 1)))
21194 {
21195 if (**arg == '(')
21196 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021197 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021198 if (evaluate)
21199 {
21200 functv = *rettv;
21201 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021202
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021203 /* Invoke the function. Recursive! */
21204 s = functv.vval.v_string;
21205 }
21206 else
21207 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021208 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021209 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21210 &len, evaluate, selfdict);
21211
21212 /* Clear the funcref afterwards, so that deleting it while
21213 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021214 if (evaluate)
21215 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021216
21217 /* Stop the expression evaluation when immediately aborting on
21218 * error, or when an interrupt occurred or an exception was thrown
21219 * but not caught. */
21220 if (aborting())
21221 {
21222 if (ret == OK)
21223 clear_tv(rettv);
21224 ret = FAIL;
21225 }
21226 dict_unref(selfdict);
21227 selfdict = NULL;
21228 }
21229 else /* **arg == '[' || **arg == '.' */
21230 {
21231 dict_unref(selfdict);
21232 if (rettv->v_type == VAR_DICT)
21233 {
21234 selfdict = rettv->vval.v_dict;
21235 if (selfdict != NULL)
21236 ++selfdict->dv_refcount;
21237 }
21238 else
21239 selfdict = NULL;
21240 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21241 {
21242 clear_tv(rettv);
21243 ret = FAIL;
21244 }
21245 }
21246 }
21247 dict_unref(selfdict);
21248 return ret;
21249}
21250
21251/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021252 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021253 * value).
21254 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021255 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021256alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021257{
Bram Moolenaar33570922005-01-25 22:26:29 +000021258 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021259}
21260
21261/*
21262 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 * The string "s" must have been allocated, it is consumed.
21264 * Return NULL for out of memory, the variable otherwise.
21265 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021266 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021267alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268{
Bram Moolenaar33570922005-01-25 22:26:29 +000021269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021271 rettv = alloc_tv();
21272 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021274 rettv->v_type = VAR_STRING;
21275 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 }
21277 else
21278 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021279 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280}
21281
21282/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021283 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021286free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287{
21288 if (varp != NULL)
21289 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021290 switch (varp->v_type)
21291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021292 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021293 func_unref(varp->vval.v_string);
21294 /*FALLTHROUGH*/
21295 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021296 vim_free(varp->vval.v_string);
21297 break;
21298 case VAR_LIST:
21299 list_unref(varp->vval.v_list);
21300 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021301 case VAR_DICT:
21302 dict_unref(varp->vval.v_dict);
21303 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021304 case VAR_JOB:
21305#ifdef FEAT_JOB
21306 job_unref(varp->vval.v_job);
21307 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021308#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021309 case VAR_NUMBER:
21310 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021311 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021312 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021313 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 vim_free(varp);
21316 }
21317}
21318
21319/*
21320 * Free the memory for a variable value and set the value to NULL or 0.
21321 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021322 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021323clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324{
21325 if (varp != NULL)
21326 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021327 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021329 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021330 func_unref(varp->vval.v_string);
21331 /*FALLTHROUGH*/
21332 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021333 vim_free(varp->vval.v_string);
21334 varp->vval.v_string = NULL;
21335 break;
21336 case VAR_LIST:
21337 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021338 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021339 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021340 case VAR_DICT:
21341 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021342 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021343 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021344 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021345 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021346 varp->vval.v_number = 0;
21347 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021348 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021349#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021350 varp->vval.v_float = 0.0;
21351 break;
21352#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021353 case VAR_JOB:
21354#ifdef FEAT_JOB
21355 job_unref(varp->vval.v_job);
21356 varp->vval.v_job = NULL;
21357#endif
21358 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021359 case VAR_UNKNOWN:
21360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021362 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 }
21364}
21365
21366/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367 * Set the value of a variable to NULL without freeing items.
21368 */
21369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021370init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021371{
21372 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021373 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021374}
21375
21376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 * Get the number value of a variable.
21378 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021379 * For incompatible types, return 0.
21380 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21381 * caller of incompatible types: it sets *denote to TRUE if "denote"
21382 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 */
21384 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021385get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021387 int error = FALSE;
21388
21389 return get_tv_number_chk(varp, &error); /* return 0L on error */
21390}
21391
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021392 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021393get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021394{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021395 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021397 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021399 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021400 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021401 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021402#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021403 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021404 break;
21405#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021406 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021407 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021408 break;
21409 case VAR_STRING:
21410 if (varp->vval.v_string != NULL)
21411 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021412 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021413 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021414 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021415 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021416 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021417 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021418 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021419 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021420 case VAR_SPECIAL:
21421 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21422 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021423 case VAR_JOB:
21424#ifdef FEAT_JOB
21425 EMSG(_("E910: Using a Job as a Number"));
21426 break;
21427#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021428 case VAR_UNKNOWN:
21429 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021430 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021432 if (denote == NULL) /* useful for values that must be unsigned */
21433 n = -1;
21434 else
21435 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021436 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437}
21438
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021439#ifdef FEAT_FLOAT
21440 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021441get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021442{
21443 switch (varp->v_type)
21444 {
21445 case VAR_NUMBER:
21446 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021447 case VAR_FLOAT:
21448 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021449 case VAR_FUNC:
21450 EMSG(_("E891: Using a Funcref as a Float"));
21451 break;
21452 case VAR_STRING:
21453 EMSG(_("E892: Using a String as a Float"));
21454 break;
21455 case VAR_LIST:
21456 EMSG(_("E893: Using a List as a Float"));
21457 break;
21458 case VAR_DICT:
21459 EMSG(_("E894: Using a Dictionary as a Float"));
21460 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021461 case VAR_SPECIAL:
21462 EMSG(_("E907: Using a special value as a Float"));
21463 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021464 case VAR_JOB:
21465# ifdef FEAT_JOB
21466 EMSG(_("E911: Using a Job as a Float"));
21467 break;
21468# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021469 case VAR_UNKNOWN:
21470 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021471 break;
21472 }
21473 return 0;
21474}
21475#endif
21476
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021478 * Get the lnum from the first argument.
21479 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021480 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 */
21482 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021483get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484{
Bram Moolenaar33570922005-01-25 22:26:29 +000021485 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 linenr_T lnum;
21487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021488 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 if (lnum == 0) /* no valid number, try using line() */
21490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021491 rettv.v_type = VAR_NUMBER;
21492 f_line(argvars, &rettv);
21493 lnum = rettv.vval.v_number;
21494 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 }
21496 return lnum;
21497}
21498
21499/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021500 * Get the lnum from the first argument.
21501 * Also accepts "$", then "buf" is used.
21502 * Returns 0 on error.
21503 */
21504 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021505get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021506{
21507 if (argvars[0].v_type == VAR_STRING
21508 && argvars[0].vval.v_string != NULL
21509 && argvars[0].vval.v_string[0] == '$'
21510 && buf != NULL)
21511 return buf->b_ml.ml_line_count;
21512 return get_tv_number_chk(&argvars[0], NULL);
21513}
21514
21515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 * Get the string value of a variable.
21517 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021518 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21519 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 * If the String variable has never been set, return an empty string.
21521 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021522 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21523 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 */
21525 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021526get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527{
21528 static char_u mybuf[NUMBUFLEN];
21529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021530 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531}
21532
21533 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021534get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021536 char_u *res = get_tv_string_buf_chk(varp, buf);
21537
21538 return res != NULL ? res : (char_u *)"";
21539}
21540
Bram Moolenaar7d647822014-04-05 21:28:56 +020021541/*
21542 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21543 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021544 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021545get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021546{
21547 static char_u mybuf[NUMBUFLEN];
21548
21549 return get_tv_string_buf_chk(varp, mybuf);
21550}
21551
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021553get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021554{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021555 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021557 case VAR_NUMBER:
21558 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21559 return buf;
21560 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021561 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021562 break;
21563 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021564 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021565 break;
21566 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021567 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021568 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021569 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021570#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021571 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021572 break;
21573#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021574 case VAR_STRING:
21575 if (varp->vval.v_string != NULL)
21576 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021577 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021578 case VAR_SPECIAL:
21579 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21580 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021581 case VAR_JOB:
21582#ifdef FEAT_JOB
21583 {
21584 job_T *job = varp->vval.v_job;
21585 char *status = job->jv_status == JOB_FAILED ? "fail"
21586 : job->jv_status == JOB_ENDED ? "dead"
21587 : "run";
21588# ifdef UNIX
21589 vim_snprintf((char *)buf, NUMBUFLEN,
21590 "process %ld %s", (long)job->jv_pid, status);
21591# else
21592 /* TODO */
21593 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21594# endif
21595 return buf;
21596 }
21597#endif
21598 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021599 case VAR_UNKNOWN:
21600 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021601 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021603 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604}
21605
21606/*
21607 * Find variable "name" in the list of variables.
21608 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021609 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021610 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021611 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021613 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021614find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021617 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618
Bram Moolenaara7043832005-01-21 11:56:39 +000021619 ht = find_var_ht(name, &varname);
21620 if (htp != NULL)
21621 *htp = ht;
21622 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021624 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625}
21626
21627/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021628 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021629 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021631 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021632find_var_in_ht(
21633 hashtab_T *ht,
21634 int htname,
21635 char_u *varname,
21636 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021637{
Bram Moolenaar33570922005-01-25 22:26:29 +000021638 hashitem_T *hi;
21639
21640 if (*varname == NUL)
21641 {
21642 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021643 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021644 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021645 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021646 case 'g': return &globvars_var;
21647 case 'v': return &vimvars_var;
21648 case 'b': return &curbuf->b_bufvar;
21649 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021650#ifdef FEAT_WINDOWS
21651 case 't': return &curtab->tp_winvar;
21652#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021653 case 'l': return current_funccal == NULL
21654 ? NULL : &current_funccal->l_vars_var;
21655 case 'a': return current_funccal == NULL
21656 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021657 }
21658 return NULL;
21659 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021660
21661 hi = hash_find(ht, varname);
21662 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021663 {
21664 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021665 * worked find the variable again. Don't auto-load a script if it was
21666 * loaded already, otherwise it would be loaded every time when
21667 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021668 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021669 {
21670 /* Note: script_autoload() may make "hi" invalid. It must either
21671 * be obtained again or not used. */
21672 if (!script_autoload(varname, FALSE) || aborting())
21673 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021674 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021675 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021676 if (HASHITEM_EMPTY(hi))
21677 return NULL;
21678 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021679 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021680}
21681
21682/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021684 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021685 * Set "varname" to the start of name without ':'.
21686 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021687 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021688find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021690 hashitem_T *hi;
21691
Bram Moolenaar73627d02015-08-11 15:46:09 +020021692 if (name[0] == NUL)
21693 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 if (name[1] != ':')
21695 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021696 /* The name must not start with a colon or #. */
21697 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 return NULL;
21699 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021700
21701 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021702 hi = hash_find(&compat_hashtab, name);
21703 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021704 return &compat_hashtab;
21705
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021707 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021708 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 }
21710 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021711 if (*name == 'g') /* global variable */
21712 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021713 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21714 */
21715 if (vim_strchr(name + 2, ':') != NULL
21716 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021717 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021719 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021720 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021721 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021722#ifdef FEAT_WINDOWS
21723 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021724 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021725#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021726 if (*name == 'v') /* v: variable */
21727 return &vimvarht;
21728 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021729 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021730 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021731 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 if (*name == 's' /* script variable */
21733 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21734 return &SCRIPT_VARS(current_SID);
21735 return NULL;
21736}
21737
21738/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021739 * Get function call environment based on bactrace debug level
21740 */
21741 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021742get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021743{
21744 int i;
21745 funccall_T *funccal;
21746 funccall_T *temp_funccal;
21747
21748 funccal = current_funccal;
21749 if (debug_backtrace_level > 0)
21750 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021751 for (i = 0; i < debug_backtrace_level; i++)
21752 {
21753 temp_funccal = funccal->caller;
21754 if (temp_funccal)
21755 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021756 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021757 /* backtrace level overflow. reset to max */
21758 debug_backtrace_level = i;
21759 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021760 }
21761 return funccal;
21762}
21763
21764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021766 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 * Returns NULL when it doesn't exist.
21768 */
21769 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021770get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771{
Bram Moolenaar33570922005-01-25 22:26:29 +000021772 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021774 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 if (v == NULL)
21776 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021777 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778}
21779
21780/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021781 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 * sourcing this script and when executing functions defined in the script.
21783 */
21784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021785new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786{
Bram Moolenaara7043832005-01-21 11:56:39 +000021787 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 hashtab_T *ht;
21789 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021790
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21792 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021793 /* Re-allocating ga_data means that an ht_array pointing to
21794 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021795 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021796 for (i = 1; i <= ga_scripts.ga_len; ++i)
21797 {
21798 ht = &SCRIPT_VARS(i);
21799 if (ht->ht_mask == HT_INIT_SIZE - 1)
21800 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021801 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021802 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021803 }
21804
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805 while (ga_scripts.ga_len < id)
21806 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021807 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021808 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021809 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 }
21812 }
21813}
21814
21815/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021816 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21817 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 */
21819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021820init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821{
Bram Moolenaar33570922005-01-25 22:26:29 +000021822 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021823 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021824 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021825 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021826 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 dict_var->di_tv.vval.v_dict = dict;
21828 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021829 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021830 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21831 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832}
21833
21834/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021835 * Unreference a dictionary initialized by init_var_dict().
21836 */
21837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021838unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021839{
21840 /* Now the dict needs to be freed if no one else is using it, go back to
21841 * normal reference counting. */
21842 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21843 dict_unref(dict);
21844}
21845
21846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021848 * Frees all allocated variables and the value they contain.
21849 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 */
21851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021852vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021853{
21854 vars_clear_ext(ht, TRUE);
21855}
21856
21857/*
21858 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21859 */
21860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021861vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862{
Bram Moolenaara7043832005-01-21 11:56:39 +000021863 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 hashitem_T *hi;
21865 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021868 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021869 for (hi = ht->ht_array; todo > 0; ++hi)
21870 {
21871 if (!HASHITEM_EMPTY(hi))
21872 {
21873 --todo;
21874
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021876 * ht_array might change then. hash_clear() takes care of it
21877 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021878 v = HI2DI(hi);
21879 if (free_val)
21880 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021881 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021883 }
21884 }
21885 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021886 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887}
21888
Bram Moolenaara7043832005-01-21 11:56:39 +000021889/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021890 * Delete a variable from hashtab "ht" at item "hi".
21891 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021894delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895{
Bram Moolenaar33570922005-01-25 22:26:29 +000021896 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021897
21898 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021899 clear_tv(&di->di_tv);
21900 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901}
21902
21903/*
21904 * List the value of one internal variable.
21905 */
21906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021907list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021909 char_u *tofree;
21910 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021911 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021912
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021913 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021915 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021916 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917}
21918
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021920list_one_var_a(
21921 char_u *prefix,
21922 char_u *name,
21923 int type,
21924 char_u *string,
21925 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926{
Bram Moolenaar31859182007-08-14 20:41:13 +000021927 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21928 msg_start();
21929 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 if (name != NULL) /* "a:" vars don't have a name stored */
21931 msg_puts(name);
21932 msg_putchar(' ');
21933 msg_advance(22);
21934 if (type == VAR_NUMBER)
21935 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021936 else if (type == VAR_FUNC)
21937 msg_putchar('*');
21938 else if (type == VAR_LIST)
21939 {
21940 msg_putchar('[');
21941 if (*string == '[')
21942 ++string;
21943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021944 else if (type == VAR_DICT)
21945 {
21946 msg_putchar('{');
21947 if (*string == '{')
21948 ++string;
21949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 else
21951 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021952
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021954
21955 if (type == VAR_FUNC)
21956 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021957 if (*first)
21958 {
21959 msg_clr_eos();
21960 *first = FALSE;
21961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962}
21963
21964/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021965 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 * If the variable already exists, the value is updated.
21967 * Otherwise the variable is created.
21968 */
21969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021970set_var(
21971 char_u *name,
21972 typval_T *tv,
21973 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974{
Bram Moolenaar33570922005-01-25 22:26:29 +000021975 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021977 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021979 ht = find_var_ht(name, &varname);
21980 if (ht == NULL || *varname == NUL)
21981 {
21982 EMSG2(_(e_illvar), name);
21983 return;
21984 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021985 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021986
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021987 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21988 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021989
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021992 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021993 if (var_check_ro(v->di_flags, name, FALSE)
21994 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021995 return;
21996 if (v->di_tv.v_type != tv->v_type
21997 && !((v->di_tv.v_type == VAR_STRING
21998 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021999 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022000 || tv->v_type == VAR_NUMBER))
22001#ifdef FEAT_FLOAT
22002 && !((v->di_tv.v_type == VAR_NUMBER
22003 || v->di_tv.v_type == VAR_FLOAT)
22004 && (tv->v_type == VAR_NUMBER
22005 || tv->v_type == VAR_FLOAT))
22006#endif
22007 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022008 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022009 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022010 return;
22011 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022012
22013 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022014 * Handle setting internal v: variables separately where needed to
22015 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022016 */
22017 if (ht == &vimvarht)
22018 {
22019 if (v->di_tv.v_type == VAR_STRING)
22020 {
22021 vim_free(v->di_tv.vval.v_string);
22022 if (copy || tv->v_type != VAR_STRING)
22023 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22024 else
22025 {
22026 /* Take over the string to avoid an extra alloc/free. */
22027 v->di_tv.vval.v_string = tv->vval.v_string;
22028 tv->vval.v_string = NULL;
22029 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022030 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022031 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022032 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022033 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022034 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022035 if (STRCMP(varname, "searchforward") == 0)
22036 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022037#ifdef FEAT_SEARCH_EXTRA
22038 else if (STRCMP(varname, "hlsearch") == 0)
22039 {
22040 no_hlsearch = !v->di_tv.vval.v_number;
22041 redraw_all_later(SOME_VALID);
22042 }
22043#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022044 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022045 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022046 else if (v->di_tv.v_type != tv->v_type)
22047 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022048 }
22049
22050 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 }
22052 else /* add a new variable */
22053 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022054 /* Can't add "v:" variable. */
22055 if (ht == &vimvarht)
22056 {
22057 EMSG2(_(e_illvar), name);
22058 return;
22059 }
22060
Bram Moolenaar92124a32005-06-17 22:03:40 +000022061 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022062 if (!valid_varname(varname))
22063 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022064
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022065 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22066 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022067 if (v == NULL)
22068 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022069 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022070 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022072 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 return;
22074 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022075 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022077
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022078 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022079 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022080 else
22081 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022082 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022083 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022084 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086}
22087
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022088/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022089 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022090 * Also give an error message.
22091 */
22092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022093var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022094{
22095 if (flags & DI_FLAGS_RO)
22096 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022097 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022098 return TRUE;
22099 }
22100 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22101 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022102 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022103 return TRUE;
22104 }
22105 return FALSE;
22106}
22107
22108/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022109 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22110 * Also give an error message.
22111 */
22112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022113var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022114{
22115 if (flags & DI_FLAGS_FIX)
22116 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022117 EMSG2(_("E795: Cannot delete variable %s"),
22118 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022119 return TRUE;
22120 }
22121 return FALSE;
22122}
22123
22124/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022125 * Check if a funcref is assigned to a valid variable name.
22126 * Return TRUE and give an error if not.
22127 */
22128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022129var_check_func_name(
22130 char_u *name, /* points to start of variable name */
22131 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022132{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022133 /* Allow for w: b: s: and t:. */
22134 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022135 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22136 ? name[2] : name[0]))
22137 {
22138 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22139 name);
22140 return TRUE;
22141 }
22142 /* Don't allow hiding a function. When "v" is not NULL we might be
22143 * assigning another function to the same var, the type is checked
22144 * below. */
22145 if (new_var && function_exists(name))
22146 {
22147 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22148 name);
22149 return TRUE;
22150 }
22151 return FALSE;
22152}
22153
22154/*
22155 * Check if a variable name is valid.
22156 * Return FALSE and give an error if not.
22157 */
22158 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022159valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022160{
22161 char_u *p;
22162
22163 for (p = varname; *p != NUL; ++p)
22164 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22165 && *p != AUTOLOAD_CHAR)
22166 {
22167 EMSG2(_(e_illvar), varname);
22168 return FALSE;
22169 }
22170 return TRUE;
22171}
22172
22173/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022174 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022175 * Also give an error message, using "name" or _("name") when use_gettext is
22176 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022177 */
22178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022179tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022180{
22181 if (lock & VAR_LOCKED)
22182 {
22183 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022184 name == NULL ? (char_u *)_("Unknown")
22185 : use_gettext ? (char_u *)_(name)
22186 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022187 return TRUE;
22188 }
22189 if (lock & VAR_FIXED)
22190 {
22191 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022192 name == NULL ? (char_u *)_("Unknown")
22193 : use_gettext ? (char_u *)_(name)
22194 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022195 return TRUE;
22196 }
22197 return FALSE;
22198}
22199
22200/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022202 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022203 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022204 * It is OK for "from" and "to" to point to the same item. This is used to
22205 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022206 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022208copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022210 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022211 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022212 switch (from->v_type)
22213 {
22214 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022215 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022216 to->vval.v_number = from->vval.v_number;
22217 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022218 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022219#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022220 to->vval.v_float = from->vval.v_float;
22221 break;
22222#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022223 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022224#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022225 to->vval.v_job = from->vval.v_job;
22226 ++to->vval.v_job->jv_refcount;
22227 break;
22228#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022229 case VAR_STRING:
22230 case VAR_FUNC:
22231 if (from->vval.v_string == NULL)
22232 to->vval.v_string = NULL;
22233 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022234 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022235 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022236 if (from->v_type == VAR_FUNC)
22237 func_ref(to->vval.v_string);
22238 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022239 break;
22240 case VAR_LIST:
22241 if (from->vval.v_list == NULL)
22242 to->vval.v_list = NULL;
22243 else
22244 {
22245 to->vval.v_list = from->vval.v_list;
22246 ++to->vval.v_list->lv_refcount;
22247 }
22248 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022249 case VAR_DICT:
22250 if (from->vval.v_dict == NULL)
22251 to->vval.v_dict = NULL;
22252 else
22253 {
22254 to->vval.v_dict = from->vval.v_dict;
22255 ++to->vval.v_dict->dv_refcount;
22256 }
22257 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022258 case VAR_UNKNOWN:
22259 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022260 break;
22261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262}
22263
22264/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022265 * Make a copy of an item.
22266 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022267 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22268 * reference to an already copied list/dict can be used.
22269 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022270 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022271 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022272item_copy(
22273 typval_T *from,
22274 typval_T *to,
22275 int deep,
22276 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022277{
22278 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022279 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022280
Bram Moolenaar33570922005-01-25 22:26:29 +000022281 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022282 {
22283 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022284 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022285 }
22286 ++recurse;
22287
22288 switch (from->v_type)
22289 {
22290 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022291 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022292 case VAR_STRING:
22293 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022294 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022295 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022296 copy_tv(from, to);
22297 break;
22298 case VAR_LIST:
22299 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022300 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022301 if (from->vval.v_list == NULL)
22302 to->vval.v_list = NULL;
22303 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22304 {
22305 /* use the copy made earlier */
22306 to->vval.v_list = from->vval.v_list->lv_copylist;
22307 ++to->vval.v_list->lv_refcount;
22308 }
22309 else
22310 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22311 if (to->vval.v_list == NULL)
22312 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022313 break;
22314 case VAR_DICT:
22315 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022316 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022317 if (from->vval.v_dict == NULL)
22318 to->vval.v_dict = NULL;
22319 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22320 {
22321 /* use the copy made earlier */
22322 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22323 ++to->vval.v_dict->dv_refcount;
22324 }
22325 else
22326 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22327 if (to->vval.v_dict == NULL)
22328 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022329 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022330 case VAR_UNKNOWN:
22331 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022332 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022333 }
22334 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022335 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022336}
22337
22338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 * ":echo expr1 ..." print each argument separated with a space, add a
22340 * newline at the end.
22341 * ":echon expr1 ..." print each argument plain.
22342 */
22343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022344ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345{
22346 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022347 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022348 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 char_u *p;
22350 int needclr = TRUE;
22351 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353
22354 if (eap->skip)
22355 ++emsg_skip;
22356 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22357 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022358 /* If eval1() causes an error message the text from the command may
22359 * still need to be cleared. E.g., "echo 22,44". */
22360 need_clr_eos = needclr;
22361
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022363 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 {
22365 /*
22366 * Report the invalid expression unless the expression evaluation
22367 * has been cancelled due to an aborting error, an interrupt, or an
22368 * exception.
22369 */
22370 if (!aborting())
22371 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022372 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373 break;
22374 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022375 need_clr_eos = FALSE;
22376
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 if (!eap->skip)
22378 {
22379 if (atstart)
22380 {
22381 atstart = FALSE;
22382 /* Call msg_start() after eval1(), evaluating the expression
22383 * may cause a message to appear. */
22384 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022385 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022386 /* Mark the saved text as finishing the line, so that what
22387 * follows is displayed on a new line when scrolling back
22388 * at the more prompt. */
22389 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 }
22393 else if (eap->cmdidx == CMD_echo)
22394 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022395 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022396 if (p != NULL)
22397 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022399 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022401 if (*p != TAB && needclr)
22402 {
22403 /* remove any text still there from the command */
22404 msg_clr_eos();
22405 needclr = FALSE;
22406 }
22407 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 }
22409 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022410 {
22411#ifdef FEAT_MBYTE
22412 if (has_mbyte)
22413 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022414 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022415
22416 (void)msg_outtrans_len_attr(p, i, echo_attr);
22417 p += i - 1;
22418 }
22419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022421 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022424 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022426 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 arg = skipwhite(arg);
22428 }
22429 eap->nextcmd = check_nextcmd(arg);
22430
22431 if (eap->skip)
22432 --emsg_skip;
22433 else
22434 {
22435 /* remove text that may still be there from the command */
22436 if (needclr)
22437 msg_clr_eos();
22438 if (eap->cmdidx == CMD_echo)
22439 msg_end();
22440 }
22441}
22442
22443/*
22444 * ":echohl {name}".
22445 */
22446 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022447ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448{
22449 int id;
22450
22451 id = syn_name2id(eap->arg);
22452 if (id == 0)
22453 echo_attr = 0;
22454 else
22455 echo_attr = syn_id2attr(id);
22456}
22457
22458/*
22459 * ":execute expr1 ..." execute the result of an expression.
22460 * ":echomsg expr1 ..." Print a message
22461 * ":echoerr expr1 ..." Print an error
22462 * Each gets spaces around each argument and a newline at the end for
22463 * echo commands
22464 */
22465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022466ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467{
22468 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022469 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 int ret = OK;
22471 char_u *p;
22472 garray_T ga;
22473 int len;
22474 int save_did_emsg;
22475
22476 ga_init2(&ga, 1, 80);
22477
22478 if (eap->skip)
22479 ++emsg_skip;
22480 while (*arg != NUL && *arg != '|' && *arg != '\n')
22481 {
22482 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022483 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 {
22485 /*
22486 * Report the invalid expression unless the expression evaluation
22487 * has been cancelled due to an aborting error, an interrupt, or an
22488 * exception.
22489 */
22490 if (!aborting())
22491 EMSG2(_(e_invexpr2), p);
22492 ret = FAIL;
22493 break;
22494 }
22495
22496 if (!eap->skip)
22497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022498 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 len = (int)STRLEN(p);
22500 if (ga_grow(&ga, len + 2) == FAIL)
22501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022502 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 ret = FAIL;
22504 break;
22505 }
22506 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 ga.ga_len += len;
22510 }
22511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022512 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 arg = skipwhite(arg);
22514 }
22515
22516 if (ret != FAIL && ga.ga_data != NULL)
22517 {
22518 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022521 out_flush();
22522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 else if (eap->cmdidx == CMD_echoerr)
22524 {
22525 /* We don't want to abort following commands, restore did_emsg. */
22526 save_did_emsg = did_emsg;
22527 EMSG((char_u *)ga.ga_data);
22528 if (!force_abort)
22529 did_emsg = save_did_emsg;
22530 }
22531 else if (eap->cmdidx == CMD_execute)
22532 do_cmdline((char_u *)ga.ga_data,
22533 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22534 }
22535
22536 ga_clear(&ga);
22537
22538 if (eap->skip)
22539 --emsg_skip;
22540
22541 eap->nextcmd = check_nextcmd(arg);
22542}
22543
22544/*
22545 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22546 * "arg" points to the "&" or '+' when called, to "option" when returning.
22547 * Returns NULL when no option name found. Otherwise pointer to the char
22548 * after the option name.
22549 */
22550 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022551find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552{
22553 char_u *p = *arg;
22554
22555 ++p;
22556 if (*p == 'g' && p[1] == ':')
22557 {
22558 *opt_flags = OPT_GLOBAL;
22559 p += 2;
22560 }
22561 else if (*p == 'l' && p[1] == ':')
22562 {
22563 *opt_flags = OPT_LOCAL;
22564 p += 2;
22565 }
22566 else
22567 *opt_flags = 0;
22568
22569 if (!ASCII_ISALPHA(*p))
22570 return NULL;
22571 *arg = p;
22572
22573 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22574 p += 4; /* termcap option */
22575 else
22576 while (ASCII_ISALPHA(*p))
22577 ++p;
22578 return p;
22579}
22580
22581/*
22582 * ":function"
22583 */
22584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022585ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586{
22587 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022588 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 int j;
22590 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022592 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 char_u *name = NULL;
22594 char_u *p;
22595 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022596 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597 garray_T newargs;
22598 garray_T newlines;
22599 int varargs = FALSE;
22600 int mustend = FALSE;
22601 int flags = 0;
22602 ufunc_T *fp;
22603 int indent;
22604 int nesting;
22605 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 dictitem_T *v;
22607 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022608 static int func_nr = 0; /* number for nameless function */
22609 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022610 hashtab_T *ht;
22611 int todo;
22612 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022613 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614
22615 /*
22616 * ":function" without argument: list functions.
22617 */
22618 if (ends_excmd(*eap->arg))
22619 {
22620 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022621 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022622 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022623 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022624 {
22625 if (!HASHITEM_EMPTY(hi))
22626 {
22627 --todo;
22628 fp = HI2UF(hi);
22629 if (!isdigit(*fp->uf_name))
22630 list_func_head(fp, FALSE);
22631 }
22632 }
22633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 eap->nextcmd = check_nextcmd(eap->arg);
22635 return;
22636 }
22637
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022638 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022639 * ":function /pat": list functions matching pattern.
22640 */
22641 if (*eap->arg == '/')
22642 {
22643 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22644 if (!eap->skip)
22645 {
22646 regmatch_T regmatch;
22647
22648 c = *p;
22649 *p = NUL;
22650 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22651 *p = c;
22652 if (regmatch.regprog != NULL)
22653 {
22654 regmatch.rm_ic = p_ic;
22655
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022656 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022657 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22658 {
22659 if (!HASHITEM_EMPTY(hi))
22660 {
22661 --todo;
22662 fp = HI2UF(hi);
22663 if (!isdigit(*fp->uf_name)
22664 && vim_regexec(&regmatch, fp->uf_name, 0))
22665 list_func_head(fp, FALSE);
22666 }
22667 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022668 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022669 }
22670 }
22671 if (*p == '/')
22672 ++p;
22673 eap->nextcmd = check_nextcmd(p);
22674 return;
22675 }
22676
22677 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022678 * Get the function name. There are these situations:
22679 * func normal function name
22680 * "name" == func, "fudi.fd_dict" == NULL
22681 * dict.func new dictionary entry
22682 * "name" == NULL, "fudi.fd_dict" set,
22683 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22684 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022685 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022686 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22687 * dict.func existing dict entry that's not a Funcref
22688 * "name" == NULL, "fudi.fd_dict" set,
22689 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022690 * s:func script-local function name
22691 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022694 name = trans_function_name(&p, eap->skip, 0, &fudi);
22695 paren = (vim_strchr(p, '(') != NULL);
22696 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 {
22698 /*
22699 * Return on an invalid expression in braces, unless the expression
22700 * evaluation has been cancelled due to an aborting error, an
22701 * interrupt, or an exception.
22702 */
22703 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022704 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022705 if (!eap->skip && fudi.fd_newkey != NULL)
22706 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022707 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 else
22711 eap->skip = TRUE;
22712 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022713
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 /* An error in a function call during evaluation of an expression in magic
22715 * braces should not cause the function not to be defined. */
22716 saved_did_emsg = did_emsg;
22717 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718
22719 /*
22720 * ":function func" with only function name: list function.
22721 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022722 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 {
22724 if (!ends_excmd(*skipwhite(p)))
22725 {
22726 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022727 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 }
22729 eap->nextcmd = check_nextcmd(p);
22730 if (eap->nextcmd != NULL)
22731 *p = NUL;
22732 if (!eap->skip && !got_int)
22733 {
22734 fp = find_func(name);
22735 if (fp != NULL)
22736 {
22737 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022738 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022740 if (FUNCLINE(fp, j) == NULL)
22741 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 msg_putchar('\n');
22743 msg_outnum((long)(j + 1));
22744 if (j < 9)
22745 msg_putchar(' ');
22746 if (j < 99)
22747 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022748 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 out_flush(); /* show a line at a time */
22750 ui_breakcheck();
22751 }
22752 if (!got_int)
22753 {
22754 msg_putchar('\n');
22755 msg_puts((char_u *)" endfunction");
22756 }
22757 }
22758 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022759 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022761 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 }
22763
22764 /*
22765 * ":function name(arg1, arg2)" Define function.
22766 */
22767 p = skipwhite(p);
22768 if (*p != '(')
22769 {
22770 if (!eap->skip)
22771 {
22772 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022773 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 }
22775 /* attempt to continue by skipping some text */
22776 if (vim_strchr(p, '(') != NULL)
22777 p = vim_strchr(p, '(');
22778 }
22779 p = skipwhite(p + 1);
22780
22781 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22782 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22783
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022784 if (!eap->skip)
22785 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022786 /* Check the name of the function. Unless it's a dictionary function
22787 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022788 if (name != NULL)
22789 arg = name;
22790 else
22791 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022792 if (arg != NULL && (fudi.fd_di == NULL
22793 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022794 {
22795 if (*arg == K_SPECIAL)
22796 j = 3;
22797 else
22798 j = 0;
22799 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22800 : eval_isnamec(arg[j])))
22801 ++j;
22802 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022803 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022804 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022805 /* Disallow using the g: dict. */
22806 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22807 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022808 }
22809
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 /*
22811 * Isolate the arguments: "arg1, arg2, ...)"
22812 */
22813 while (*p != ')')
22814 {
22815 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22816 {
22817 varargs = TRUE;
22818 p += 3;
22819 mustend = TRUE;
22820 }
22821 else
22822 {
22823 arg = p;
22824 while (ASCII_ISALNUM(*p) || *p == '_')
22825 ++p;
22826 if (arg == p || isdigit(*arg)
22827 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22828 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22829 {
22830 if (!eap->skip)
22831 EMSG2(_("E125: Illegal argument: %s"), arg);
22832 break;
22833 }
22834 if (ga_grow(&newargs, 1) == FAIL)
22835 goto erret;
22836 c = *p;
22837 *p = NUL;
22838 arg = vim_strsave(arg);
22839 if (arg == NULL)
22840 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022841
22842 /* Check for duplicate argument name. */
22843 for (i = 0; i < newargs.ga_len; ++i)
22844 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22845 {
22846 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022847 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022848 goto erret;
22849 }
22850
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22852 *p = c;
22853 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 if (*p == ',')
22855 ++p;
22856 else
22857 mustend = TRUE;
22858 }
22859 p = skipwhite(p);
22860 if (mustend && *p != ')')
22861 {
22862 if (!eap->skip)
22863 EMSG2(_(e_invarg2), eap->arg);
22864 break;
22865 }
22866 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022867 if (*p != ')')
22868 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 ++p; /* skip the ')' */
22870
Bram Moolenaare9a41262005-01-15 22:18:47 +000022871 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 for (;;)
22873 {
22874 p = skipwhite(p);
22875 if (STRNCMP(p, "range", 5) == 0)
22876 {
22877 flags |= FC_RANGE;
22878 p += 5;
22879 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022880 else if (STRNCMP(p, "dict", 4) == 0)
22881 {
22882 flags |= FC_DICT;
22883 p += 4;
22884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 else if (STRNCMP(p, "abort", 5) == 0)
22886 {
22887 flags |= FC_ABORT;
22888 p += 5;
22889 }
22890 else
22891 break;
22892 }
22893
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022894 /* When there is a line break use what follows for the function body.
22895 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22896 if (*p == '\n')
22897 line_arg = p + 1;
22898 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 EMSG(_(e_trailing));
22900
22901 /*
22902 * Read the body of the function, until ":endfunction" is found.
22903 */
22904 if (KeyTyped)
22905 {
22906 /* Check if the function already exists, don't let the user type the
22907 * whole function before telling him it doesn't work! For a script we
22908 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022909 if (!eap->skip && !eap->forceit)
22910 {
22911 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22912 EMSG(_(e_funcdict));
22913 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022914 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022917 if (!eap->skip && did_emsg)
22918 goto erret;
22919
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 msg_putchar('\n'); /* don't overwrite the function name */
22921 cmdline_row = msg_row;
22922 }
22923
22924 indent = 2;
22925 nesting = 0;
22926 for (;;)
22927 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022928 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022929 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022930 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022931 saved_wait_return = FALSE;
22932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022934 sourcing_lnum_off = sourcing_lnum;
22935
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022936 if (line_arg != NULL)
22937 {
22938 /* Use eap->arg, split up in parts by line breaks. */
22939 theline = line_arg;
22940 p = vim_strchr(theline, '\n');
22941 if (p == NULL)
22942 line_arg += STRLEN(line_arg);
22943 else
22944 {
22945 *p = NUL;
22946 line_arg = p + 1;
22947 }
22948 }
22949 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 theline = getcmdline(':', 0L, indent);
22951 else
22952 theline = eap->getline(':', eap->cookie, indent);
22953 if (KeyTyped)
22954 lines_left = Rows - 1;
22955 if (theline == NULL)
22956 {
22957 EMSG(_("E126: Missing :endfunction"));
22958 goto erret;
22959 }
22960
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022961 /* Detect line continuation: sourcing_lnum increased more than one. */
22962 if (sourcing_lnum > sourcing_lnum_off + 1)
22963 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22964 else
22965 sourcing_lnum_off = 0;
22966
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 if (skip_until != NULL)
22968 {
22969 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22970 * don't check for ":endfunc". */
22971 if (STRCMP(theline, skip_until) == 0)
22972 {
22973 vim_free(skip_until);
22974 skip_until = NULL;
22975 }
22976 }
22977 else
22978 {
22979 /* skip ':' and blanks*/
22980 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22981 ;
22982
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022983 /* Check for "endfunction". */
22984 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022986 if (line_arg == NULL)
22987 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 break;
22989 }
22990
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022991 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 * at "end". */
22993 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22994 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022995 else if (STRNCMP(p, "if", 2) == 0
22996 || STRNCMP(p, "wh", 2) == 0
22997 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 || STRNCMP(p, "try", 3) == 0)
22999 indent += 2;
23000
23001 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023002 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023004 if (*p == '!')
23005 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023007 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23008 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023010 ++nesting;
23011 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 }
23013 }
23014
23015 /* Check for ":append" or ":insert". */
23016 p = skip_range(p, NULL);
23017 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23018 || (p[0] == 'i'
23019 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23020 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23021 skip_until = vim_strsave((char_u *)".");
23022
23023 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23024 arg = skipwhite(skiptowhite(p));
23025 if (arg[0] == '<' && arg[1] =='<'
23026 && ((p[0] == 'p' && p[1] == 'y'
23027 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23028 || (p[0] == 'p' && p[1] == 'e'
23029 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23030 || (p[0] == 't' && p[1] == 'c'
23031 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023032 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23033 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23035 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023036 || (p[0] == 'm' && p[1] == 'z'
23037 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 ))
23039 {
23040 /* ":python <<" continues until a dot, like ":append" */
23041 p = skipwhite(arg + 2);
23042 if (*p == NUL)
23043 skip_until = vim_strsave((char_u *)".");
23044 else
23045 skip_until = vim_strsave(p);
23046 }
23047 }
23048
23049 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023050 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023051 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023052 if (line_arg == NULL)
23053 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023055 }
23056
23057 /* Copy the line to newly allocated memory. get_one_sourceline()
23058 * allocates 250 bytes per line, this saves 80% on average. The cost
23059 * is an extra alloc/free. */
23060 p = vim_strsave(theline);
23061 if (p != NULL)
23062 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023063 if (line_arg == NULL)
23064 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023065 theline = p;
23066 }
23067
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023068 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23069
23070 /* Add NULL lines for continuation lines, so that the line count is
23071 * equal to the index in the growarray. */
23072 while (sourcing_lnum_off-- > 0)
23073 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023074
23075 /* Check for end of eap->arg. */
23076 if (line_arg != NULL && *line_arg == NUL)
23077 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 }
23079
23080 /* Don't define the function when skipping commands or when an error was
23081 * detected. */
23082 if (eap->skip || did_emsg)
23083 goto erret;
23084
23085 /*
23086 * If there are no errors, add the function
23087 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023088 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023089 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023090 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023091 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023092 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023093 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023094 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023095 goto erret;
23096 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023097
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023098 fp = find_func(name);
23099 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023101 if (!eap->forceit)
23102 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023103 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023104 goto erret;
23105 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023106 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023107 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023108 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023109 name);
23110 goto erret;
23111 }
23112 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023113 ga_clear_strings(&(fp->uf_args));
23114 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023115 vim_free(name);
23116 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 }
23119 else
23120 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023121 char numbuf[20];
23122
23123 fp = NULL;
23124 if (fudi.fd_newkey == NULL && !eap->forceit)
23125 {
23126 EMSG(_(e_funcdict));
23127 goto erret;
23128 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023129 if (fudi.fd_di == NULL)
23130 {
23131 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023132 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023133 goto erret;
23134 }
23135 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023136 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023137 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023138
23139 /* Give the function a sequential number. Can only be used with a
23140 * Funcref! */
23141 vim_free(name);
23142 sprintf(numbuf, "%d", ++func_nr);
23143 name = vim_strsave((char_u *)numbuf);
23144 if (name == NULL)
23145 goto erret;
23146 }
23147
23148 if (fp == NULL)
23149 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023150 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023151 {
23152 int slen, plen;
23153 char_u *scriptname;
23154
23155 /* Check that the autoload name matches the script name. */
23156 j = FAIL;
23157 if (sourcing_name != NULL)
23158 {
23159 scriptname = autoload_name(name);
23160 if (scriptname != NULL)
23161 {
23162 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023163 plen = (int)STRLEN(p);
23164 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023165 if (slen > plen && fnamecmp(p,
23166 sourcing_name + slen - plen) == 0)
23167 j = OK;
23168 vim_free(scriptname);
23169 }
23170 }
23171 if (j == FAIL)
23172 {
23173 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23174 goto erret;
23175 }
23176 }
23177
23178 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 if (fp == NULL)
23180 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023181
23182 if (fudi.fd_dict != NULL)
23183 {
23184 if (fudi.fd_di == NULL)
23185 {
23186 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023187 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023188 if (fudi.fd_di == NULL)
23189 {
23190 vim_free(fp);
23191 goto erret;
23192 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023193 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23194 {
23195 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023196 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023197 goto erret;
23198 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023199 }
23200 else
23201 /* overwrite existing dict entry */
23202 clear_tv(&fudi.fd_di->di_tv);
23203 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023204 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023205 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023206 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023207
23208 /* behave like "dict" was used */
23209 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023210 }
23211
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023213 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023214 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23215 {
23216 vim_free(fp);
23217 goto erret;
23218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023220 fp->uf_args = newargs;
23221 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023222#ifdef FEAT_PROFILE
23223 fp->uf_tml_count = NULL;
23224 fp->uf_tml_total = NULL;
23225 fp->uf_tml_self = NULL;
23226 fp->uf_profiling = FALSE;
23227 if (prof_def_func())
23228 func_do_profile(fp);
23229#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023230 fp->uf_varargs = varargs;
23231 fp->uf_flags = flags;
23232 fp->uf_calls = 0;
23233 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023234 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235
23236erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 ga_clear_strings(&newargs);
23238 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023239ret_free:
23240 vim_free(skip_until);
23241 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023244 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245}
23246
23247/*
23248 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023249 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023251 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023252 * TFN_INT: internal function name OK
23253 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023254 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 * Advances "pp" to just after the function name (if no error).
23256 */
23257 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023258trans_function_name(
23259 char_u **pp,
23260 int skip, /* only find the end, don't evaluate */
23261 int flags,
23262 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023264 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265 char_u *start;
23266 char_u *end;
23267 int lead;
23268 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023270 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023271
23272 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023273 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023275
23276 /* Check for hard coded <SNR>: already translated function ID (from a user
23277 * command). */
23278 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23279 && (*pp)[2] == (int)KE_SNR)
23280 {
23281 *pp += 3;
23282 len = get_id_len(pp) + 3;
23283 return vim_strnsave(start, len);
23284 }
23285
23286 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23287 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023289 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023291
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023292 /* Note that TFN_ flags use the same values as GLV_ flags. */
23293 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023294 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023295 if (end == start)
23296 {
23297 if (!skip)
23298 EMSG(_("E129: Function name required"));
23299 goto theend;
23300 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023301 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023302 {
23303 /*
23304 * Report an invalid expression in braces, unless the expression
23305 * evaluation has been cancelled due to an aborting error, an
23306 * interrupt, or an exception.
23307 */
23308 if (!aborting())
23309 {
23310 if (end != NULL)
23311 EMSG2(_(e_invarg2), start);
23312 }
23313 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023314 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023315 goto theend;
23316 }
23317
23318 if (lv.ll_tv != NULL)
23319 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023320 if (fdp != NULL)
23321 {
23322 fdp->fd_dict = lv.ll_dict;
23323 fdp->fd_newkey = lv.ll_newkey;
23324 lv.ll_newkey = NULL;
23325 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023326 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023327 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23328 {
23329 name = vim_strsave(lv.ll_tv->vval.v_string);
23330 *pp = end;
23331 }
23332 else
23333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023334 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23335 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023336 EMSG(_(e_funcref));
23337 else
23338 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023339 name = NULL;
23340 }
23341 goto theend;
23342 }
23343
23344 if (lv.ll_name == NULL)
23345 {
23346 /* Error found, but continue after the function name. */
23347 *pp = end;
23348 goto theend;
23349 }
23350
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023351 /* Check if the name is a Funcref. If so, use the value. */
23352 if (lv.ll_exp_name != NULL)
23353 {
23354 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023355 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023356 if (name == lv.ll_exp_name)
23357 name = NULL;
23358 }
23359 else
23360 {
23361 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023362 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023363 if (name == *pp)
23364 name = NULL;
23365 }
23366 if (name != NULL)
23367 {
23368 name = vim_strsave(name);
23369 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023370 if (STRNCMP(name, "<SNR>", 5) == 0)
23371 {
23372 /* Change "<SNR>" to the byte sequence. */
23373 name[0] = K_SPECIAL;
23374 name[1] = KS_EXTRA;
23375 name[2] = (int)KE_SNR;
23376 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23377 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023378 goto theend;
23379 }
23380
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023381 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023382 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023383 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023384 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23385 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23386 {
23387 /* When there was "s:" already or the name expanded to get a
23388 * leading "s:" then remove it. */
23389 lv.ll_name += 2;
23390 len -= 2;
23391 lead = 2;
23392 }
23393 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023394 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023395 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023396 /* skip over "s:" and "g:" */
23397 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023398 lv.ll_name += 2;
23399 len = (int)(end - lv.ll_name);
23400 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023401
23402 /*
23403 * Copy the function name to allocated memory.
23404 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23405 * Accept <SNR>123_name() outside a script.
23406 */
23407 if (skip)
23408 lead = 0; /* do nothing */
23409 else if (lead > 0)
23410 {
23411 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023412 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23413 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023414 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023415 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023416 if (current_SID <= 0)
23417 {
23418 EMSG(_(e_usingsid));
23419 goto theend;
23420 }
23421 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23422 lead += (int)STRLEN(sid_buf);
23423 }
23424 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023425 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023426 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023427 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023428 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023429 goto theend;
23430 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023431 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023432 {
23433 char_u *cp = vim_strchr(lv.ll_name, ':');
23434
23435 if (cp != NULL && cp < end)
23436 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023437 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023438 goto theend;
23439 }
23440 }
23441
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023442 name = alloc((unsigned)(len + lead + 1));
23443 if (name != NULL)
23444 {
23445 if (lead > 0)
23446 {
23447 name[0] = K_SPECIAL;
23448 name[1] = KS_EXTRA;
23449 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023450 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023451 STRCPY(name + 3, sid_buf);
23452 }
23453 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023454 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023455 }
23456 *pp = end;
23457
23458theend:
23459 clear_lval(&lv);
23460 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461}
23462
23463/*
23464 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23465 * Return 2 if "p" starts with "s:".
23466 * Return 0 otherwise.
23467 */
23468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023469eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023471 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23472 * the standard library function. */
23473 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23474 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 return 5;
23476 if (p[0] == 's' && p[1] == ':')
23477 return 2;
23478 return 0;
23479}
23480
23481/*
23482 * Return TRUE if "p" starts with "<SID>" or "s:".
23483 * Only works if eval_fname_script() returned non-zero for "p"!
23484 */
23485 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023486eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487{
23488 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23489}
23490
23491/*
23492 * List the head of the function: "name(arg1, arg2)".
23493 */
23494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023495list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496{
23497 int j;
23498
23499 msg_start();
23500 if (indent)
23501 MSG_PUTS(" ");
23502 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023503 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023504 {
23505 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023506 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 }
23508 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023509 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023511 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 {
23513 if (j)
23514 MSG_PUTS(", ");
23515 msg_puts(FUNCARG(fp, j));
23516 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023517 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518 {
23519 if (j)
23520 MSG_PUTS(", ");
23521 MSG_PUTS("...");
23522 }
23523 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023524 if (fp->uf_flags & FC_ABORT)
23525 MSG_PUTS(" abort");
23526 if (fp->uf_flags & FC_RANGE)
23527 MSG_PUTS(" range");
23528 if (fp->uf_flags & FC_DICT)
23529 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023530 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023531 if (p_verbose > 0)
23532 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533}
23534
23535/*
23536 * Find a function by name, return pointer to it in ufuncs.
23537 * Return NULL for unknown function.
23538 */
23539 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023540find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023542 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023544 hi = hash_find(&func_hashtab, name);
23545 if (!HASHITEM_EMPTY(hi))
23546 return HI2UF(hi);
23547 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548}
23549
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023550#if defined(EXITFREE) || defined(PROTO)
23551 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023552free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023553{
23554 hashitem_T *hi;
23555
23556 /* Need to start all over every time, because func_free() may change the
23557 * hash table. */
23558 while (func_hashtab.ht_used > 0)
23559 for (hi = func_hashtab.ht_array; ; ++hi)
23560 if (!HASHITEM_EMPTY(hi))
23561 {
23562 func_free(HI2UF(hi));
23563 break;
23564 }
23565}
23566#endif
23567
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023568 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023569translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023570{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023571 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023572 return find_internal_func(name) >= 0;
23573 return find_func(name) != NULL;
23574}
23575
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023576/*
23577 * Return TRUE if a function "name" exists.
23578 */
23579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023580function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023581{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023582 char_u *nm = name;
23583 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023584 int n = FALSE;
23585
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023586 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23587 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023588 nm = skipwhite(nm);
23589
23590 /* Only accept "funcname", "funcname ", "funcname (..." and
23591 * "funcname(...", not "funcname!...". */
23592 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023593 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023594 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023595 return n;
23596}
23597
Bram Moolenaara1544c02013-05-30 12:35:52 +020023598 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023599get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023600{
23601 char_u *nm = name;
23602 char_u *p;
23603
23604 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23605
23606 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023607 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023608 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023609
Bram Moolenaara1544c02013-05-30 12:35:52 +020023610 vim_free(p);
23611 return NULL;
23612}
23613
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023614/*
23615 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023616 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23617 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023618 */
23619 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023620builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023621{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023622 char_u *p;
23623
23624 if (!ASCII_ISLOWER(name[0]))
23625 return FALSE;
23626 p = vim_strchr(name, AUTOLOAD_CHAR);
23627 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023628}
23629
Bram Moolenaar05159a02005-02-26 23:04:13 +000023630#if defined(FEAT_PROFILE) || defined(PROTO)
23631/*
23632 * Start profiling function "fp".
23633 */
23634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023635func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023636{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023637 int len = fp->uf_lines.ga_len;
23638
23639 if (len == 0)
23640 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023641 fp->uf_tm_count = 0;
23642 profile_zero(&fp->uf_tm_self);
23643 profile_zero(&fp->uf_tm_total);
23644 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023645 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023646 if (fp->uf_tml_total == NULL)
23647 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023648 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023649 if (fp->uf_tml_self == NULL)
23650 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023651 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023652 fp->uf_tml_idx = -1;
23653 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23654 || fp->uf_tml_self == NULL)
23655 return; /* out of memory */
23656
23657 fp->uf_profiling = TRUE;
23658}
23659
23660/*
23661 * Dump the profiling results for all functions in file "fd".
23662 */
23663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023664func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023665{
23666 hashitem_T *hi;
23667 int todo;
23668 ufunc_T *fp;
23669 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023670 ufunc_T **sorttab;
23671 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023672
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023673 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023674 if (todo == 0)
23675 return; /* nothing to dump */
23676
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023677 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023678
Bram Moolenaar05159a02005-02-26 23:04:13 +000023679 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23680 {
23681 if (!HASHITEM_EMPTY(hi))
23682 {
23683 --todo;
23684 fp = HI2UF(hi);
23685 if (fp->uf_profiling)
23686 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023687 if (sorttab != NULL)
23688 sorttab[st_len++] = fp;
23689
Bram Moolenaar05159a02005-02-26 23:04:13 +000023690 if (fp->uf_name[0] == K_SPECIAL)
23691 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23692 else
23693 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23694 if (fp->uf_tm_count == 1)
23695 fprintf(fd, "Called 1 time\n");
23696 else
23697 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23698 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23699 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23700 fprintf(fd, "\n");
23701 fprintf(fd, "count total (s) self (s)\n");
23702
23703 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23704 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023705 if (FUNCLINE(fp, i) == NULL)
23706 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023707 prof_func_line(fd, fp->uf_tml_count[i],
23708 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023709 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23710 }
23711 fprintf(fd, "\n");
23712 }
23713 }
23714 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023715
23716 if (sorttab != NULL && st_len > 0)
23717 {
23718 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23719 prof_total_cmp);
23720 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23721 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23722 prof_self_cmp);
23723 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23724 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023725
23726 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023727}
Bram Moolenaar73830342005-02-28 22:48:19 +000023728
23729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023730prof_sort_list(
23731 FILE *fd,
23732 ufunc_T **sorttab,
23733 int st_len,
23734 char *title,
23735 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023736{
23737 int i;
23738 ufunc_T *fp;
23739
23740 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23741 fprintf(fd, "count total (s) self (s) function\n");
23742 for (i = 0; i < 20 && i < st_len; ++i)
23743 {
23744 fp = sorttab[i];
23745 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23746 prefer_self);
23747 if (fp->uf_name[0] == K_SPECIAL)
23748 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23749 else
23750 fprintf(fd, " %s()\n", fp->uf_name);
23751 }
23752 fprintf(fd, "\n");
23753}
23754
23755/*
23756 * Print the count and times for one function or function line.
23757 */
23758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023759prof_func_line(
23760 FILE *fd,
23761 int count,
23762 proftime_T *total,
23763 proftime_T *self,
23764 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023765{
23766 if (count > 0)
23767 {
23768 fprintf(fd, "%5d ", count);
23769 if (prefer_self && profile_equal(total, self))
23770 fprintf(fd, " ");
23771 else
23772 fprintf(fd, "%s ", profile_msg(total));
23773 if (!prefer_self && profile_equal(total, self))
23774 fprintf(fd, " ");
23775 else
23776 fprintf(fd, "%s ", profile_msg(self));
23777 }
23778 else
23779 fprintf(fd, " ");
23780}
23781
23782/*
23783 * Compare function for total time sorting.
23784 */
23785 static int
23786#ifdef __BORLANDC__
23787_RTLENTRYF
23788#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023789prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023790{
23791 ufunc_T *p1, *p2;
23792
23793 p1 = *(ufunc_T **)s1;
23794 p2 = *(ufunc_T **)s2;
23795 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23796}
23797
23798/*
23799 * Compare function for self time sorting.
23800 */
23801 static int
23802#ifdef __BORLANDC__
23803_RTLENTRYF
23804#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023805prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023806{
23807 ufunc_T *p1, *p2;
23808
23809 p1 = *(ufunc_T **)s1;
23810 p2 = *(ufunc_T **)s2;
23811 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23812}
23813
Bram Moolenaar05159a02005-02-26 23:04:13 +000023814#endif
23815
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023816/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023817 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023818 * Return TRUE if a package was loaded.
23819 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023820 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023821script_autoload(
23822 char_u *name,
23823 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023824{
23825 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023826 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023827 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023828 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023829
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023830 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023831 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023832 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023833 return FALSE;
23834
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023835 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023836
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023837 /* Find the name in the list of previously loaded package names. Skip
23838 * "autoload/", it's always the same. */
23839 for (i = 0; i < ga_loaded.ga_len; ++i)
23840 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23841 break;
23842 if (!reload && i < ga_loaded.ga_len)
23843 ret = FALSE; /* was loaded already */
23844 else
23845 {
23846 /* Remember the name if it wasn't loaded already. */
23847 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23848 {
23849 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23850 tofree = NULL;
23851 }
23852
23853 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023854 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023855 ret = TRUE;
23856 }
23857
23858 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023859 return ret;
23860}
23861
23862/*
23863 * Return the autoload script name for a function or variable name.
23864 * Returns NULL when out of memory.
23865 */
23866 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023867autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023868{
23869 char_u *p;
23870 char_u *scriptname;
23871
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023872 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023873 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23874 if (scriptname == NULL)
23875 return FALSE;
23876 STRCPY(scriptname, "autoload/");
23877 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023878 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023879 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023880 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023881 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023882 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023883}
23884
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23886
23887/*
23888 * Function given to ExpandGeneric() to obtain the list of user defined
23889 * function names.
23890 */
23891 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023892get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023894 static long_u done;
23895 static hashitem_T *hi;
23896 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897
23898 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023900 done = 0;
23901 hi = func_hashtab.ht_array;
23902 }
23903 if (done < func_hashtab.ht_used)
23904 {
23905 if (done++ > 0)
23906 ++hi;
23907 while (HASHITEM_EMPTY(hi))
23908 ++hi;
23909 fp = HI2UF(hi);
23910
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023911 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023912 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023913
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023914 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23915 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916
23917 cat_func_name(IObuff, fp);
23918 if (xp->xp_context != EXPAND_USER_FUNC)
23919 {
23920 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023921 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 STRCAT(IObuff, ")");
23923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023924 return IObuff;
23925 }
23926 return NULL;
23927}
23928
23929#endif /* FEAT_CMDL_COMPL */
23930
23931/*
23932 * Copy the function name of "fp" to buffer "buf".
23933 * "buf" must be able to hold the function name plus three bytes.
23934 * Takes care of script-local function names.
23935 */
23936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023937cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023939 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 {
23941 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023942 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 }
23944 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023945 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946}
23947
23948/*
23949 * ":delfunction {name}"
23950 */
23951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023952ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023954 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 char_u *p;
23956 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023957 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958
23959 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023960 name = trans_function_name(&p, eap->skip, 0, &fudi);
23961 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023963 {
23964 if (fudi.fd_dict != NULL && !eap->skip)
23965 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 if (!ends_excmd(*skipwhite(p)))
23969 {
23970 vim_free(name);
23971 EMSG(_(e_trailing));
23972 return;
23973 }
23974 eap->nextcmd = check_nextcmd(p);
23975 if (eap->nextcmd != NULL)
23976 *p = NUL;
23977
23978 if (!eap->skip)
23979 fp = find_func(name);
23980 vim_free(name);
23981
23982 if (!eap->skip)
23983 {
23984 if (fp == NULL)
23985 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023986 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 return;
23988 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023989 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 {
23991 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23992 return;
23993 }
23994
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023995 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023997 /* Delete the dict item that refers to the function, it will
23998 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023999 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024001 else
24002 func_free(fp);
24003 }
24004}
24005
24006/*
24007 * Free a function and remove it from the list of functions.
24008 */
24009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024010func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024011{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024012 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024013
24014 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024015 ga_clear_strings(&(fp->uf_args));
24016 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024017#ifdef FEAT_PROFILE
24018 vim_free(fp->uf_tml_count);
24019 vim_free(fp->uf_tml_total);
24020 vim_free(fp->uf_tml_self);
24021#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024023 /* remove the function from the function hashtable */
24024 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24025 if (HASHITEM_EMPTY(hi))
24026 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024027 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024028 hash_remove(&func_hashtab, hi);
24029
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024030 vim_free(fp);
24031}
24032
24033/*
24034 * Unreference a Function: decrement the reference count and free it when it
24035 * becomes zero. Only for numbered functions.
24036 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024037 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024038func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024039{
24040 ufunc_T *fp;
24041
24042 if (name != NULL && isdigit(*name))
24043 {
24044 fp = find_func(name);
24045 if (fp == NULL)
24046 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024047 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024048 {
24049 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024050 * when "uf_calls" becomes zero. */
24051 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024052 func_free(fp);
24053 }
24054 }
24055}
24056
24057/*
24058 * Count a reference to a Function.
24059 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024061func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024062{
24063 ufunc_T *fp;
24064
24065 if (name != NULL && isdigit(*name))
24066 {
24067 fp = find_func(name);
24068 if (fp == NULL)
24069 EMSG2(_(e_intern2), "func_ref()");
24070 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024071 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 }
24073}
24074
24075/*
24076 * Call a user function.
24077 */
24078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024079call_user_func(
24080 ufunc_T *fp, /* pointer to function */
24081 int argcount, /* nr of args */
24082 typval_T *argvars, /* arguments */
24083 typval_T *rettv, /* return value */
24084 linenr_T firstline, /* first line of range */
24085 linenr_T lastline, /* last line of range */
24086 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087{
Bram Moolenaar33570922005-01-25 22:26:29 +000024088 char_u *save_sourcing_name;
24089 linenr_T save_sourcing_lnum;
24090 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024091 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024092 int save_did_emsg;
24093 static int depth = 0;
24094 dictitem_T *v;
24095 int fixvar_idx = 0; /* index in fixvar[] */
24096 int i;
24097 int ai;
24098 char_u numbuf[NUMBUFLEN];
24099 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024100 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024101#ifdef FEAT_PROFILE
24102 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024103 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105
24106 /* If depth of calling is getting too high, don't execute the function */
24107 if (depth >= p_mfd)
24108 {
24109 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024110 rettv->v_type = VAR_NUMBER;
24111 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 return;
24113 }
24114 ++depth;
24115
24116 line_breakcheck(); /* check for CTRL-C hit */
24117
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024118 fc = (funccall_T *)alloc(sizeof(funccall_T));
24119 fc->caller = current_funccal;
24120 current_funccal = fc;
24121 fc->func = fp;
24122 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024123 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024124 fc->linenr = 0;
24125 fc->returned = FALSE;
24126 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024127 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024128 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24129 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130
Bram Moolenaar33570922005-01-25 22:26:29 +000024131 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024132 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024133 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24134 * each argument variable and saves a lot of time.
24135 */
24136 /*
24137 * Init l: variables.
24138 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024139 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024140 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024141 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024142 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24143 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024144 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024145 name = v->di_key;
24146 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024147 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024148 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024149 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024150 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024151 v->di_tv.vval.v_dict = selfdict;
24152 ++selfdict->dv_refcount;
24153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024154
Bram Moolenaar33570922005-01-25 22:26:29 +000024155 /*
24156 * Init a: variables.
24157 * Set a:0 to "argcount".
24158 * Set a:000 to a list with room for the "..." arguments.
24159 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024160 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024161 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024162 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024163 /* Use "name" to avoid a warning from some compiler that checks the
24164 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024165 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024166 name = v->di_key;
24167 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024168 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024169 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024170 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024171 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024172 v->di_tv.vval.v_list = &fc->l_varlist;
24173 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24174 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24175 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024176
24177 /*
24178 * Set a:firstline to "firstline" and a:lastline to "lastline".
24179 * Set a:name to named arguments.
24180 * Set a:N to the "..." arguments.
24181 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024182 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024183 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024184 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024185 (varnumber_T)lastline);
24186 for (i = 0; i < argcount; ++i)
24187 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024188 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024189 if (ai < 0)
24190 /* named argument a:name */
24191 name = FUNCARG(fp, i);
24192 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024193 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024194 /* "..." argument a:1, a:2, etc. */
24195 sprintf((char *)numbuf, "%d", ai + 1);
24196 name = numbuf;
24197 }
24198 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24199 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024200 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024201 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24202 }
24203 else
24204 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024205 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24206 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024207 if (v == NULL)
24208 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024209 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024210 }
24211 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024212 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024213
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024214 /* Note: the values are copied directly to avoid alloc/free.
24215 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024216 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024217 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024218
24219 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24220 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024221 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24222 fc->l_listitems[ai].li_tv = argvars[i];
24223 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024224 }
24225 }
24226
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 /* Don't redraw while executing the function. */
24228 ++RedrawingDisabled;
24229 save_sourcing_name = sourcing_name;
24230 save_sourcing_lnum = sourcing_lnum;
24231 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024232 /* need space for function name + ("function " + 3) or "[number]" */
24233 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24234 + STRLEN(fp->uf_name) + 20;
24235 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 if (sourcing_name != NULL)
24237 {
24238 if (save_sourcing_name != NULL
24239 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024240 sprintf((char *)sourcing_name, "%s[%d]..",
24241 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 else
24243 STRCPY(sourcing_name, "function ");
24244 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24245
24246 if (p_verbose >= 12)
24247 {
24248 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024249 verbose_enter_scroll();
24250
Bram Moolenaar555b2802005-05-19 21:08:39 +000024251 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 if (p_verbose >= 14)
24253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024255 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024256 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024257 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258
24259 msg_puts((char_u *)"(");
24260 for (i = 0; i < argcount; ++i)
24261 {
24262 if (i > 0)
24263 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024264 if (argvars[i].v_type == VAR_NUMBER)
24265 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 else
24267 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024268 /* Do not want errors such as E724 here. */
24269 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024270 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024271 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024272 if (s != NULL)
24273 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024274 if (vim_strsize(s) > MSG_BUF_CLEN)
24275 {
24276 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24277 s = buf;
24278 }
24279 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024280 vim_free(tofree);
24281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 }
24283 }
24284 msg_puts((char_u *)")");
24285 }
24286 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024287
24288 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 --no_wait_return;
24290 }
24291 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024292#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024293 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024294 {
24295 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24296 func_do_profile(fp);
24297 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024298 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024299 {
24300 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024301 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024302 profile_zero(&fp->uf_tm_children);
24303 }
24304 script_prof_save(&wait_start);
24305 }
24306#endif
24307
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024309 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024310 save_did_emsg = did_emsg;
24311 did_emsg = FALSE;
24312
24313 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024314 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24316
24317 --RedrawingDisabled;
24318
24319 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024320 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024322 clear_tv(rettv);
24323 rettv->v_type = VAR_NUMBER;
24324 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 }
24326
Bram Moolenaar05159a02005-02-26 23:04:13 +000024327#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024328 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024329 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024330 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024331 profile_end(&call_start);
24332 profile_sub_wait(&wait_start, &call_start);
24333 profile_add(&fp->uf_tm_total, &call_start);
24334 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024335 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024336 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024337 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24338 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024339 }
24340 }
24341#endif
24342
Bram Moolenaar071d4272004-06-13 20:20:40 +000024343 /* when being verbose, mention the return value */
24344 if (p_verbose >= 12)
24345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024347 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024350 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024351 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024352 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024353 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024354 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024356 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024357 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024358 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024359 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024360
Bram Moolenaar555b2802005-05-19 21:08:39 +000024361 /* The value may be very long. Skip the middle part, so that we
24362 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024363 * truncate it at the end. Don't want errors such as E724 here. */
24364 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024365 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024366 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024367 if (s != NULL)
24368 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024369 if (vim_strsize(s) > MSG_BUF_CLEN)
24370 {
24371 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24372 s = buf;
24373 }
24374 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024375 vim_free(tofree);
24376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 }
24378 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024379
24380 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381 --no_wait_return;
24382 }
24383
24384 vim_free(sourcing_name);
24385 sourcing_name = save_sourcing_name;
24386 sourcing_lnum = save_sourcing_lnum;
24387 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024388#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024389 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024390 script_prof_restore(&wait_start);
24391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392
24393 if (p_verbose >= 12 && sourcing_name != NULL)
24394 {
24395 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024396 verbose_enter_scroll();
24397
Bram Moolenaar555b2802005-05-19 21:08:39 +000024398 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024400
24401 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402 --no_wait_return;
24403 }
24404
24405 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024406 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024408
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024409 /* If the a:000 list and the l: and a: dicts are not referenced we can
24410 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024411 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24412 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24413 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24414 {
24415 free_funccal(fc, FALSE);
24416 }
24417 else
24418 {
24419 hashitem_T *hi;
24420 listitem_T *li;
24421 int todo;
24422
24423 /* "fc" is still in use. This can happen when returning "a:000" or
24424 * assigning "l:" to a global variable.
24425 * Link "fc" in the list for garbage collection later. */
24426 fc->caller = previous_funccal;
24427 previous_funccal = fc;
24428
24429 /* Make a copy of the a: variables, since we didn't do that above. */
24430 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24431 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24432 {
24433 if (!HASHITEM_EMPTY(hi))
24434 {
24435 --todo;
24436 v = HI2DI(hi);
24437 copy_tv(&v->di_tv, &v->di_tv);
24438 }
24439 }
24440
24441 /* Make a copy of the a:000 items, since we didn't do that above. */
24442 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24443 copy_tv(&li->li_tv, &li->li_tv);
24444 }
24445}
24446
24447/*
24448 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024449 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024450 */
24451 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024452can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024453{
24454 return (fc->l_varlist.lv_copyID != copyID
24455 && fc->l_vars.dv_copyID != copyID
24456 && fc->l_avars.dv_copyID != copyID);
24457}
24458
24459/*
24460 * Free "fc" and what it contains.
24461 */
24462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024463free_funccal(
24464 funccall_T *fc,
24465 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024466{
24467 listitem_T *li;
24468
24469 /* The a: variables typevals may not have been allocated, only free the
24470 * allocated variables. */
24471 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24472
24473 /* free all l: variables */
24474 vars_clear(&fc->l_vars.dv_hashtab);
24475
24476 /* Free the a:000 variables if they were allocated. */
24477 if (free_val)
24478 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24479 clear_tv(&li->li_tv);
24480
24481 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024482}
24483
24484/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024485 * Add a number variable "name" to dict "dp" with value "nr".
24486 */
24487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024488add_nr_var(
24489 dict_T *dp,
24490 dictitem_T *v,
24491 char *name,
24492 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024493{
24494 STRCPY(v->di_key, name);
24495 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24496 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24497 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024498 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024499 v->di_tv.vval.v_number = nr;
24500}
24501
24502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 * ":return [expr]"
24504 */
24505 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024506ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507{
24508 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024509 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510 int returning = FALSE;
24511
24512 if (current_funccal == NULL)
24513 {
24514 EMSG(_("E133: :return not inside a function"));
24515 return;
24516 }
24517
24518 if (eap->skip)
24519 ++emsg_skip;
24520
24521 eap->nextcmd = NULL;
24522 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024523 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024524 {
24525 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024526 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024528 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529 }
24530 /* It's safer to return also on error. */
24531 else if (!eap->skip)
24532 {
24533 /*
24534 * Return unless the expression evaluation has been cancelled due to an
24535 * aborting error, an interrupt, or an exception.
24536 */
24537 if (!aborting())
24538 returning = do_return(eap, FALSE, TRUE, NULL);
24539 }
24540
24541 /* When skipping or the return gets pending, advance to the next command
24542 * in this line (!returning). Otherwise, ignore the rest of the line.
24543 * Following lines will be ignored by get_func_line(). */
24544 if (returning)
24545 eap->nextcmd = NULL;
24546 else if (eap->nextcmd == NULL) /* no argument */
24547 eap->nextcmd = check_nextcmd(arg);
24548
24549 if (eap->skip)
24550 --emsg_skip;
24551}
24552
24553/*
24554 * Return from a function. Possibly makes the return pending. Also called
24555 * for a pending return at the ":endtry" or after returning from an extra
24556 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024557 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024558 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 * FALSE when the return gets pending.
24560 */
24561 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024562do_return(
24563 exarg_T *eap,
24564 int reanimate,
24565 int is_cmd,
24566 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567{
24568 int idx;
24569 struct condstack *cstack = eap->cstack;
24570
24571 if (reanimate)
24572 /* Undo the return. */
24573 current_funccal->returned = FALSE;
24574
24575 /*
24576 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24577 * not in its finally clause (which then is to be executed next) is found.
24578 * In this case, make the ":return" pending for execution at the ":endtry".
24579 * Otherwise, return normally.
24580 */
24581 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24582 if (idx >= 0)
24583 {
24584 cstack->cs_pending[idx] = CSTP_RETURN;
24585
24586 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024587 /* A pending return again gets pending. "rettv" points to an
24588 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024589 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024590 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591 else
24592 {
24593 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024594 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024596 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024598 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 {
24600 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024601 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024602 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603 else
24604 EMSG(_(e_outofmem));
24605 }
24606 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024607 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608
24609 if (reanimate)
24610 {
24611 /* The pending return value could be overwritten by a ":return"
24612 * without argument in a finally clause; reset the default
24613 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024614 current_funccal->rettv->v_type = VAR_NUMBER;
24615 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616 }
24617 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024618 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024619 }
24620 else
24621 {
24622 current_funccal->returned = TRUE;
24623
24624 /* If the return is carried out now, store the return value. For
24625 * a return immediately after reanimation, the value is already
24626 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024627 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024629 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024630 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024632 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633 }
24634 }
24635
24636 return idx < 0;
24637}
24638
24639/*
24640 * Free the variable with a pending return value.
24641 */
24642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024643discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644{
Bram Moolenaar33570922005-01-25 22:26:29 +000024645 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646}
24647
24648/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024649 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 * is an allocated string. Used by report_pending() for verbose messages.
24651 */
24652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024653get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024654{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024655 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024656 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024657 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024659 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024660 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024661 if (s == NULL)
24662 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024663
24664 STRCPY(IObuff, ":return ");
24665 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24666 if (STRLEN(s) + 8 >= IOSIZE)
24667 STRCPY(IObuff + IOSIZE - 4, "...");
24668 vim_free(tofree);
24669 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670}
24671
24672/*
24673 * Get next function line.
24674 * Called by do_cmdline() to get the next line.
24675 * Returns allocated string, or NULL for end of function.
24676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024678get_func_line(
24679 int c UNUSED,
24680 void *cookie,
24681 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682{
Bram Moolenaar33570922005-01-25 22:26:29 +000024683 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024684 ufunc_T *fp = fcp->func;
24685 char_u *retval;
24686 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687
24688 /* If breakpoints have been added/deleted need to check for it. */
24689 if (fcp->dbg_tick != debug_tick)
24690 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024691 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692 sourcing_lnum);
24693 fcp->dbg_tick = debug_tick;
24694 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024695#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024696 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024697 func_line_end(cookie);
24698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699
Bram Moolenaar05159a02005-02-26 23:04:13 +000024700 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024701 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24702 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703 retval = NULL;
24704 else
24705 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024706 /* Skip NULL lines (continuation lines). */
24707 while (fcp->linenr < gap->ga_len
24708 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24709 ++fcp->linenr;
24710 if (fcp->linenr >= gap->ga_len)
24711 retval = NULL;
24712 else
24713 {
24714 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24715 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024716#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024717 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024718 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024719#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024721 }
24722
24723 /* Did we encounter a breakpoint? */
24724 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24725 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024726 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024728 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729 sourcing_lnum);
24730 fcp->dbg_tick = debug_tick;
24731 }
24732
24733 return retval;
24734}
24735
Bram Moolenaar05159a02005-02-26 23:04:13 +000024736#if defined(FEAT_PROFILE) || defined(PROTO)
24737/*
24738 * Called when starting to read a function line.
24739 * "sourcing_lnum" must be correct!
24740 * When skipping lines it may not actually be executed, but we won't find out
24741 * until later and we need to store the time now.
24742 */
24743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024744func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024745{
24746 funccall_T *fcp = (funccall_T *)cookie;
24747 ufunc_T *fp = fcp->func;
24748
24749 if (fp->uf_profiling && sourcing_lnum >= 1
24750 && sourcing_lnum <= fp->uf_lines.ga_len)
24751 {
24752 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024753 /* Skip continuation lines. */
24754 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24755 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024756 fp->uf_tml_execed = FALSE;
24757 profile_start(&fp->uf_tml_start);
24758 profile_zero(&fp->uf_tml_children);
24759 profile_get_wait(&fp->uf_tml_wait);
24760 }
24761}
24762
24763/*
24764 * Called when actually executing a function line.
24765 */
24766 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024767func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024768{
24769 funccall_T *fcp = (funccall_T *)cookie;
24770 ufunc_T *fp = fcp->func;
24771
24772 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24773 fp->uf_tml_execed = TRUE;
24774}
24775
24776/*
24777 * Called when done with a function line.
24778 */
24779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024780func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024781{
24782 funccall_T *fcp = (funccall_T *)cookie;
24783 ufunc_T *fp = fcp->func;
24784
24785 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24786 {
24787 if (fp->uf_tml_execed)
24788 {
24789 ++fp->uf_tml_count[fp->uf_tml_idx];
24790 profile_end(&fp->uf_tml_start);
24791 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024792 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024793 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24794 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024795 }
24796 fp->uf_tml_idx = -1;
24797 }
24798}
24799#endif
24800
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801/*
24802 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024803 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 */
24805 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024806func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807{
Bram Moolenaar33570922005-01-25 22:26:29 +000024808 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809
24810 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24811 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024812 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813 || fcp->returned);
24814}
24815
24816/*
24817 * return TRUE if cookie indicates a function which "abort"s on errors.
24818 */
24819 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024820func_has_abort(
24821 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024822{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024823 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824}
24825
24826#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24827typedef enum
24828{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024829 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24830 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24831 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832} var_flavour_T;
24833
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024834static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835
24836 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024837var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838{
24839 char_u *p = varname;
24840
24841 if (ASCII_ISUPPER(*p))
24842 {
24843 while (*(++p))
24844 if (ASCII_ISLOWER(*p))
24845 return VAR_FLAVOUR_SESSION;
24846 return VAR_FLAVOUR_VIMINFO;
24847 }
24848 else
24849 return VAR_FLAVOUR_DEFAULT;
24850}
24851#endif
24852
24853#if defined(FEAT_VIMINFO) || defined(PROTO)
24854/*
24855 * Restore global vars that start with a capital from the viminfo file
24856 */
24857 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024858read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024859{
24860 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024861 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024862 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024863 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864
24865 if (!writing && (find_viminfo_parameter('!') != NULL))
24866 {
24867 tab = vim_strchr(virp->vir_line + 1, '\t');
24868 if (tab != NULL)
24869 {
24870 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024871 switch (*tab)
24872 {
24873 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024874#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024875 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024876#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024877 case 'D': type = VAR_DICT; break;
24878 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024879 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881
24882 tab = vim_strchr(tab, '\t');
24883 if (tab != NULL)
24884 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024885 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024886 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024887 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024889#ifdef FEAT_FLOAT
24890 else if (type == VAR_FLOAT)
24891 (void)string2float(tab + 1, &tv.vval.v_float);
24892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024894 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024895 if (type == VAR_DICT || type == VAR_LIST)
24896 {
24897 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24898
24899 if (etv == NULL)
24900 /* Failed to parse back the dict or list, use it as a
24901 * string. */
24902 tv.v_type = VAR_STRING;
24903 else
24904 {
24905 vim_free(tv.vval.v_string);
24906 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024907 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024908 }
24909 }
24910
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024911 /* when in a function use global variables */
24912 save_funccal = current_funccal;
24913 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024914 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024915 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024916
24917 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024918 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024919 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24920 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921 }
24922 }
24923 }
24924
24925 return viminfo_readline(virp);
24926}
24927
24928/*
24929 * Write global vars that start with a capital to the viminfo file
24930 */
24931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024932write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933{
Bram Moolenaar33570922005-01-25 22:26:29 +000024934 hashitem_T *hi;
24935 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024936 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024937 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024938 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024939 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024940 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941
24942 if (find_viminfo_parameter('!') == NULL)
24943 return;
24944
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024945 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024946
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024947 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024948 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024950 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024952 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024953 this_var = HI2DI(hi);
24954 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024955 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024956 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024957 {
24958 case VAR_STRING: s = "STR"; break;
24959 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024960 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024961 case VAR_DICT: s = "DIC"; break;
24962 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024963 case VAR_SPECIAL: s = "XPL"; break;
24964
24965 case VAR_UNKNOWN:
24966 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010024967 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +010024968 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000024969 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024970 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024971 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024972 if (p != NULL)
24973 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024974 vim_free(tofree);
24975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024976 }
24977 }
24978}
24979#endif
24980
24981#if defined(FEAT_SESSION) || defined(PROTO)
24982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024983store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024984{
Bram Moolenaar33570922005-01-25 22:26:29 +000024985 hashitem_T *hi;
24986 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024987 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 char_u *p, *t;
24989
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024990 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024991 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024993 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024995 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024996 this_var = HI2DI(hi);
24997 if ((this_var->di_tv.v_type == VAR_NUMBER
24998 || this_var->di_tv.v_type == VAR_STRING)
24999 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025000 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025001 /* Escape special characters with a backslash. Turn a LF and
25002 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025003 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025004 (char_u *)"\\\"\n\r");
25005 if (p == NULL) /* out of memory */
25006 break;
25007 for (t = p; *t != NUL; ++t)
25008 if (*t == '\n')
25009 *t = 'n';
25010 else if (*t == '\r')
25011 *t = 'r';
25012 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025013 this_var->di_key,
25014 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25015 : ' ',
25016 p,
25017 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25018 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025019 || put_eol(fd) == FAIL)
25020 {
25021 vim_free(p);
25022 return FAIL;
25023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 vim_free(p);
25025 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025026#ifdef FEAT_FLOAT
25027 else if (this_var->di_tv.v_type == VAR_FLOAT
25028 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25029 {
25030 float_T f = this_var->di_tv.vval.v_float;
25031 int sign = ' ';
25032
25033 if (f < 0)
25034 {
25035 f = -f;
25036 sign = '-';
25037 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025038 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025039 this_var->di_key, sign, f) < 0)
25040 || put_eol(fd) == FAIL)
25041 return FAIL;
25042 }
25043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025044 }
25045 }
25046 return OK;
25047}
25048#endif
25049
Bram Moolenaar661b1822005-07-28 22:36:45 +000025050/*
25051 * Display script name where an item was last set.
25052 * Should only be invoked when 'verbose' is non-zero.
25053 */
25054 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025055last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025056{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025057 char_u *p;
25058
Bram Moolenaar661b1822005-07-28 22:36:45 +000025059 if (scriptID != 0)
25060 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025061 p = home_replace_save(NULL, get_scriptname(scriptID));
25062 if (p != NULL)
25063 {
25064 verbose_enter();
25065 MSG_PUTS(_("\n\tLast set from "));
25066 MSG_PUTS(p);
25067 vim_free(p);
25068 verbose_leave();
25069 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025070 }
25071}
25072
Bram Moolenaard812df62008-11-09 12:46:09 +000025073/*
25074 * List v:oldfiles in a nice way.
25075 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025077ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025078{
25079 list_T *l = vimvars[VV_OLDFILES].vv_list;
25080 listitem_T *li;
25081 int nr = 0;
25082
25083 if (l == NULL)
25084 msg((char_u *)_("No old files"));
25085 else
25086 {
25087 msg_start();
25088 msg_scroll = TRUE;
25089 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25090 {
25091 msg_outnum((long)++nr);
25092 MSG_PUTS(": ");
25093 msg_outtrans(get_tv_string(&li->li_tv));
25094 msg_putchar('\n');
25095 out_flush(); /* output one line at a time */
25096 ui_breakcheck();
25097 }
25098 /* Assume "got_int" was set to truncate the listing. */
25099 got_int = FALSE;
25100
25101#ifdef FEAT_BROWSE_CMD
25102 if (cmdmod.browse)
25103 {
25104 quit_more = FALSE;
25105 nr = prompt_for_number(FALSE);
25106 msg_starthere();
25107 if (nr > 0)
25108 {
25109 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25110 (long)nr);
25111
25112 if (p != NULL)
25113 {
25114 p = expand_env_save(p);
25115 eap->arg = p;
25116 eap->cmdidx = CMD_edit;
25117 cmdmod.browse = FALSE;
25118 do_exedit(eap, NULL);
25119 vim_free(p);
25120 }
25121 }
25122 }
25123#endif
25124 }
25125}
25126
Bram Moolenaar53744302015-07-17 17:38:22 +020025127/* reset v:option_new, v:option_old and v:option_type */
25128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025129reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025130{
25131 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25132 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25133 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25134}
25135
25136
Bram Moolenaar071d4272004-06-13 20:20:40 +000025137#endif /* FEAT_EVAL */
25138
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025140#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025141
25142#ifdef WIN3264
25143/*
25144 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25145 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025146static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25147static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25148static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149
25150/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025151 * Get the short path (8.3) for the filename in "fnamep".
25152 * Only works for a valid file name.
25153 * When the path gets longer "fnamep" is changed and the allocated buffer
25154 * is put in "bufp".
25155 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25156 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025157 */
25158 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025159get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025161 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 char_u *newbuf;
25163
25164 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165 l = GetShortPathName(*fnamep, *fnamep, len);
25166 if (l > len - 1)
25167 {
25168 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025169 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025170 newbuf = vim_strnsave(*fnamep, l);
25171 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025173
25174 vim_free(*bufp);
25175 *fnamep = *bufp = newbuf;
25176
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025177 /* Really should always succeed, as the buffer is big enough. */
25178 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025179 }
25180
25181 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025182 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183}
25184
25185/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025186 * Get the short path (8.3) for the filename in "fname". The converted
25187 * path is returned in "bufp".
25188 *
25189 * Some of the directories specified in "fname" may not exist. This function
25190 * will shorten the existing directories at the beginning of the path and then
25191 * append the remaining non-existing path.
25192 *
25193 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025194 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025195 * bufp - Pointer to an allocated buffer for the filename.
25196 * fnamelen - Length of the filename pointed to by fname
25197 *
25198 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199 */
25200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025201shortpath_for_invalid_fname(
25202 char_u **fname,
25203 char_u **bufp,
25204 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025206 char_u *short_fname, *save_fname, *pbuf_unused;
25207 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025209 int old_len, len;
25210 int new_len, sfx_len;
25211 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212
25213 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025214 old_len = *fnamelen;
25215 save_fname = vim_strnsave(*fname, old_len);
25216 pbuf_unused = NULL;
25217 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025218
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025219 endp = save_fname + old_len - 1; /* Find the end of the copy */
25220 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025221
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025222 /*
25223 * Try shortening the supplied path till it succeeds by removing one
25224 * directory at a time from the tail of the path.
25225 */
25226 len = 0;
25227 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025229 /* go back one path-separator */
25230 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25231 --endp;
25232 if (endp <= save_fname)
25233 break; /* processed the complete path */
25234
25235 /*
25236 * Replace the path separator with a NUL and try to shorten the
25237 * resulting path.
25238 */
25239 ch = *endp;
25240 *endp = 0;
25241 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025242 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025243 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25244 {
25245 retval = FAIL;
25246 goto theend;
25247 }
25248 *endp = ch; /* preserve the string */
25249
25250 if (len > 0)
25251 break; /* successfully shortened the path */
25252
25253 /* failed to shorten the path. Skip the path separator */
25254 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 }
25256
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025257 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025259 /*
25260 * Succeeded in shortening the path. Now concatenate the shortened
25261 * path with the remaining path at the tail.
25262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025264 /* Compute the length of the new path. */
25265 sfx_len = (int)(save_endp - endp) + 1;
25266 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025268 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025270 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025272 /* There is not enough space in the currently allocated string,
25273 * copy it to a buffer big enough. */
25274 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025276 {
25277 retval = FAIL;
25278 goto theend;
25279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 }
25281 else
25282 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025283 /* Transfer short_fname to the main buffer (it's big enough),
25284 * unless get_short_pathname() did its work in-place. */
25285 *fname = *bufp = save_fname;
25286 if (short_fname != save_fname)
25287 vim_strncpy(save_fname, short_fname, len);
25288 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025290
25291 /* concat the not-shortened part of the path */
25292 vim_strncpy(*fname + len, endp, sfx_len);
25293 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025295
25296theend:
25297 vim_free(pbuf_unused);
25298 vim_free(save_fname);
25299
25300 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025301}
25302
25303/*
25304 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025305 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 */
25307 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025308shortpath_for_partial(
25309 char_u **fnamep,
25310 char_u **bufp,
25311 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312{
25313 int sepcount, len, tflen;
25314 char_u *p;
25315 char_u *pbuf, *tfname;
25316 int hasTilde;
25317
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025318 /* Count up the path separators from the RHS.. so we know which part
25319 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025321 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322 if (vim_ispathsep(*p))
25323 ++sepcount;
25324
25325 /* Need full path first (use expand_env() to remove a "~/") */
25326 hasTilde = (**fnamep == '~');
25327 if (hasTilde)
25328 pbuf = tfname = expand_env_save(*fnamep);
25329 else
25330 pbuf = tfname = FullName_save(*fnamep, FALSE);
25331
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025332 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025334 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336
25337 if (len == 0)
25338 {
25339 /* Don't have a valid filename, so shorten the rest of the
25340 * path if we can. This CAN give us invalid 8.3 filenames, but
25341 * there's not a lot of point in guessing what it might be.
25342 */
25343 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025344 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346 }
25347
25348 /* Count the paths backward to find the beginning of the desired string. */
25349 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025350 {
25351#ifdef FEAT_MBYTE
25352 if (has_mbyte)
25353 p -= mb_head_off(tfname, p);
25354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025355 if (vim_ispathsep(*p))
25356 {
25357 if (sepcount == 0 || (hasTilde && sepcount == 1))
25358 break;
25359 else
25360 sepcount --;
25361 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 if (hasTilde)
25364 {
25365 --p;
25366 if (p >= tfname)
25367 *p = '~';
25368 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025369 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370 }
25371 else
25372 ++p;
25373
25374 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25375 vim_free(*bufp);
25376 *fnamelen = (int)STRLEN(p);
25377 *bufp = pbuf;
25378 *fnamep = p;
25379
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025380 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381}
25382#endif /* WIN3264 */
25383
25384/*
25385 * Adjust a filename, according to a string of modifiers.
25386 * *fnamep must be NUL terminated when called. When returning, the length is
25387 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025388 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389 * When there is an error, *fnamep is set to NULL.
25390 */
25391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025392modify_fname(
25393 char_u *src, /* string with modifiers */
25394 int *usedlen, /* characters after src that are used */
25395 char_u **fnamep, /* file name so far */
25396 char_u **bufp, /* buffer for allocated file name or NULL */
25397 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398{
25399 int valid = 0;
25400 char_u *tail;
25401 char_u *s, *p, *pbuf;
25402 char_u dirname[MAXPATHL];
25403 int c;
25404 int has_fullname = 0;
25405#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025406 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407 int has_shortname = 0;
25408#endif
25409
25410repeat:
25411 /* ":p" - full path/file_name */
25412 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25413 {
25414 has_fullname = 1;
25415
25416 valid |= VALID_PATH;
25417 *usedlen += 2;
25418
25419 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25420 if ((*fnamep)[0] == '~'
25421#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25422 && ((*fnamep)[1] == '/'
25423# ifdef BACKSLASH_IN_FILENAME
25424 || (*fnamep)[1] == '\\'
25425# endif
25426 || (*fnamep)[1] == NUL)
25427
25428#endif
25429 )
25430 {
25431 *fnamep = expand_env_save(*fnamep);
25432 vim_free(*bufp); /* free any allocated file name */
25433 *bufp = *fnamep;
25434 if (*fnamep == NULL)
25435 return -1;
25436 }
25437
25438 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025439 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025440 {
25441 if (vim_ispathsep(*p)
25442 && p[1] == '.'
25443 && (p[2] == NUL
25444 || vim_ispathsep(p[2])
25445 || (p[2] == '.'
25446 && (p[3] == NUL || vim_ispathsep(p[3])))))
25447 break;
25448 }
25449
25450 /* FullName_save() is slow, don't use it when not needed. */
25451 if (*p != NUL || !vim_isAbsName(*fnamep))
25452 {
25453 *fnamep = FullName_save(*fnamep, *p != NUL);
25454 vim_free(*bufp); /* free any allocated file name */
25455 *bufp = *fnamep;
25456 if (*fnamep == NULL)
25457 return -1;
25458 }
25459
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025460#ifdef WIN3264
25461# if _WIN32_WINNT >= 0x0500
25462 if (vim_strchr(*fnamep, '~') != NULL)
25463 {
25464 /* Expand 8.3 filename to full path. Needed to make sure the same
25465 * file does not have two different names.
25466 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25467 p = alloc(_MAX_PATH + 1);
25468 if (p != NULL)
25469 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025470 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025471 {
25472 vim_free(*bufp);
25473 *bufp = *fnamep = p;
25474 }
25475 else
25476 vim_free(p);
25477 }
25478 }
25479# endif
25480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 /* Append a path separator to a directory. */
25482 if (mch_isdir(*fnamep))
25483 {
25484 /* Make room for one or two extra characters. */
25485 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25486 vim_free(*bufp); /* free any allocated file name */
25487 *bufp = *fnamep;
25488 if (*fnamep == NULL)
25489 return -1;
25490 add_pathsep(*fnamep);
25491 }
25492 }
25493
25494 /* ":." - path relative to the current directory */
25495 /* ":~" - path relative to the home directory */
25496 /* ":8" - shortname path - postponed till after */
25497 while (src[*usedlen] == ':'
25498 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25499 {
25500 *usedlen += 2;
25501 if (c == '8')
25502 {
25503#ifdef WIN3264
25504 has_shortname = 1; /* Postpone this. */
25505#endif
25506 continue;
25507 }
25508 pbuf = NULL;
25509 /* Need full path first (use expand_env() to remove a "~/") */
25510 if (!has_fullname)
25511 {
25512 if (c == '.' && **fnamep == '~')
25513 p = pbuf = expand_env_save(*fnamep);
25514 else
25515 p = pbuf = FullName_save(*fnamep, FALSE);
25516 }
25517 else
25518 p = *fnamep;
25519
25520 has_fullname = 0;
25521
25522 if (p != NULL)
25523 {
25524 if (c == '.')
25525 {
25526 mch_dirname(dirname, MAXPATHL);
25527 s = shorten_fname(p, dirname);
25528 if (s != NULL)
25529 {
25530 *fnamep = s;
25531 if (pbuf != NULL)
25532 {
25533 vim_free(*bufp); /* free any allocated file name */
25534 *bufp = pbuf;
25535 pbuf = NULL;
25536 }
25537 }
25538 }
25539 else
25540 {
25541 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25542 /* Only replace it when it starts with '~' */
25543 if (*dirname == '~')
25544 {
25545 s = vim_strsave(dirname);
25546 if (s != NULL)
25547 {
25548 *fnamep = s;
25549 vim_free(*bufp);
25550 *bufp = s;
25551 }
25552 }
25553 }
25554 vim_free(pbuf);
25555 }
25556 }
25557
25558 tail = gettail(*fnamep);
25559 *fnamelen = (int)STRLEN(*fnamep);
25560
25561 /* ":h" - head, remove "/file_name", can be repeated */
25562 /* Don't remove the first "/" or "c:\" */
25563 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25564 {
25565 valid |= VALID_HEAD;
25566 *usedlen += 2;
25567 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025568 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025569 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570 *fnamelen = (int)(tail - *fnamep);
25571#ifdef VMS
25572 if (*fnamelen > 0)
25573 *fnamelen += 1; /* the path separator is part of the path */
25574#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025575 if (*fnamelen == 0)
25576 {
25577 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25578 p = vim_strsave((char_u *)".");
25579 if (p == NULL)
25580 return -1;
25581 vim_free(*bufp);
25582 *bufp = *fnamep = tail = p;
25583 *fnamelen = 1;
25584 }
25585 else
25586 {
25587 while (tail > s && !after_pathsep(s, tail))
25588 mb_ptr_back(*fnamep, tail);
25589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590 }
25591
25592 /* ":8" - shortname */
25593 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25594 {
25595 *usedlen += 2;
25596#ifdef WIN3264
25597 has_shortname = 1;
25598#endif
25599 }
25600
25601#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025602 /*
25603 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025604 */
25605 if (has_shortname)
25606 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025607 /* Copy the string if it is shortened by :h and when it wasn't copied
25608 * yet, because we are going to change it in place. Avoids changing
25609 * the buffer name for "%:8". */
25610 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 {
25612 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025613 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 return -1;
25615 vim_free(*bufp);
25616 *bufp = *fnamep = p;
25617 }
25618
25619 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025620 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025621 if (!has_fullname && !vim_isAbsName(*fnamep))
25622 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025623 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624 return -1;
25625 }
25626 else
25627 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025628 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025629
Bram Moolenaardc935552011-08-17 15:23:23 +020025630 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025631 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025632 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025633 return -1;
25634
25635 if (l == 0)
25636 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025637 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025639 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 return -1;
25641 }
25642 *fnamelen = l;
25643 }
25644 }
25645#endif /* WIN3264 */
25646
25647 /* ":t" - tail, just the basename */
25648 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25649 {
25650 *usedlen += 2;
25651 *fnamelen -= (int)(tail - *fnamep);
25652 *fnamep = tail;
25653 }
25654
25655 /* ":e" - extension, can be repeated */
25656 /* ":r" - root, without extension, can be repeated */
25657 while (src[*usedlen] == ':'
25658 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25659 {
25660 /* find a '.' in the tail:
25661 * - for second :e: before the current fname
25662 * - otherwise: The last '.'
25663 */
25664 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25665 s = *fnamep - 2;
25666 else
25667 s = *fnamep + *fnamelen - 1;
25668 for ( ; s > tail; --s)
25669 if (s[0] == '.')
25670 break;
25671 if (src[*usedlen + 1] == 'e') /* :e */
25672 {
25673 if (s > tail)
25674 {
25675 *fnamelen += (int)(*fnamep - (s + 1));
25676 *fnamep = s + 1;
25677#ifdef VMS
25678 /* cut version from the extension */
25679 s = *fnamep + *fnamelen - 1;
25680 for ( ; s > *fnamep; --s)
25681 if (s[0] == ';')
25682 break;
25683 if (s > *fnamep)
25684 *fnamelen = s - *fnamep;
25685#endif
25686 }
25687 else if (*fnamep <= tail)
25688 *fnamelen = 0;
25689 }
25690 else /* :r */
25691 {
25692 if (s > tail) /* remove one extension */
25693 *fnamelen = (int)(s - *fnamep);
25694 }
25695 *usedlen += 2;
25696 }
25697
25698 /* ":s?pat?foo?" - substitute */
25699 /* ":gs?pat?foo?" - global substitute */
25700 if (src[*usedlen] == ':'
25701 && (src[*usedlen + 1] == 's'
25702 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25703 {
25704 char_u *str;
25705 char_u *pat;
25706 char_u *sub;
25707 int sep;
25708 char_u *flags;
25709 int didit = FALSE;
25710
25711 flags = (char_u *)"";
25712 s = src + *usedlen + 2;
25713 if (src[*usedlen + 1] == 'g')
25714 {
25715 flags = (char_u *)"g";
25716 ++s;
25717 }
25718
25719 sep = *s++;
25720 if (sep)
25721 {
25722 /* find end of pattern */
25723 p = vim_strchr(s, sep);
25724 if (p != NULL)
25725 {
25726 pat = vim_strnsave(s, (int)(p - s));
25727 if (pat != NULL)
25728 {
25729 s = p + 1;
25730 /* find end of substitution */
25731 p = vim_strchr(s, sep);
25732 if (p != NULL)
25733 {
25734 sub = vim_strnsave(s, (int)(p - s));
25735 str = vim_strnsave(*fnamep, *fnamelen);
25736 if (sub != NULL && str != NULL)
25737 {
25738 *usedlen = (int)(p + 1 - src);
25739 s = do_string_sub(str, pat, sub, flags);
25740 if (s != NULL)
25741 {
25742 *fnamep = s;
25743 *fnamelen = (int)STRLEN(s);
25744 vim_free(*bufp);
25745 *bufp = s;
25746 didit = TRUE;
25747 }
25748 }
25749 vim_free(sub);
25750 vim_free(str);
25751 }
25752 vim_free(pat);
25753 }
25754 }
25755 /* after using ":s", repeat all the modifiers */
25756 if (didit)
25757 goto repeat;
25758 }
25759 }
25760
Bram Moolenaar26df0922014-02-23 23:39:13 +010025761 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25762 {
25763 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25764 if (p == NULL)
25765 return -1;
25766 vim_free(*bufp);
25767 *bufp = *fnamep = p;
25768 *fnamelen = (int)STRLEN(p);
25769 *usedlen += 2;
25770 }
25771
Bram Moolenaar071d4272004-06-13 20:20:40 +000025772 return valid;
25773}
25774
25775/*
25776 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25777 * "flags" can be "g" to do a global substitute.
25778 * Returns an allocated string, NULL for error.
25779 */
25780 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025781do_string_sub(
25782 char_u *str,
25783 char_u *pat,
25784 char_u *sub,
25785 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786{
25787 int sublen;
25788 regmatch_T regmatch;
25789 int i;
25790 int do_all;
25791 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025792 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 garray_T ga;
25794 char_u *ret;
25795 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025796 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025797
25798 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25799 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025800 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025801
25802 ga_init2(&ga, 1, 200);
25803
25804 do_all = (flags[0] == 'g');
25805
25806 regmatch.rm_ic = p_ic;
25807 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25808 if (regmatch.regprog != NULL)
25809 {
25810 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025811 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25813 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025814 /* Skip empty match except for first match. */
25815 if (regmatch.startp[0] == regmatch.endp[0])
25816 {
25817 if (zero_width == regmatch.startp[0])
25818 {
25819 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025820 i = MB_PTR2LEN(tail);
25821 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25822 (size_t)i);
25823 ga.ga_len += i;
25824 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025825 continue;
25826 }
25827 zero_width = regmatch.startp[0];
25828 }
25829
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 /*
25831 * Get some space for a temporary buffer to do the substitution
25832 * into. It will contain:
25833 * - The text up to where the match is.
25834 * - The substituted text.
25835 * - The text after the match.
25836 */
25837 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025838 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25840 {
25841 ga_clear(&ga);
25842 break;
25843 }
25844
25845 /* copy the text up to where the match is */
25846 i = (int)(regmatch.startp[0] - tail);
25847 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25848 /* add the substituted text */
25849 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25850 + ga.ga_len + i, TRUE, TRUE, FALSE);
25851 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025852 tail = regmatch.endp[0];
25853 if (*tail == NUL)
25854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025855 if (!do_all)
25856 break;
25857 }
25858
25859 if (ga.ga_data != NULL)
25860 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25861
Bram Moolenaar473de612013-06-08 18:19:48 +020025862 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025863 }
25864
25865 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25866 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025867 if (p_cpo == empty_option)
25868 p_cpo = save_cpo;
25869 else
25870 /* Darn, evaluating {sub} expression changed the value. */
25871 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872
25873 return ret;
25874}
25875
25876#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */